• Witaj na Forum Arduino Polska! Zapraszamy do rejestracji!
  • Znajdziesz tutaj wiele informacji na temat hardware / software.
Witaj! Logowanie Rejestracja


Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Problem z OLED 128x64 (Adafruit_SSD1306 & Adafruit_GFX)
#1
Cześć,

piszę w tym dziale, ale nie jestem pewien czy to problem wyświetlacza. Jestem nawet pewien, że to problem nie samego wyświetlacza ale wyszedł mi podczas prób jego użycia więc opiszę go tutaj.

Generalnie program jest prosty: jest 5 czujników deszczu. Jeśli którykolwiek z nich wskaże pomiar powyżej oczekiwanego uruchamiana jest pompa, oraz otwierany jest zawór powiązany z tym czujnikiem. Jeśli ani jeden nie wskaże pomiarów poniżej jakiejś wartości pompa jest wyłączana. 

Kod to realizujący:
 
Kod:
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define OLED_ADDR 0x3C  //defince OLED addres. Default was 0x3D and i thik it was adafruit default. But from aliexpress has onother athres. To find I2C address use I2Scanncer
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


#define RAIN_SENSOR_OPEN 1000
#define RAIN_SENSOR_CLOSE 400
int rainSensorPin[] = {A0, A1, A2, A3, A6}; //rain sesnors pins
int rainSensorValue[] = {0, 0, 0, 0, 0}; //rain sensor values

#define VALVE_OPEN 1
#define VALVE_CLOSE 0
int valvePin[] = {2, 3, 4, 5, 6};
int valveState[] = {VALVE_CLOSE, VALVE_CLOSE, VALVE_CLOSE, VALVE_CLOSE, VALVE_CLOSE};

#define PUMP_PIN 7

void setup() {
  Serial.begin(9600);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) { // Address OLED
    Serial.println(F("SSD1306 allocation failed"));
  }

  for(int i = 0; i < 5; i++){
    pinMode(valvePin[i], OUTPUT);
  }
  pinMode(PUMP_PIN, OUTPUT);

  // Clear the buffer
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 32);
  display.println("Auto Garden v 0.1.");
  display.display();
  delay(500);
 
}

void printTableOnLCD(){
 
  //vertical lines for dashboard
  display.drawLine(01, 01, 01, 31, SSD1306_WHITE);
  display.drawLine(16, 01, 16, 31, SSD1306_WHITE);
  display.drawLine(31, 01, 31, 31, SSD1306_WHITE);
  display.drawLine(46, 01, 46, 31, SSD1306_WHITE);
  display.drawLine(61, 01, 61, 31, SSD1306_WHITE);
  display.drawLine(76, 01, 76, 31, SSD1306_WHITE);
  display.drawLine(91, 01, 91, 31, SSD1306_WHITE);
  //horizontal lines for dashboard
  display.drawLine(01, 01, 91, 01, SSD1306_WHITE);
  display.drawLine(01, 11, 91, 11, SSD1306_WHITE);
  display.drawLine(01, 21, 91, 21, SSD1306_WHITE);
  display.drawLine(01, 31, 91, 31, SSD1306_WHITE);
 
  //draw water lvl symbol
  display.setCursor(04, 13);
  display.print("W");
  //display.drawBitmap(02, 12, image_water, 5, 5, SSD1306_WHITE);
 
  //draw valve symbol
  display.setCursor(04, 23);
  display.print("P");
  //display.drawBitmap(02, 22, image_valve, 5, 5, SSD1306_WHITE);
 
  //draw pupmp symbol
  //display.drawBitmap(65, 1, image_pump, 21, 16, SSD1306_WHITE);
 
}

void fillTableByData(){
  for(int i = 0; i < 5; i++){
    Serial.print("Loop Sensor ");
    Serial.print(i, DEC);
    Serial.print(" value: ");
    Serial.println(rainSensorValue[i]);
    display.setCursor(18 + 15 * i, 13);
    display.print(rainSensorValue[i]/10, DEC);
    display.setCursor(18 + 15 * i, 23);
    display.print(valveState[i] == VALVE_OPEN ? 1 : 0, DEC);
    //display.drawBitmap(12 + 10 * i, 22, valveState[i] == VALVE_OPEN ? image_on : image_off, 5, 5, SSD1306_WHITE);
  }
}

