[Variable] was not declared in this scope

Hallo, ich habe ein kleines Script geschrieben, welches auf meinem OLED-Display mit der random()-Funktion einen Matrix-Bildschirm simuliert. jedoch bekomme ich durchgehend den Error aus dem Titel. Hier der Code:

#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

void setup() {
  long step0 = random(1000000000000,9999999999999);
  long step1 = 0;
  long step2 = 0;
  long step3 = 0;
  long step4 = 0;
  long step5 = 0;

}

void loop() {
  
  step5 = step4;
  step4 = step3;
  step3 = step2;
  step2 = step1;
  step1 = step0;
  step0 = random(1000000000000,9999999999999);

  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setCursor(1,0);
  display.println(step0);
  display.setCursor(1,7);
  display.println(step1);  
  display.setCursor(1,14);
  display.println(step2);
  display.setCursor(1,21);
  display.println(step3);
  display.setCursor(1,28);
  display.println(step4);
  display.setCursor(1,35);
  display.println(step5);
  display.display();

  delay(100);
  
}

Und der komplette Error:

E:\Dokumente\Arduino\matrix_oled.ino\matrix_oled.ino.ino: In function 'void setup()':

E:\Dokumente\Arduino\matrix_oled.ino\matrix_oled.ino.ino:7:50: warning: overflow in implicit constant conversion [-Woverflow]

   long step0 = random(1000000000000,9999999999999);
                                                  ^
E:\Dokumente\Arduino\matrix_oled.ino\matrix_oled.ino.ino:7:50: warning: overflow in implicit constant conversion [-Woverflow]

E:\Dokumente\Arduino\matrix_oled.ino\matrix_oled.ino.ino: In function 'void loop()':

matrix_oled.ino:18: error: 'step5' was not declared in this scope

   step5 = step4;
   ^
matrix_oled.ino:18: error: 'step4' was not declared in this scope

   step5 = step4;
           ^
matrix_oled.ino:19: error: 'step3' was not declared in this scope

   step4 = step3;

           ^

matrix_oled.ino:20: error: 'step2' was not declared in this scope

   step3 = step2;
           ^
matrix_oled.ino:21: error: 'step1' was not declared in this scope

   step2 = step1;
           ^
matrix_oled.ino:22: error: 'step0' was not declared in this scope

   step1 = step0;
           ^
E:\Dokumente\Arduino\matrix_oled.ino\matrix_oled.ino.ino:23:45: warning: overflow in implicit constant conversion [-Woverflow]

   step0 = random(1000000000000,9999999999999);
                                             ^
E:\Dokumente\Arduino\matrix_oled.ino\matrix_oled.ino.ino:23:45: warning: overflow in implicit constant conversion [-Woverflow]

exit status 1
'step5' was not declared in this scope

Kann mir jemand sagen was falsch ist?

Random erwartet eine Zahl zwischen
min und max -1 (long)

versuche es mal Testweise mit (1, 9999)

Stefan

3Domse3:
... Und der komplette Error: ...

Wichtig ist bei den Fehlermeldungen im Grunde nur die erste Meldung. Und die sagt, dass Du Werte benutzt, die für den Variablentyp zu groß sind. Nimm in der betreffenden Zeile kleinere Werte für random(). Die folgenden Meldungen sind normalerweise weniger interessant, weil das häufig Folgefehler sind, die nach der Korrektur verschwinden.

Gruß

Gregor

Nein!
Das mit dem Random ist nur ein Warning!
Kaputte Daten, ja, aber kein Error.

Die andere Meldung ist das Problem.

: 'step2' was not declared in this scope

Und die sagt die Wahrheit.
Die definierten Variablen sind nur im Namensraum/Block der setup() Funktion gültig.
Das will verändert werden.

Tipp:
Variablen durchzunummerieren ist meist eine suboptimale Idee.
Arrays wurden genau für solche Zwecke erfunden.

Falls die (Folge)Fehler nicht verschwinden sollten,
versuchs mal so:

   #include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

long step0 = 0
  long step1 = 0;
  long step2 = 0;
  long step3 = 0;
  long step4 = 0;
  long step5 = 0

void setup() {
step0 = random(1,99999999);
}
.....

Combie hat natürlich recht. Die erste Meldung ist nur eine Warnung.

Variablen sind nur in dem „Block“ sichtbar, in dem sie deklariert wurden. Ein Block ist das, was zwischen { und } steht.

Gruß

Gregor

Ok. Ich probier es gleich mal aus. Vielen Dank :smiley: