Error bei Aufrufen von Array

Hallo Gemeinde

Ich kriege einen Error bei der Funktion "FlashArea". Hier soll die entsprechende Stelle des Led-Array anhand der zuvor übergebenen Variable i (Area) aufgerufen werden.
Weiss wer, was ich falsch mache? Danke!

#include <Keyboard.h>




void printDetail(uint8_t type, int value);

// Digital IO's
int IRreceive[]            = {3, 4, 5, 6};   // Sensor Array, starting upper right, going clockwise
byte Led[]                 = {7, 8, 9, 10};  // Led Array, starting upper right, going clockwise
int Piezo                  = 11; // Buzzer


int Area                   = 0; //





// Code Variables
int timeOut                = 0;      // Deffined in frequencyCalculations (IRpulse + 50)
int FIRE                   = 0;      // 0 = don't fire, 1 = Primary Fire, 2 = Secondary Fire
int TR                     = 0;      // Trigger Reading
int LTR                    = 0;      // Last Trigger Reading
int T2R                    = 0;      // Trigger 2 Reading (For secondary fire)
int LT2R                   = 0;      // Last Trigger 2 Reading (For secondary fire)

// Signal Properties
int IRpulse                = 600;    // Basic pulse duration of 600uS MilesTag standard 4*IRpulse for header bit, 2*IRpulse for 1, 1*IRpulse for 0.
int IRfrequency            = 36;     // Frequency in kHz Standard values are: 38kHz, 40kHz. Choose dependant on your receiver characteristics
int IRt                    = 0;      // LED on time to give correct transmission frequency, calculated in setup.
int IRpulses               = 0;      // Number of oscillations needed to make a full IRpulse, calculated in setup.
int header                 = 4;      // Header lenght in pulses. 4 = Miles tag standard
int maxSPS                 = 10;     // Maximum Shots Per Seconds. Not yet used.


//Incoming signal Details
int received[18];                    // Received data: received[0] = which sensor, received[1] - [17] byte1 byte2 parity (Miles Tag structure)
int check                  = 0;      // Variable used in parity checking




void setup() {
  // Serial coms set up to help with debugging.
  Serial.begin(9600);         
  Serial.println("Startup...");


  
  // Pin declarations
  pinMode(IRreceive[3, 4, 5, 6], INPUT);

  pinMode(Led[0, 1, 2, 3], OUTPUT);
  pinMode(Piezo, OUTPUT);

}


// Main loop most of the code is in the sub routines
void loop() {

  Area = 10;

  if(digitalRead(IRreceive[0]) == LOW){    // If the receive pin is low a signal is being received.
    Area = 0;
    digitalWrite(Led[0],HIGH);
    receiveIR(Area);
    FlashArea(Area);
    
  }
    
  else  if(digitalRead(IRreceive[1]) == LOW){ 
    Area = 1;
    digitalWrite(Led[1],HIGH);
    receiveIR(Area);
    FlashArea(Area);
    
  }

  else if(digitalRead(IRreceive[2]) == LOW){
    Area = 2;
    digitalWrite(Led[2],HIGH);
    receiveIR(Area);
    FlashArea(Area);
    
  }

  else if(digitalRead(IRreceive[3]) == LOW){
    Area = 3;
    digitalWrite(Led[3],HIGH);
    receiveIR(Area);
    FlashArea(Area);
    
  }
 }


  

// SUB ROUTINES

void playTone(int tone, int duration) { // A sub routine for playing tones like the standard arduino melody example
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(Piezo, HIGH);
    delayMicroseconds(tone);
    digitalWrite(Piezo, LOW);
    delayMicroseconds(tone);
  }
}

void FlashArea(int i) {
  
   for (int i=0; i<10; i++) {
   playTone(500, 500);
   digitalWrite(Led[i]), HIGH);
   delay (500);
   digitalWrite(Led[i], LOW);
   delay (500);
}
}

Hi

Also einen Error bekomme ich auch - Der lautet aber, daß receicIR() nicht bekannt ist.
Auch dürfte die übergebene Variable zu Deiner Flash-Funktion nicht viel machen, da Du innerhalb Diese direkt mit einer lokalen Variable überdeckst.

MfG

sorry, hier der code mit der richtigen variable (nicht "i"):

#include <Keyboard.h>




void printDetail(uint8_t type, int value);

