'intervalon' was not declared in this scope

Hallo ich bekomme mit folgenden Code den Fehler :

C:\Users\user\Arduino\arduino_3_motoren_ohne_delay\sketch_feb20b\sketch_feb20b.ino: In function ‘void loop()’:

C:\Users\user\Arduino\arduino_3_motoren_ohne_delay\sketch_feb20b\sketch_feb20b.ino:68:21: warning: NULL used in arithmetic [-Wpointer-arith]

if (values[0] ==NULL){

^

sketch_feb20b:99: error: ‘intervalon’ was not declared in this scope

if(ledState == HIGH && currentMillis - previousMillis > intervalon ) {

^

sketch_feb20b:105: error: ‘intervaloff’ was not declared in this scope

if(ledState == LOW && currentMillis - previousMillis > intervaloff) {

^

sketch_feb20b:113: error: ‘intervalon2’ was not declared in this scope

if(ledState2 == HIGH && currentMillis2 - previousMillis2 > intervalon2 ) {

^

sketch_feb20b:119: error: ‘intervaloff2’ was not declared in this scope

if(ledState2 == LOW && currentMillis2 - previousMillis2 > intervaloff2) {

^

sketch_feb20b:127: error: ‘intervalon3’ was not declared in this scope

if(ledState3 == HIGH && currentMillis3 - previousMillis3 > intervalon3 ) {

^

sketch_feb20b:133: error: ‘intervaloff3’ was not declared in this scope

if(ledState3 == LOW && currentMillis3 - previousMillis3 > intervaloff3) {

^

exit status 1
‘intervalon’ was not declared in this scope

Ziel ist es über bluetooth Werte zu senden, um 3 Motoren zu steuern.
So lange keine Werte gesendet werden, sollen keine der Motoren laufen, deshalb dachte ich, ich mache eine Abfrage ob die Variablen gleich Null sind oder einen Wert enthalten.

#include <AFMotor.h>

//////////////////////////////////
const int NUMBER_OF_FIELDS = 6; // Wie viele kommaseparierte Felder erwarten wir?
int fieldIndex = 0;             // Das aktuell empfangene Feld
int values[NUMBER_OF_FIELDS];   // Array mit den Werte aller Felder
void readSerialString () {
   if( Serial.available()) {
    for(fieldIndex = 0; fieldIndex  < NUMBER_OF_FIELDS; fieldIndex ++)
    {
      values[fieldIndex] = Serial.parseInt(); // Numerischen Wert einlesen

    }
    Serial.print( fieldIndex);
    Serial.println(" Felder empfangen:");
    for(int i=0; i <  fieldIndex; i++)
    {
       Serial.println(values[i]);
    }
    //fieldIndex = 0;  // und von vorn anfangen
  } 
}


// this created our first motor (called "motor") on port 3 of the motorshield:
AF_DCMotor motor(1, MOTOR12_64KHZ);  

// we do it again, this time our second motor will be called "motor2" on port
AF_DCMotor motor2(2, MOTOR12_64KHZ);

// we do it again, this time our second motor will be called "motor2" on port
AF_DCMotor motor3(3, MOTOR12_64KHZ);

// so now we have 2 motors called "motor" and "motor2"
// Variables will change:
int ledState = LOW;             // ledState used to set the LED
int ledState2 = LOW;             // ledState used to set the LED
int ledState3 = LOW;             // ledState used to set the LED

long previousMillis = 0;        // will store last time LED was updated
long previousMillis2 = 0;        // will store last time LED was updated
long previousMillis3 = 0;        // will store last time LED was updated





void setup() {
  
    Serial.begin(9600);           // set up Serial library at 9600 bps
    Serial.println("Motor test!");
    
    // set the speed to 200 of 255 of "motor".  Note that 0 is stop,
    // 255 is full speed:
    motor.setSpeed(200);    
    
    // set the speed to 200 of 255 of "motor2":
    motor2.setSpeed(200);  

    // set the speed to 200 of 255 of "motor3":
    motor3.setSpeed(200);  
   
   //pinMode(ledPin, OUTPUT);   
}

void loop() {
    readSerialString(); 
    if (values[0] ==NULL){  
        long intervalon = 0;           // interval at which to blink (milliseconds)
        long intervaloff = 0;
        
        //Intervall für zweiten Motor
        long intervalon2 = 0;           // interval at which to blink (milliseconds)
        long intervaloff2 = 0;
        
        //Intervall für zweiten Motor
        long intervalon3 = 0;           // interval at which to blink (milliseconds)
        long intervaloff3 = 0;      
        
    }
  else {  
          // the follow variables is a long because the time, measured in miliseconds,
        // will quickly become a bigger number than can be stored in an int.
        long intervalon = values[1] * 1000;           // interval at which to blink (milliseconds)
        long intervaloff = values[2] * 1000;
        
        //Intervall für zweiten Motor
        long intervalon2 = values[3] * 1000;           // interval at which to blink (milliseconds)
        long intervaloff2 = values[4] * 1000;
        
        //Intervall für zweiten Motor
        long intervalon3 = values[5] * 1000;           // interval at which to blink (milliseconds)
        long intervaloff3 = values[6] * 1000;         
     
  }

     unsigned long currentMillis = millis();
  
    if(ledState == HIGH && currentMillis - previousMillis > intervalon ) {
       previousMillis = currentMillis;  
       ledState = LOW;
       motor.run(FORWARD);
       //digitalWrite(ledPin, ledState);
    }
     if(ledState == LOW && currentMillis - previousMillis > intervaloff) {
       previousMillis = currentMillis;  
       ledState = HIGH;
       motor.run(RELEASE);
       //digitalWrite(ledPin, ledState);
    }
    
    unsigned long currentMillis2 = millis();
    if(ledState2 == HIGH && currentMillis2 - previousMillis2 > intervalon2 ) {
       previousMillis2 = currentMillis2;  
       ledState2 = LOW;
       motor2.run(FORWARD);
       //digitalWrite(ledPin, ledState);
    }
     if(ledState2 == LOW && currentMillis2 - previousMillis2 > intervaloff2) {
       previousMillis2 = currentMillis;  
       ledState2 = HIGH;
       motor2.run(RELEASE);
       //digitalWrite(ledPin, ledState);
    }  
    
    unsigned long currentMillis3 = millis();
    if(ledState3 == HIGH && currentMillis3 - previousMillis3 > intervalon3 ) {
       previousMillis3 = currentMillis3;  
       ledState3 = LOW;
       motor3.run(FORWARD);
       //digitalWrite(ledPin, ledState);
    }
     if(ledState3 == LOW && currentMillis3 - previousMillis3 > intervaloff3) {
       previousMillis3 = currentMillis;  
       ledState3 = HIGH;
       motor3.run(RELEASE);
       //digitalWrite(ledPin, ledState);
    }      
    
}

Mach globale Variablen draus

Lokale Variablen existieren nur in ihrem { } Block

Serenifly:
Mach globale Variablen draus

Lokale Variablen existieren nur in ihrem { } Block

Also in diesem Fall nur im IF{} bzw ELSE{} Teil aber nicht in loop() Funktion außerhalb if{} bzw ELSE{}.

Definiere die Variablen außerhalb von loop{} als unsigned long.

Grüße Uwe

Hallo

danke für den Hinweis.
Ich habe die Variablen außerhalb von Loop als unsigned long definiert.

Ziel ist es, dass die Motoren erst starten, wenn ich die Werte per Bluetooth übermittle.
Momentan starten die Motoren sofort, sobald diese an das Stromnetz angeschlossen werden.

#include <AFMotor.h>

//////////////////////////////////
const int NUMBER_OF_FIELDS = 6; // Wie viele kommaseparierte Felder erwarten wir?
int fieldIndex = 0;             // Das aktuell empfangene Feld
int values[NUMBER_OF_FIELDS];   // Array mit den Werte aller Felder
void readSerialString () {
   if( Serial.available()) {
    for(fieldIndex = 0; fieldIndex  < NUMBER_OF_FIELDS; fieldIndex ++)
    {
      values[fieldIndex] = Serial.parseInt(); // Numerischen Wert einlesen

    }
    Serial.print( fieldIndex);
    Serial.println(" Felder empfangen:");
    for(int i=0; i <  fieldIndex; i++)
    {
       Serial.println(values[i]);
    }
    //fieldIndex = 0;  // und von vorn anfangen
  } 
}


// this created our first motor (called "motor") on port 3 of the motorshield:
AF_DCMotor motor(1, MOTOR12_64KHZ);  

// we do it again, this time our second motor will be called "motor2" on port
AF_DCMotor motor2(2, MOTOR12_64KHZ);

// we do it again, this time our second motor will be called "motor2" on port
AF_DCMotor motor3(3, MOTOR12_64KHZ);

// so now we have 2 motors called "motor" and "motor2"
// Variables will change:
int ledState = LOW;             // ledState used to set the LED
int ledState2 = LOW;             // ledState used to set the LED
int ledState3 = LOW;             // ledState used to set the LED

long previousMillis = 0;        // will store last time LED was updated
long previousMillis2 = 0;        // will store last time LED was updated
long previousMillis3 = 0;        // will store last time LED was updated





void setup() {
  
    Serial.begin(9600);           // set up Serial library at 9600 bps
    Serial.println("Motor test!");
    
    // set the speed to 200 of 255 of "motor".  Note that 0 is stop,
    // 255 is full speed:
    motor.setSpeed(200);    
    
    // set the speed to 200 of 255 of "motor2":
    motor2.setSpeed(200);  

    // set the speed to 200 of 255 of "motor3":
    motor3.setSpeed(200);  
   
   //pinMode(ledPin, OUTPUT);   
}
 
        unsigned long intervalon = 0;           // interval at which to blink (milliseconds)
        unsigned long intervaloff = 0;
        
        //Intervall für zweiten Motor
        unsigned long intervalon2 = 0;           // interval at which to blink (milliseconds)
        unsigned long intervaloff2 = 0;
        
        //Intervall für zweiten Motor
        unsigned long intervalon3 = 0;           // interval at which to blink (milliseconds)
        unsigned long intervaloff3 = 0;      
        
    
void loop() {
    readSerialString(); 
  if (values[0] !=NULL){  
          // the follow variables is a long because the time, measured in miliseconds,
        // will quickly become a bigger number than can be stored in an int.
        long intervalon = values[1] * 1000;           // interval at which to blink (milliseconds)
        long intervaloff = values[2] * 1000;
        
        //Intervall für zweiten Motor
        long intervalon2 = values[3] * 1000;           // interval at which to blink (milliseconds)
        long intervaloff2 = values[4] * 1000;
        
        //Intervall für zweiten Motor
        long intervalon3 = values[5] * 1000;           // interval at which to blink (milliseconds)
        long intervaloff3 = values[6] * 1000;         
     
  }

     unsigned long currentMillis = millis();
  
    if(ledState == HIGH && currentMillis - previousMillis > intervalon ) {
       previousMillis = currentMillis;  
       ledState = LOW;
       motor.run(FORWARD);
       //digitalWrite(ledPin, ledState);
    }
     if(ledState == LOW && currentMillis - previousMillis > intervaloff) {
       previousMillis = currentMillis;  
       ledState = HIGH;
       motor.run(RELEASE);
       //digitalWrite(ledPin, ledState);
    }
    
    unsigned long currentMillis2 = millis();
    if(ledState2 == HIGH && currentMillis2 - previousMillis2 > intervalon2 ) {
       previousMillis2 = currentMillis2;  
       ledState2 = LOW;
       motor2.run(FORWARD);
       //digitalWrite(ledPin, ledState);
    }
     if(ledState2 == LOW && currentMillis2 - previousMillis2 > intervaloff2) {
       previousMillis2 = currentMillis;  
       ledState2 = HIGH;
       motor2.run(RELEASE);
       //digitalWrite(ledPin, ledState);
    }  
    
    unsigned long currentMillis3 = millis();
    if(ledState3 == HIGH && currentMillis3 - previousMillis3 > intervalon3 ) {
       previousMillis3 = currentMillis3;  
       ledState3 = LOW;
       motor3.run(FORWARD);
       //digitalWrite(ledPin, ledState);
    }
     if(ledState3 == LOW && currentMillis3 - previousMillis3 > intervaloff3) {
       previousMillis3 = currentMillis;  
       ledState3 = HIGH;
       motor3.run(RELEASE);
       //digitalWrite(ledPin, ledState);
    }      
    
}

Und dann natürlich die lokalen Definitionen drin gelassen, so dass diese die globalen Variablen überdecken

Ungetesteter Vorschlag:

if ((intervalon > 0) && (intervaloff > 0) && (intervalon2 > 0) && (intervaloff2 > 0) && (intervalon3 > 0) && (intervaloff3 > 0)) {
 // normale Aktion
} else {
 // RELEASE für alle Motoren
}

Oder einen zusätzlichen bool für alles. Ist am Anfang false und wird true gesetzt wenn was gültiges empfangen wurde. Dann kann man auch mit einer Zuweisung die Motoren abschalten.