Skocz do zawartości

Zawieszanie arduino nano(klon), jestem amatorem szukam wskazówek/wsparcia w małym projekcie


Pomocna odpowiedź

#include <Adafruit_SH1106.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>


#define time 200
#define SCREEN_WIDTH 128  // OLED display width, in pixels
#define SCREEN_HEIGHT 64  // OLED display height, in pixels
#define OBWOD 0.8         // obwodkoła
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1  // Reset pin # (or -1 if sharing Arduino reset pin)
//Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_SH1106 display(OLED_RESET);

const int debounceTime = 20;
bool count = 0;
int num = 0;
bool hactive = 0;
float szer = 0;
float hektary = 0;
float predkosc = 0;
float velo = 0;
float droga = 0;
float czas = 0;
word obrot;
byte state;
int half_revolution_time = 0;
unsigned long last_event = 0;
float dst = OBWOD / 1000;
int time0, time1, time2, time3;

long switchTime(int pin, bool &state, unsigned long &startTime) {

  if (digitalRead(pin) != state) {
    state = !state;
    startTime = millis();
  }
  if (state == LOW) {
    return millis() - startTime;
  } else {
    return 0;
  }
} /*badanie czasu wciśniętego przycisku*/

long switchA() {
  static unsigned long startTime = 0;
  static bool state;
  return switchTime(4, state, startTime);
}

long switchB() {
  static unsigned long startTime = 0;
  static bool state;
  return switchTime(5, state, startTime);
}

void rpm_fan() {
  unsigned long current_time = millis();
  if (current_time - last_event < 10) {  // debounce
    return;
  }
  half_revolution_time = (millis() - last_event);
  last_event = current_time;
  droga += dst;
}

void setup() {
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  Serial.begin(115200);
  display.begin(SH1106_SWITCHCAPVCC, 0x3C);
  display.setTextColor(WHITE);
}

void loop() {
  unsigned long current_time = millis();
  if (num == 0) {
    display.clearDisplay();
    display.setTextSize(2);
    display.setCursor(20, 4);
    display.print("Renault");
    display.setCursor(30, 30);
    display.print("103-54");
    display.display();
    delay(3000);
    num = 1;
  }
  if (half_revolution_time != 0) {
    velo = predkosc;
    predkosc = (OBWOD / half_revolution_time) * 3600;
  } else {
    predkosc = 0;
  }

  if (current_time - last_event >= 3000) {
    predkosc = 0;
  }

  if (szer != 0 && hactive == 1) { /*funkcja dająca mozliwosc uruchomienia hektaromierza*/
    hektary = droga * szer / 10;
  }

  if (switchA() > debounceTime && hactive == 1) {
    if (num == 2) {
      num = 1;
    } else {
      num = 2;
    }
    delay(time);
  }
  if (switchB() > debounceTime && num == 1) {
    if (hactive == 1) {
      hactive = 0;
    } else {
      hactive = 1;
    }
    delay(time);
  }
  if (switchB() > debounceTime && num == 2) {
    szer += 0.1;
    if (szer > 4) {
      szer = 1;
    }
    delay(time);
  }

  if (digitalRead(4) == LOW && digitalRead(5) == LOW) {
    time3 = millis();
    if ((digitalRead(4) == LOW && digitalRead(5) == LOW) && millis() - time3 >= time) {
      hektary = 0;
    }
  }

  if (current_time - czas >= 200) {
    display.clearDisplay();
    czas = millis();
    if (num == 1) {
      display.setCursor(15, 10);
      display.setTextSize(3);
      display.print(predkosc, 1);
      display.drawLine(0, 45, 128, 45, WHITE);
      display.drawLine(0, 42, 128, 42, WHITE);
      display.setTextSize(1);
      display.setCursor(80, 25);
      display.print(" km/h");
      if (hactive == 1) {
        display.setCursor(15, 50);
        display.setTextSize(2);
        display.print(hektary, 3);  // hektary - obwód koła x szerokość maszyny podzielić przez hektar (10000m20
        display.setCursor(90, 57);
        display.setTextSize(1);
        display.print(" ha AC");
        display.display();
      } else {
        display.setCursor(15, 50);
        display.setTextSize(2);
        display.print(droga, 3);  // hektary - obwód koła x szerokość maszyny podzielić przez hektar (10000m20)
        display.setCursor(90, 57);
        display.setTextSize(1);
        display.print(" km");
        display.display();
      }
    }
    if (num == 2) {
      display.setCursor(5, 0);
      display.setTextSize(2);
      display.print("SZEROKOSC:");
      display.setCursor(35, 25);
      display.setTextSize(3);
      display.print(szer, 1);
      display.setTextSize(1);
      display.setCursor(60, 50);
      display.print("metr(y)");
      display.display();
    }
  }
}

 

Uważacie, że użycie takiego kodu do drgan styków ma sens? Bo ostatecznie i tak musze użyć delay.

long switchTime(int pin, bool &state, unsigned long &startTime) {

  if (digitalRead(pin) != state) {
    state = !state;
    startTime = millis();
  }
  if (state == LOW) {
    return millis() - startTime;
  } else {
    return 0;
  }
} /*badanie czasu wciśniętego przycisku*/

long switchA() {
  static unsigned long startTime = 0;
  static bool state;
  return switchTime(4, state, startTime);
}

long switchB() {
  static unsigned long startTime = 0;
  static bool state;
  return switchTime(5, state, startTime);
}

i przykład:

if (switchB() > debounceTime && num == 2) {
    szer += 0.1;
    if (szer > 4) {
      szer = 1;
    }
    delay(time);
  }

delay -  musiałem użyć bo tak to możecie się domyślić jak dodawała się wartość szer...

  • 3 tygodnie później...

prototyp zrobiony, w koncu sie zebralem, jakby ktos chcial jakies pliki lub potrzebowal podobnego urządzenia, moze pisac, 

obudowa bedzie nowa, teraz to tylko szybki prototyp robiony na pretce byle cos bylo

zrobie nowa obudowe ewentualnie jakby ktos cos chcial to udostepnie pliki stl jak bedzie cos ladniejszego, 

urzadzonko bedzie rozwijane do roznych zadan, chocby jak dalaczenie modulu ehr oraz przeplywomierza do sprawdzania spalania

w tej chwili dodatkowo urzadzenie przesyla dane na telefon ktore mozna zapisywac sobie w notatkach lub gdzie kto woli

image.jpg

  • Lubię! 2

Bądź aktywny - zaloguj się lub utwórz konto!

Tylko zarejestrowani użytkownicy mogą komentować zawartość tej strony

Utwórz konto w ~20 sekund!

Zarejestruj nowe konto, to proste!

Zarejestruj się »

Zaloguj się

Posiadasz własne konto? Użyj go!

Zaloguj się »
×
×
  • Utwórz nowe...