| #define PIN_THERM A7 | #define PIN_THERM A7 | ||||
| #define PIN_BAT A2 | #define PIN_BAT A2 | ||||
| #define MIN_SPEED 1000 | |||||
| #define MIN_SPEED 1100 | |||||
| #define MAX_SPEED 1700 | #define MAX_SPEED 1700 | ||||
| #define ZERO_SPEED 900 | |||||
| #define ZERO_SPEED 1000 | |||||
| //#define MIN_SPEED 90 | |||||
| //#define MAX_SPEED 180 | |||||
| //#define ZERO_SPEED 90 | |||||
| #define ACCEL 10 | |||||
| #define POT_MIN 220 | #define POT_MIN 220 | ||||
| #define POT_MAX 1023 | #define POT_MAX 1023 | ||||
| #define STEP 10 | |||||
| #define STEP 5 | |||||
| #define DISPLAY_I2C 0x3C | #define DISPLAY_I2C 0x3C | ||||
| #define DISPLAY_CHARS 11 | #define DISPLAY_CHARS 11 | ||||
| #define BAT_MAX 4.2 | #define BAT_MAX 4.2 | ||||
| pinMode(PIN_BAT, INPUT); | pinMode(PIN_BAT, INPUT); | ||||
| esc.attach(PIN_ESC); | esc.attach(PIN_ESC); | ||||
| esc.writeMicroseconds(ZERO_SPEED); | |||||
| esc.writeMicroseconds(MIN_SPEED); | |||||
| delay(4000); | delay(4000); | ||||
| Serial.println("ready"); | Serial.println("ready"); | ||||
| } | } | ||||
| return celsius; | return celsius; | ||||
| } | } | ||||
| int batteryOk = 1; | |||||
| float voltLim = ((float)BAT_MIN * (float)BAT_CELLS) / (float)BAT_MULT; | |||||
| int measureBat() { | int measureBat() { | ||||
| int val = analogRead(PIN_BAT); | int val = analogRead(PIN_BAT); | ||||
| float volt = val * (5.0 / 1023.0); | float volt = val * (5.0 / 1023.0); | ||||
| float minV = BAT_MIN * BAT_CELLS; | float minV = BAT_MIN * BAT_CELLS; | ||||
| float maxV = BAT_MAX * BAT_CELLS; | float maxV = BAT_MAX * BAT_CELLS; | ||||
| if (volt <= voltLim) | |||||
| batteryOk = 0; | |||||
| if (currV < minV) | if (currV < minV) | ||||
| currV = minV; | currV = minV; | ||||
| if (currV > maxV) | if (currV > maxV) | ||||
| return; | return; | ||||
| statsCount = STATS_TIMER + 1; | statsCount = STATS_TIMER + 1; | ||||
| int throttle = ((double)(actualSpeed - MIN_SPEED) / (double)(MAX_SPEED - MIN_SPEED)) * 100; | |||||
| int throttle = ((double)(actualSpeed - ZERO_SPEED) / (double)(MAX_SPEED - ZERO_SPEED)) * 100; | |||||
| #ifdef USE_DISPLAY | #ifdef USE_DISPLAY | ||||
| display.setCursor(0, 0); | display.setCursor(0, 0); | ||||
| String tmp = String(round(measureTemp()))+"C"; | String tmp = String(round(measureTemp()))+"C"; | ||||
| String bat = String(measureBat())+"%"; | String bat = String(measureBat())+"%"; | ||||
| printTwo(thr, pot+" "); | |||||
| if (batteryOk) { | |||||
| //printTwo(thr, pot+" "); | |||||
| printTwo(thr, spd+" "); | |||||
| } else { | |||||
| printTwo("Battery", ":( "); | |||||
| } | |||||
| printTwo(tmp, bat+" "); | printTwo(tmp, bat+" "); | ||||
| } | } | ||||
| int currentSpeed = ZERO_SPEED; | |||||
| void loop() { | void loop() { | ||||
| int pressed = 0; | int pressed = 0; | ||||
| int reachedMax = 0; | int reachedMax = 0; | ||||
| pressed = 1; | pressed = 1; | ||||
| int pot = analogRead(PIN_POT); | int pot = analogRead(PIN_POT); | ||||
| int speed = map(pot, POT_MIN, POT_MAX, 0, MAX_SPEED - MIN_SPEED); | |||||
| int speed = map(pot, POT_MIN, POT_MAX, 0, MAX_SPEED - ZERO_SPEED); | |||||
| speed = speed / STEP; | speed = speed / STEP; | ||||
| speed = speed * STEP; | speed = speed * STEP; | ||||
| int actualSpeed = speed + MIN_SPEED; | |||||
| int actualSpeed = speed + ZERO_SPEED; | |||||
| if (actualSpeed > currentSpeed) { | |||||
| if (currentSpeed < MIN_SPEED) | |||||
| currentSpeed = MIN_SPEED; | |||||
| else | |||||
| currentSpeed += ACCEL; | |||||
| } else { | |||||
| currentSpeed -= ACCEL; | |||||
| } | |||||
| if (abs(actualSpeed - ZERO_SPEED) < THRESHOLD) | |||||
| actualSpeed = ZERO_SPEED; | |||||
| else if (actualSpeed > MAX_SPEED) | |||||
| actualSpeed = MAX_SPEED; | |||||
| else if (actualSpeed < MIN_SPEED) | |||||
| actualSpeed = MIN_SPEED; | |||||
| if (currentSpeed > MAX_SPEED) | |||||
| currentSpeed = MAX_SPEED; | |||||
| else if (currentSpeed < ZERO_SPEED) | |||||
| currentSpeed = ZERO_SPEED; | |||||
| if (actualSpeed != prev) { | |||||
| esc.writeMicroseconds(actualSpeed); | |||||
| prev = actualSpeed; | |||||
| } | |||||
| //if (currentSpeed != prev) { | |||||
| esc.writeMicroseconds(currentSpeed); | |||||
| //esc.write(currentSpeed); | |||||
| prev = currentSpeed; | |||||
| //} | |||||
| showStats(actualSpeed, pot); | |||||
| showStats(currentSpeed, pot); | |||||
| delay(DELAY); | delay(DELAY); | ||||
| } | } |