// Digital IO's
int IRreceive[]            = {3, 4, 5, 6};   // Sensor Array, starting upper right, going clockwise
byte Led[]                 = {7, 8, 9, 10};  // Led Array, starting upper right, going clockwise
int Piezo                  = 11; // Buzzer


int Area                   = 0; //





// Code Variables
int timeOut                = 0;      // Deffined in frequencyCalculations (IRpulse + 50)
int FIRE                   = 0;      // 0 = don't fire, 1 = Primary Fire, 2 = Secondary Fire
int TR                     = 0;      // Trigger Reading
int LTR                    = 0;      // Last Trigger Reading
int T2R                    = 0;      // Trigger 2 Reading (For secondary fire)
int LT2R                   = 0;      // Last Trigger 2 Reading (For secondary fire)

// Signal Properties
int IRpulse                = 600;    // Basic pulse duration of 600uS MilesTag standard 4*IRpulse for header bit, 2*IRpulse for 1, 1*IRpulse for 0.
int IRfrequency            = 36;     // Frequency in kHz Standard values are: 38kHz, 40kHz. Choose dependant on your receiver characteristics
int IRt                    = 0;      // LED on time to give correct transmission frequency, calculated in setup.
int IRpulses               = 0;      // Number of oscillations needed to make a full IRpulse, calculated in setup.
int header                 = 4;      // Header lenght in pulses. 4 = Miles tag standard
int maxSPS                 = 10;     // Maximum Shots Per Seconds. Not yet used.


//Incoming signal Details
int received[18];                    // Received data: received[0] = which sensor, received[1] - [17] byte1 byte2 parity (Miles Tag structure)
int check                  = 0;      // Variable used in parity checking




void setup() {
  // Serial coms set up to help with debugging.
  Serial.begin(9600);         
  Serial.println("Startup...");


  
  // Pin declarations
  pinMode(IRreceive[3, 4, 5, 6], INPUT);

  pinMode(Led[0, 1, 2, 3], OUTPUT);
  pinMode(Piezo, OUTPUT);

}


// Main loop most of the code is in the sub routines
void loop() {

  Area = 10;

  if(digitalRead(IRreceive[0]) == LOW){    // If the receive pin is low a signal is being received.
    Area = 0;
    digitalWrite(Led[0],HIGH);
    receiveIR(Area);
    FlashArea(Area);
    
  }
    
  else  if(digitalRead(IRreceive[1]) == LOW){ 
    Area = 1;
    digitalWrite(Led[1],HIGH);
    receiveIR(Area);
    FlashArea(Area);
    
  }

  else if(digitalRead(IRreceive[2]) == LOW){
    Area = 2;
    digitalWrite(Led[2],HIGH);
    receiveIR(Area);
    FlashArea(Area);
    
  }

  else if(digitalRead(IRreceive[3]) == LOW){
    Area = 3;
    digitalWrite(Led[3],HIGH);
    receiveIR(Area);
    FlashArea(Area);
    
  }
 }


  

// SUB ROUTINES

void playTone(int tone, int duration) { // A sub routine for playing tones like the standard arduino melody example
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(Piezo, HIGH);
    delayMicroseconds(tone);
    digitalWrite(Piezo, LOW);
    delayMicroseconds(tone);
  }
}

void FlashArea(int LED) {
  
   for (int i=0; i<10; i++) {
   playTone(500, 500);
   digitalWrite(Led[LED]), HIGH);
   delay (500);
   digitalWrite(Led[LED], LOW);
   delay (500);
}

laurin123:
Hallo Gemeinde

Ich kriege einen Error bei der Funktion "FlashArea". Hier soll die entsprechende Stelle des Led-Array anhand der zuvor übergebenen Variable i (Area) aufgerufen werden.
Weiss wer, was ich falsch mache? Danke!

#include <Keyboard.h>

void printDetail(uint8_t type, int value);

// Digital IO's
int IRreceive[]            = {3, 4, 5, 6};  // Sensor Array, starting upper right, going clockwise
byte Led[]                = {7, 8, 9, 10};  // Led Array, starting upper right, going clockwise
int Piezo                  = 11; // Buzzer

int Area                  = 0; //