void loop() {
  int val = 0;
  bool isAnyOpenedValve = false;
  for(int i = 0; i < 5; i++){
    rainSensorValue[i] = analogRead(rainSensorPin[i]); //read value of 'i' sensor
    Serial.print("First Sensor ");
    Serial.print(i, DEC);
    Serial.print(" value: ");
    Serial.println(rainSensorValue[i]);
    if(rainSensorValue[i] >= RAIN_SENSOR_OPEN){ //if value greater or equal open valve thrshole
      openValve(valvePin[i]); //open valve and save them state
      isAnyOpenedValve = true; //indicate that valve is opened
    }
    if(rainSensorValue[i] <= RAIN_SENSOR_CLOSE){
      closeValve(valvePin[i]);
    }
    Serial.print("Sensor ");
    Serial.print(i, DEC);
    Serial.print(" value: ");
    Serial.println(rainSensorValue[i]);
  }

  if(isAnyOpenedValve){ //let's check if is any valve opened
    turnOnPump(); //if so then powe up pump! even if was switched on
  }else{
    turnOffPump(); //if no thet power off pump, even if was switched off
  }
  display.clearDisplay();
  printTableOnLCD();
  fillTableByData();
 
  display.display();      // Show initial text
  delay(5000);
}

void turnOnPump(){
  Serial.println("Pump switch on");
  digitalWrite(PUMP_PIN, HIGH);
}

void turnOffPump(){
  Serial.println("Pump switch off");
  digitalWrite(PUMP_PIN, LOW);
}


void openValve(int pin){
  valveChangeState(pin, VALVE_OPEN);
}

void closeValve(int pin){
  valveChangeState(pin, VALVE_CLOSE);
}

void valveChangeState(int pin, int state){
  valveState[pin] = state;
  digitalWrite(pin, state == 1 ? HIGH : LOW);
}

