123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- #include <Servo.h>
- #include <SSD1306Ascii.h>
- #include <SSD1306AsciiAvrI2c.h>
-
- #define PIN_POT A0
- #define PIN_ESC 5
- #define PIN_THERM A7
- #define PIN_BAT A2
-
- #define MIN_SPEED 900
- #define MAX_SPEED 1700
- #define ZERO_SPEED 900
- #define POT_MIN 220
- #define POT_MAX 1023
-
- #define STEP 10
- #define DISPLAY_I2C 0x3C
- #define DISPLAY_CHARS 11
- #define BAT_MAX 4.2
- #define BAT_MIN 3.6
- #define BAT_CELLS 6
- #define BAT_MULT 5
- #define THERM_RESISTOR 10000
- #define STATS_TIMER 3
-
- #define THRESHOLD 5
- #define DELAY 50
-
- #define USE_DISPLAY
-
- Servo esc;
- #ifdef USE_DISPLAY
- SSD1306AsciiAvrI2c display;
- #endif
-
- void setup() {
- Serial.begin(115200);
- Serial.println("Hello");
-
- #ifdef USE_DISPLAY
- Serial.println("Looking for i3c 0x"+String(DISPLAY_I2C, 16)+"...");
- display.begin(&Adafruit128x32, DISPLAY_I2C);
- display.setFont(Adafruit5x7);
- display.clear();
- display.set2X();
- display.println("SuperSkuter");
- display.println("3000");
- #endif
-
- pinMode(PIN_POT, INPUT);
- pinMode(PIN_THERM, INPUT);
- pinMode(PIN_BAT, INPUT);
-
- esc.attach(PIN_ESC);
- esc.writeMicroseconds(ZERO_SPEED);
- delay(4000);
- Serial.println("ready");
- }
-
- int prev = -1;
-
- float measureTemp() {
- int val = analogRead(PIN_THERM);
- float r2 = (THERM_RESISTOR / 10.0) / (1023.0 / (float)val - 1.0);
- float logR2 = log(r2);
- float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
- float t = (1.0 / (c1 + c2*logR2 + c3*logR2*logR2*logR2));
- float celsius = t - 273.15;
- return celsius;
- }
-
- int measureBat() {
- int val = analogRead(PIN_BAT);
- float volt = val * (5.0 / 1023.0);
- float currV = volt * BAT_MULT;
- float minV = BAT_MIN * BAT_CELLS;
- float maxV = BAT_MAX * BAT_CELLS;
-
- if (currV < minV)
- currV = minV;
- if (currV > maxV)
- currV = maxV;
-
- currV -= minV;
- maxV -= minV;
- return (currV / maxV) * 100;
- }
-
- void printTwo(String s1, String s2) {
- #ifdef USE_DISPLAY
- int nspaces = DISPLAY_CHARS - (s1.length() + s2.length());
- char spaces[DISPLAY_CHARS + 1];
- memset(spaces, ' ', DISPLAY_CHARS);
- spaces[nspaces] = '\0';
- display.println(s1+String(spaces)+s2);
- #endif
- }
-
- int statsCount = 1;
- void showStats(int actualSpeed, int potmeterVal) {
- statsCount -= 1;
- if (statsCount != 0)
- return;
- statsCount = STATS_TIMER + 1;
-
- int throttle = ((double)(actualSpeed - MIN_SPEED) / (double)(MAX_SPEED - MIN_SPEED)) * 100;
-
- #ifdef USE_DISPLAY
- display.setCursor(0, 0);
- display.set2X();
- #endif
-
- String pot = String(potmeterVal);
- String thr = String(throttle)+"%";
- String spd = String(actualSpeed);
- String tmp = String(round(measureTemp()))+"C";
- String bat = String(measureBat())+"%";
-
- printTwo(thr, pot+" ");
- printTwo(tmp, bat+" ");
- }
-
- void loop() {
- int pressed = 0;
- int reachedMax = 0;
-
- pressed = 1;
- int pot = analogRead(PIN_POT);
- int speed = map(pot, POT_MIN, POT_MAX, 0, MAX_SPEED - MIN_SPEED);
- speed = speed / STEP;
- speed = speed * STEP;
-
- int actualSpeed = speed + MIN_SPEED;
-
- 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 (actualSpeed != prev) {
- esc.writeMicroseconds(actualSpeed);
- prev = actualSpeed;
- }
-
- showStats(actualSpeed, pot);
- delay(DELAY);
- }
|