// Code Variables
int timeOut                = 0;      // Deffined in frequencyCalculations (IRpulse + 50)
int FIRE                  = 0;      // 0 = don't fire, 1 = Primary Fire, 2 = Secondary Fire
int TR                    = 0;      // Trigger Reading
int LTR                    = 0;      // Last Trigger Reading
int T2R                    = 0;      // Trigger 2 Reading (For secondary fire)
int LT2R                  = 0;      // Last Trigger 2 Reading (For secondary fire)

// Signal Properties
int IRpulse                = 600;    // Basic pulse duration of 600uS MilesTag standard 4IRpulse for header bit, 2IRpulse for 1, 1*IRpulse for 0.
int IRfrequency            = 36;    // Frequency in kHz Standard values are: 38kHz, 40kHz. Choose dependant on your receiver characteristics
int IRt                    = 0;      // LED on time to give correct transmission frequency, calculated in setup.
int IRpulses              = 0;      // Number of oscillations needed to make a full IRpulse, calculated in setup.
int header                = 4;      // Header lenght in pulses. 4 = Miles tag standard
int maxSPS                = 10;    // Maximum Shots Per Seconds. Not yet used.

//Incoming signal Details
int received[18];                    // Received data: received[0] = which sensor, received[1] - [17] byte1 byte2 parity (Miles Tag structure)
int check                  = 0;      // Variable used in parity checking

void setup() {
  // Serial coms set up to help with debugging.
  Serial.begin(9600);       
  Serial.println("Startup...");

// Pin declarations
  pinMode(IRreceive[3, 4, 5, 6], INPUT);

pinMode(Led[0, 1, 2, 3], OUTPUT);
  pinMode(Piezo, OUTPUT);

}

// Main loop most of the code is in the sub routines
void loop() {

Area = 10;

if(digitalRead(IRreceive[0]) == LOW){    // If the receive pin is low a signal is being received.
    Area = 0;
    digitalWrite(Led[0],HIGH);
    receiveIR(Area);
    FlashArea(Area);
   
  }
   
  else  if(digitalRead(IRreceive[1]) == LOW){
    Area = 1;
    digitalWrite(Led[1],HIGH);
    receiveIR(Area);
    FlashArea(Area);
   
  }

else if(digitalRead(IRreceive[2]) == LOW){
    Area = 2;
    digitalWrite(Led[2],HIGH);
    receiveIR(Area);
    FlashArea(Area);
   
  }

else if(digitalRead(IRreceive[3]) == LOW){
    Area = 3;
    digitalWrite(Led[3],HIGH);
    receiveIR(Area);
    FlashArea(Area);
   
  }
}

// SUB ROUTINES

void playTone(int tone, int duration) { // A sub routine for playing tones like the standard arduino melody example
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(Piezo, HIGH);
    delayMicroseconds(tone);
    digitalWrite(Piezo, LOW);
    delayMicroseconds(tone);
  }
}

void FlashArea(int i) {
 
  for (int i=0; i<10; i++) {
  playTone(500, 500);
  digitalWrite(Led[i]), HIGH);
  delay (500);
  digitalWrite(Led[i], LOW);
  delay (500);
}
}

  pinMode(IRreceive[3, 4, 5, 6], INPUT);

  pinMode(Led[0, 1, 2, 3], OUTPUT);

Das geht in C++ nicht. Du musst schon über das Array iterieren.

Den Komma Operator gibt es. Wodurch das vielleicht kompiliert. Aber ich nehme mal da kommt eine Warnung

for (int i=0; i<10; i++)

Dein Array hat keine 10 Elemente. Du solltest da die eigentliche Größe nehmen und nicht feste Konstanten

digitalWrite(Led[LED]), HIGH);

Falsche Verwendung von Klammern

Das

pinMode(IRreceive[3, 4, 5, 6], INPUT);
pinMode(Led[0, 1, 2, 3], OUTPUT);

funktioniert nicht.
Das ist keine gültige Funktion/Anweisung.

Um mehrere Pins anzusprechen mußt Du das über ein for() machen.

Grüße Uwe

ach so. danke!

Tipp: "range based for loop"

byte led[]  {7, 8, 9, 10};

void setup() 
{
  for(const byte l : led) pinMode(l, OUTPUT);
}

Danke für eure Inputs!