I teraz niby wszytko pięknie i ładnie, ale na serialu mam:
Cytat:23:03:21.901 -> First Sensor 0 value: 429
23:03:21.901 -> Sensor 0 value: 429
23:03:21.935 -> First Sensor 1 value: 440
23:03:21.968 -> Sensor 1 value: 440
23:03:22.002 -> First Sensor 2 value: 384
23:03:22.002 -> Sensor 2 value: 384
23:03:22.036 -> First Sensor 3 value: 452
23:03:22.070 -> Sensor 3 value: 452
23:03:22.104 -> First Sensor 4 value: 421
23:03:22.104 -> Sensor 4 value: 421
23:03:22.137 -> Pump switch off
23:03:22.171 -> Loop sensor 0 value: 429
23:03:22.171 -> Loop sensor 1 value: 440
23:03:22.204 -> Loop sensor 2 value: 384
23:03:22.238 -> Loop sensor 3 value: 452
23:03:22.271 -> Loop sensor 4 value: 421
23:03:27.295 -> First Sensor 0 value: 365
23:03:27.295 -> Sensor 0 value: 365
23:03:27.328 -> First Sensor 1 value: 362
23:03:27.362 -> Sensor 1 value: 362
23:03:27.396 -> First Sensor 2 value: 375
23:03:27.396 -> Sensor 2 value: 375
23:03:27.430 -> First Sensor 3 value: 395
23:03:27.464 -> Sensor 3 value: 395
23:03:27.498 -> First Sensor 4 value: 405
23:03:27.498 -> Sensor 4 value: 405
23:03:27.532 -> Pump switch off
23:03:27.567 -> Loop sensor 0 value: 0
23:03:27.567 -> Loop sensor 1 value: 362
23:03:27.567 -> Loop sensor 2 value: 375
23:03:27.602 -> Loop sensor 3 value: 395
23:03:27.636 -> Loop sensor 4 value: 405
23:03:32.652 -> First Sensor 0 value: 400
23:03:32.652 -> Sensor 0 value: 400
23:03:32.685 -> First Sensor 1 value: 398
23:03:32.719 -> Sensor 1 value: 398
23:03:32.754 -> First Sensor 2 value: 382
23:03:32.754 -> Sensor 2 value: 382
23:03:32.787 -> First Sensor 3 value: 405
23:03:32.820 -> Sensor 3 value: 405
23:03:32.854 -> First Sensor 4 value: 394
23:03:32.854 -> Sensor 4 value: 394
23:03:32.888 -> Pump switch off
23:03:32.922 -> Loop sensor 0 value: 400
23:03:32.922 -> Loop sensor 1 value: 0
23:03:32.956 -> Loop sensor 2 value: 382
23:03:32.989 -> Loop sensor 3 value: 405
23:03:33.024 -> Loop sensor 4 value: 394`

Zwróćcie proszę uwagę na to, że w niektórych okolicznościach rainSensorValue w funkcji fillTableByData pokazuje 0 (np. odczyt 23:03:27.567). Tak samo jest to prezentowane na wyświetlaczu. Z początku myślałem, że problem z wyświetlaczem, dlatego zdebugowałem wartości odczytywane z tejże zmiennej w funkcji i już one wskazują na 0. 

Moje pytanie jest: dlaczego? Nie jestem w stanie tego zrozumieć.

Płytka to arduino nano
 
Odpowiedź
#2
Zrób jakiś odstęp czasowy między odczytami i na razie zakomentuj elementy wykonawcze, zobacz czy one nie zakłócają odczytów.
 
Odpowiedź
#3
Z wyłączonym pisaniem na wyświetlacz:


Cytat:08:28:09.343 -> In messure loop values: 386 382 363 390 410
08:28:09.376 -> Pump switch off
08:28:09.376 -> In fillTableByData 386 382 363 390 410
08:28:14.441 -> In messure loop values: 400 406 397 428 427
08:28:14.475 -> Pump switch off
08:28:14.475 -> In fillTableByData 400 406 397 428 427
08:28:19.508 -> In messure loop values: 440 453 446 473 446
08:28:19.542 -> Pump switch off
08:28:19.542 -> In fillTableByData 440 453 446 473 446
08:28:24.587 -> In messure loop values: 435 445 433 456 426
08:28:24.621 -> Pump switch off
08:28:24.621 -> In fillTableByData 435 445 433 456 426 
08:28:31.266 -> In messure loop values: 393 384 363 389 411
08:28:31.300 -> Pump switch off
08:28:31.300 -> In fillTableByData 393 384 363 389 411
08:28:36.337 -> In messure loop values: 375 371 352 379 389
08:28:36.370 -> Pump switch off
08:28:36.370 -> In fillTableByData 375 371 352 379 389
08:28:41.448 -> In messure loop values: 368 370 358 389 396
08:28:41.482 -> Pump switch off
08:28:41.482 -> In fillTableByData 368 370 358 389 396
08:28:46.532 -> In messure loop values: 390 398 390 421 419
08:28:46.565 -> Pump switch off
08:28:46.565 -> In fillTableByData 390 398 390 421 419
08:28:51.587 -> In messure loop values: 430 442 435 470 446
08:28:51.621 -> Pump switch off
08:28:51.621 -> In fillTableByData 430 442 435 470 446 


Włączyłem pisanie na wyświetlacz:

Cytat:08:29:07.084 -> In messure loop values: 411 419 408 437 456 
08:29:07.118 -> Pump switch off
08:29:07.118 -> In fillTableByData 411 419 408 437 456
08:29:12.156 -> In messure loop values: 434 453 449 480 468
08:29:12.190 -> Pump switch off
08:29:12.190 -> In fillTableByData 434 453 449 480 468
08:29:17.228 -> In messure loop values: 450 465 460 485 464
08:29:17.262 -> Pump switch off
08:29:17.262 -> In fillTableByData 450 465 460 485 464
08:29:22.336 -> In messure loop values: 419 430 417 439 417
08:29:22.369 -> Pump switch off
08:29:22.369 -> In fillTableByData 419 430 417 439 417
08:29:27.389 -> In messure loop values: 403 409 396 419 404
08:29:27.423 -> Pump switch off
08:29:27.423 -> In fillTableByData 403 409 396 419 404
08:29:32.500 -> In messure loop values: 393 394 379 402 392
08:29:32.535 -> Pump switch off
08:29:32.535 -> In fillTableByData 393 0 379 402 392
08:29:37.577 -> In messure loop values: 409 415 402 425 408
08:29:37.611 -> Pump switch off
08:29:37.611 -> In fillTableByData 409 415 402 425 408
08:29:42.652 -> In messure loop values: 382 382 365 389 383
08:29:42.686 -> Pump switch off
08:29:42.686 -> In fillTableByData 0 0 365 389 383
08:29:47.714 -> In messure loop values: 415 421 408 431 414
08:29:47.749 -> Pump switch off
08:29:47.749 -> In fillTableByData 415 421 408 431 414
08:29:52.803 -> In messure loop values: 376 374 356 380 378
08:29:52.837 -> Pump switch off
08:29:52.837 -> In fillTableByData 0 0 356 380 378
08:29:57.887 -> In messure loop values: 402 406 391 413 400
08:29:57.921 -> Pump switch off
08:29:57.921 -> In fillTableByData 402 0 391 413 400
08:30:02.962 -> In messure loop values: 382 381 364 387 382
08:30:02.996 -> Pump switch off
08:30:02.996 -> In fillTableByData 0 0 364 387 382
08:30:08.035 -> In messure loop values: 398 402 386 408 396
08:30:08.069 -> Pump switch off
08:30:08.069 -> In fillTableByData 398 0 386 408 396
Jak widać w odczytach 08:29:52.837, 08:30:02.996 oraz 08:30:08.069 są wartości zerowe.

Ponownie wyłączyłem pisanie:

Cytat:08:30:22.842 -> In messure loop values: 451 468 460 481 488 
08:30:22.876 -> Pump switch off
08:30:22.876 -> In fillTableByData 451 468 460 481 488
08:30:27.910 -> In messure loop values: 428 434 419 440 415
08:30:27.943 -> Pump switch off
08:30:27.943 -> In fillTableByData 428 434 419 440 415
08:30:32.997 -> In messure loop values: 394 394 378 400 392
08:30:33.031 -> Pump switch off
08:30:33.031 -> In fillTableByData 394 394 378 400 392
08:30:38.088 -> In messure loop values: 366 363 344 368 369
08:30:38.122 -> Pump switch off
08:30:38.122 -> In fillTableByData 366 363 344 368 369
08:30:43.175 -> In messure loop values: 352 350 334 361 369
08:30:43.210 -> Pump switch off
08:30:43.210 -> In fillTableByData 352 350 334 361 369
08:30:48.254 -> In messure loop values: 353 350 333 363 371
08:30:48.288 -> Pump switch off
08:30:48.288 -> In fillTableByData 353 350 333 363 371
08:30:53.324 -> In messure loop values: 354 350 336 360 369
08:30:53.358 -> Pump switch off
08:30:53.358 -> In fillTableByData 354 350 336 360 369
08:30:58.415 -> In messure loop values: 354 351 334 362 370
08:30:58.448 -> Pump switch off
08:30:58.448 -> In fillTableByData 354 351 334 362 370
08:31:03.519 -> In messure loop values: 355 349 331 361 371
08:31:03.519 -> Pump switch off
08:31:03.519 -> In fillTableByData 355 349 331 361 371
08:31:08.598 -> In messure loop values: 353 350 334 362 370
08:31:08.633 -> Pump switch off
08:31:08.633 -> In fillTableByData 353 350 334 362 370
08:31:13.657 -> In messure loop values: 353 350 335 361 369
08:31:13.691 -> Pump switch off
08:31:13.691 -> In fillTableByData 353 350 335 361 369
08:31:18.742 -> In messure loop values: 353 353 341 371 383
08:31:18.777 -> Pump switch off
08:31:18.777 -> In fillTableByData 353 353 341 371 383
08:31:23.823 -> In messure loop values: 362 366 355 388 397
08:31:23.856 -> Pump switch off
08:31:23.856 -> In fillTableByData 362 366 355 388 397
08:31:28.924 -> In messure loop values: 366 370 361 393 401
08:31:28.958 -> Pump switch off
08:31:28.958 -> In fillTableByData 366 370 361 393 401



Aktualny kod:
Kod:
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define OLED_ADDR 0x3C  //defince OLED addres. Default was 0x3D and i thik it was adafruit default. But from aliexpress has onother athres. To find I2C address use I2Scanncer
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


#define RAIN_SENSOR_OPEN 1000
#define RAIN_SENSOR_CLOSE 400
int rainSensorPin[] = {A0, A1, A2, A3, A6}; //rain sesnors pins
int rainSensorValue[] = {0, 0, 0, 0, 0}; //rain sensor values

#define VALVE_OPEN 1
#define VALVE_CLOSE 0
int valvePin[] = {2, 3, 4, 5, 6};
int valveState[] = {VALVE_CLOSE, VALVE_CLOSE, VALVE_CLOSE, VALVE_CLOSE, VALVE_CLOSE};

#define PUMP_PIN 7

void setup() {
  Serial.begin(9600);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) { // Address OLED
    Serial.println(F("SSD1306 allocation failed"));
  }

  for(int i = 0; i < 5; i++){
    pinMode(valvePin[i], OUTPUT);
  }
  pinMode(PUMP_PIN, OUTPUT);

  // Clear the buffer
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 32);
  display.println("Auto Garden v 0.1.");
  display.display();
  delay(500);
 
}

void printTableOnLCD(){
 
  //vertical lines for dashboard
  display.drawLine(01, 01, 01, 31, SSD1306_WHITE);
  display.drawLine(16, 01, 16, 31, SSD1306_WHITE);
  display.drawLine(31, 01, 31, 31, SSD1306_WHITE);
  display.drawLine(46, 01, 46, 31, SSD1306_WHITE);
  display.drawLine(61, 01, 61, 31, SSD1306_WHITE);
  display.drawLine(76, 01, 76, 31, SSD1306_WHITE);
  display.drawLine(91, 01, 91, 31, SSD1306_WHITE);
  //horizontal lines for dashboard
  display.drawLine(01, 01, 91, 01, SSD1306_WHITE);
  display.drawLine(01, 11, 91, 11, SSD1306_WHITE);
  display.drawLine(01, 21, 91, 21, SSD1306_WHITE);
  display.drawLine(01, 31, 91, 31, SSD1306_WHITE);
 
  //draw water lvl symbol
  display.setCursor(04, 13);
  display.print("W");
  //display.drawBitmap(02, 12, image_water, 5, 5, SSD1306_WHITE);
 
  //draw valve symbol
  display.setCursor(04, 23);
  display.print("P");
  //display.drawBitmap(02, 22, image_valve, 5, 5, SSD1306_WHITE);
 
  //draw pupmp symbol
  //display.drawBitmap(65, 1, image_pump, 21, 16, SSD1306_WHITE);
 
}

void fillTableByData(){
  Serial.print("In fillTableByData ");
  for(int i = 0; i < 5; i++){
    Serial.print(rainSensorValue[i]);
    Serial.print(" ");
    /*
    display.setCursor(18 + 15 * i, 13);
    display.print(rainSensorValue[i]/10, DEC);
    display.setCursor(18 + 15 * i, 23);
    display.print(valveState[i] == VALVE_OPEN ? 1 : 0, DEC);
    */
//    display.drawBitmap(12 + 10 * i, 22, valveState[i] == VALVE_OPEN ? image_on : image_off, 5, 5, SSD1306_WHITE);
  }
  Serial.println();
}

void loop() {
  int val = 0;
  bool isAnyOpenedValve = false;
  Serial.print("In messure loop values: ");
  for(int i = 0; i < 5; i++){
    rainSensorValue[i] = analogRead(rainSensorPin[i]); //read value of 'i' sensor
    Serial.print(rainSensorValue[i]);
    Serial.print(" ");
    if(rainSensorValue[i] >= RAIN_SENSOR_OPEN){ //if value greater or equal open valve thrshole
      openValve(valvePin[i]); //open valve and save them state
      isAnyOpenedValve = true; //indicate that valve is opened
    }
    if(rainSensorValue[i] <= RAIN_SENSOR_CLOSE){
      closeValve(valvePin[i]);
    }
  }
  Serial.println();

  if(isAnyOpenedValve){ //let's check if is any valve opened
    turnOnPump(); //if so then powe up pump! even if was switched on
  }else{
    turnOffPump(); //if no thet power off pump, even if was switched off
  }
  display.clearDisplay();
  //printTableOnLCD();
  fillTableByData();
 
  display.display();      // Show initial text
  delay(5000);
}

void turnOnPump(){
  Serial.println("Pump switch on");
  digitalWrite(PUMP_PIN, HIGH);
}

void turnOffPump(){
  Serial.println("Pump switch off");
  digitalWrite(PUMP_PIN, LOW);
}


void openValve(int pin){
  valveChangeState(pin, VALVE_OPEN);
}

void closeValve(int pin){
  valveChangeState(pin, VALVE_CLOSE);
}

void valveChangeState(int pin, int state){
  valveState[pin] = state;
  digitalWrite(pin, state == 1 ? HIGH : LOW);
}
 
Odpowiedź
#4
I2C wykorzystuje piny analogowe A4 A5, widocznie ustaliłeś co zakłóca.
 
Odpowiedź
#5
no właśnie nie wydaje mi się. Spójrz na kod aplikacji.

Pobierane dane są ok i nie mają wartości zerowych.

Debug In messure loop values wyrzuca na konsole dane zaraz po pobraniu i tam nigdy zer nie ma.

Jak rozumiem dane pobierane z funkcji analogRead() to wartość a nie referencja. Więc dlaczego w pierwszej pętli odczytując tabelę rainSensorValue wartości są poprawne a później niepoprawne? 

Czy dane zapisane w tabeli rainSensorValue nie są wartościami z analogRead() a tylko referencjami do wartości z pinów?
 
Odpowiedź
#6
Faktycznie dane odczytane po chwili z tej samej tabeli powinny być takie same. Może zerknij ile RAM Ci zostaje, niestety nie mam takich bibliotek by to skompilować u siebie, czasami OLED zjada 1k i dla reszty zostaje za mało, OLED coś nadpisuje. Najprostsza weryfikacja to wziąć MEGA i zobaczyć czy dalej problem występuje.
 
Odpowiedź
#7
Jakby stos zajeżdżał na ram to by brednie raczej wypluwało a nie zera....
Arduino zostało wymyślone po to, by robić dobrze jedną prostą rzecz – migać diodą. 
 
Odpowiedź
#8
Nie mam możliwości wgrania tego na mega w prosty sposób niestety :/ oczywiście jak nie będzie wyboru to to zrobię.

Pozwolę sobie jednak chwilę jeszcze poczekać. Liczę na to, że ktoś wpadnie na genialną myśl, która rozwiąże problem.

W międzyczasie napisze jeszcze na forum Adafruit. Zobaczę, może oni coś mi powiedzą.
 
Odpowiedź
#9
Ja ci powiem  dziwne że ci to ardunio przepuszcza... Odwołujesz się do funkcji zanim została zdefiniowana...

np linijka 107
a zdefiniowane w 140 linijce tak nie może być


Zobacz teraz 

Kod:
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define OLED_ADDR 0x3C  //defince OLED addres. Default was 0x3D and i thik it was adafruit default. But from aliexpress has onother athres. To find I2C address use I2Scanncer
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


#define RAIN_SENSOR_OPEN 1000
#define RAIN_SENSOR_CLOSE 400
int rainSensorPin[] = {A0, A1, A2, A3, A6}; //rain sesnors pins
int rainSensorValue[] = {0, 0, 0, 0, 0}; //rain sensor values

#define VALVE_OPEN 1
#define VALVE_CLOSE 0
int valvePin[] = {2, 3, 4, 5, 6};
int valveState[] = {VALVE_CLOSE, VALVE_CLOSE, VALVE_CLOSE, VALVE_CLOSE, VALVE_CLOSE};

#define PUMP_PIN 7

void setup() {
  Serial.begin(9600);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) { // Address OLED
    Serial.println(F("SSD1306 allocation failed"));
  }

  for(int i = 0; i < 5; i++){
    pinMode(valvePin[i], OUTPUT);
  }
  pinMode(PUMP_PIN, OUTPUT);

  // Clear the buffer
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 32);
  display.println("Auto Garden v 0.1.");
  display.display();
  delay(500);

}

void printTableOnLCD(){

  //vertical lines for dashboard
  display.drawLine(01, 01, 01, 31, SSD1306_WHITE);
  display.drawLine(16, 01, 16, 31, SSD1306_WHITE);
  display.drawLine(31, 01, 31, 31, SSD1306_WHITE);
  display.drawLine(46, 01, 46, 31, SSD1306_WHITE);
  display.drawLine(61, 01, 61, 31, SSD1306_WHITE);
  display.drawLine(76, 01, 76, 31, SSD1306_WHITE);
  display.drawLine(91, 01, 91, 31, SSD1306_WHITE);
  //horizontal lines for dashboard
  display.drawLine(01, 01, 91, 01, SSD1306_WHITE);
  display.drawLine(01, 11, 91, 11, SSD1306_WHITE);
  display.drawLine(01, 21, 91, 21, SSD1306_WHITE);
  display.drawLine(01, 31, 91, 31, SSD1306_WHITE);

  //draw water lvl symbol
  display.setCursor(04, 13);
  display.print("W");
  //display.drawBitmap(02, 12, image_water, 5, 5, SSD1306_WHITE);

  //draw valve symbol
  display.setCursor(04, 23);
  display.print("P");
  //display.drawBitmap(02, 22, image_valve, 5, 5, SSD1306_WHITE);

  //draw pupmp symbol
  //display.drawBitmap(65, 1, image_pump, 21, 16, SSD1306_WHITE);

}

void fillTableByData(){
  Serial.print("In fillTableByData ");
  for(int i = 0; i < 5; i++){
    Serial.print(rainSensorValue[i]);
    Serial.print(" ");
    /*
    display.setCursor(18 + 15 * i, 13);
    display.print(rainSensorValue[i]/10, DEC);
    display.setCursor(18 + 15 * i, 23);
    display.print(valveState[i] == VALVE_OPEN ? 1 : 0, DEC);
    */
//    display.drawBitmap(12 + 10 * i, 22, valveState[i] == VALVE_OPEN ? image_on : image_off, 5, 5, SSD1306_WHITE);
  }
  Serial.println();
}

void valveChangeState(int pin, int state){
  valveState[pin] = state;
  digitalWrite(pin, state == 1 ? HIGH : LOW);
}

void turnOnPump(){
  Serial.println("Pump switch on");
  digitalWrite(PUMP_PIN, HIGH);
}

void turnOffPump(){
  Serial.println("Pump switch off");
  digitalWrite(PUMP_PIN, LOW);
}


void openValve(int pin){
  valveChangeState(pin, VALVE_OPEN);
}

void closeValve(int pin){
  valveChangeState(pin, VALVE_CLOSE);
}


void loop() {
  bool isAnyOpenedValve = false;
  Serial.print("In messure loop values: ");
  for(int i = 0; i < 5; i++){
    rainSensorValue[i] = analogRead(rainSensorPin[i]); //read value of 'i' sensor
    Serial.print(rainSensorValue[i]);
    Serial.print(" ");
    if(rainSensorValue[i] >= RAIN_SENSOR_OPEN){ //if value greater or equal open valve thrshole
      openValve(valvePin[i]); //open valve and save them state
      isAnyOpenedValve = true; //indicate that valve is opened
    }
    if(rainSensorValue[i] <= RAIN_SENSOR_CLOSE){
      closeValve(valvePin[i]);
    }
  }
  Serial.println();

  if(isAnyOpenedValve){ //let's check if is any valve opened
    turnOnPump(); //if so then powe up pump! even if was switched on
  }else{
    turnOffPump(); //if no thet power off pump, even if was switched off
  }
  display.clearDisplay();
  //printTableOnLCD();
  fillTableByData();

  display.display();      // Show initial text
  delay(5000);
}
Arduino zostało wymyślone po to, by robić dobrze jedną prostą rzecz – migać diodą. 
 
Odpowiedź
#10
to że puszcza nie jest zaskakujące IMO. Przyznam, że mało na Arduino programuje, ale ostatnie czasy jakie pamiętam że linker+kompiler sobie z tym nie radziły to Turbo Pascal Wink

Znalazłem linijkę, która jest winowajcą. W sensie jej zakomentowanie powoduje, że wartości są poprawne. Nie doszedłem jednak dlaczego. Zakomentowanie:
Kod:
display.print(valveState[i] == VALVE_OPEN ? 1 : 0, DEC);

w funkcji fillTableByData zatrzymuje występowanie błędu.

Zamiana powyższego na 
Kod:
    tmpValveState = valveState[i] == VALVE_OPEN ? 1 : 0;
    display.print(tmpValveState, DEC);
także nie rozwiązuje problemu. Czy źle używam składni języka?
 
Odpowiedź
  


Skocz do:


Przeglądający: 1 gości