Nun habe ich schon das nächste Problem. Die 5 if-Abfragen im loop scheinen zu langsam zu sein. An den "IRreceive" hängen IR-Receiver, die ein digitales Signal von einer Länge von ca. 30ms empfangen sollen. Das Signal kommt aber so gut wie nie an.

Kann es sein, dass mein loop zu langsam ist für diese Pulse? Gibt es überhaupt eine schnellere Abfrage als if?

Danke euch!

Hi

Im Normalfall braucht ein loop()-Durchlauf deutlich unter einer Millisekunde - man sollte halt mit delay() sparsam umgehen (oder ganz vermeiden).

Da Deine IRreceive() immer noch geheim ist, mag ich dazu auch nicht viel sagen - wird wohl irgendwo irgendwas irgendwie nicht so toll zusammen laufen.

MfG

Ich habe aktuell kein einziges delay() im loop. Sorry, dass ich den IRreceive() euch vorenthalten habe. Deshalb hier die Funktion:

void receiveIR(int a) { // Void checks for an incoming signal and decodes it if it sees one.
  int error = 0;

   
    while(digitalRead(IRreceive[a]) == LOW);{
    }
    for(int i = 1; i <= 17; i++) {                        // Repeats several times to make sure the whole signal has been received
      received[i] = pulseIn((IRreceive[a]), LOW, 15000);  // pulseIn command waits for a pulse and then records its duration in microseconds.
    }
   
    Serial.print("sensor: ");                            // Prints if it was a head shot or not.
    Serial.print(received[0]); 
    Serial.print("...");
   
    for(int i = 1; i <= 17; i++) {  // Looks at each one of the received pulses
      int receivedTemp[18];
      receivedTemp[i] = 2;
      if(received[i] > (IRpulse - 200) &&  received[i] < (IRpulse + 200)) {receivedTemp[i] = 0;}                      // Works out from the pulse length if it was a data 1 or 0 that was received writes result to receivedTemp string
      if(received[i] > (IRpulse + IRpulse - 200) &&  received[i] < (IRpulse + IRpulse + 200)) {receivedTemp[i] = 1;}  // Works out from the pulse length if it was a data 1 or 0 that was received  
      received[i] = 3;                   // Wipes raw received data
      received[i] = receivedTemp[i];     // Inputs interpreted data
     
      Serial.print(" ");
      Serial.print(received[i]);         // Print interpreted data results
    }
    Serial.println("");                  // New line to tidy up printed results


   
    // Parity Check. Was the data received a valid signal?
    check = 0;
    for(int i = 1; i <= 16; i++) {
      if(received[i] == 1){check = check + 1;}
      if(received[i] == 2){error = 1;}
    }
    check = check >> 0 & B1;
    // Serial.println(check);
    if(check != received[17]){error = 1;}
    if(error == 0){Serial.println("Valid Signal");}
    else{Serial.println("ERROR");}
    if(error == 0){(FlashArea(a));       
  }
}

Hi

Dort sehe ich Dein Problem der langsamen loop()-Zeiten - Du versuchst hier sämtliche Impulse 'am Stück' einzulesen.
Wenn Du dabei nicht beim ersten Impuls begonnen hast, wartest Du Ewigkeiten auf den Letzten - da Dieser eben schon da war, kommt Der aber auch erst wieder in einer kleinen Ewigkeit.

Mache Dir den Spaß und merke Die die Zeit, Die Du für diese Funktion brauchst - ein micros() davor und ein micros() dahinter, Zwischenzeit ausrechnen, ausgeben.

Stichwort Ausgaben - mit 9600 Baus braucht JEDES ZEICHEN eine Millisekunde (9600 Bit pro Sekunde, jedes Zeichen hat 10 Bit, macht 1000 Zeichen pro Sekunde).
Also bei der Ausgabe von 30 Zeichen sind 30 Millisekunden perse futsch.

Denke, hier könnte man einen Interrupt bemühen, die Signale im Hintergrund abzutasten und ein Flag setzen, wenn ein vollständiges Signal erfasst wurde.

MfG

while(digitalRead(IRreceive[a]) == LOW);

Das ist erheblich schlechter als jedes delay, vor dem hier immer gewarnt wird.
Weil gar nicht klar ist, wie lange es dauert. (Möglich ist sogar: forever)

Auch das folgende pulseIn kann bis zum Timeout dauern.

Und deine FlashArea Aufrufe brauchen ebenfalls Zeit.