zerou24
Goto Top

Arduino C++ "A function definition is not allowed here before "Bracekt" token

Ich bin gerade dabei ein programm für meinen arduino zu basteln und "frankensteine" es zusammen.
Das programm soll bei erhalten eines HEX wertes über IR eine loop abspielen die ein bestimmtes farbmuster in der ELD erzeugt abspielen immer und immer wieder (loop)
Problem nun ist wenn ich im ide folgendes eingebe
void loopFade(){
  {
  crossFade(red);
  crossFade(green);
  crossFade(blue);
  crossFade(yellow);

  if (repeat) { // Do we loop a finite number of times?
    j += 1;
    if (j >= repeat) { // Are we there yet?
      exit(j);         // If so, stop.
    }
  }
}
Bekomme ich die oben stehende fehlermeldung: "A function definition is not allowed here before "{" token .
Im eigeltichen programm(es sind 2 modis einmal "fade" und einmal "mood") funktioniert das eins a und er mecker nicht kaum pack ich es in die "veränderte" loop zickt der compiler. :C
Originalcode:
void loop()
{
  crossFade(red);
  crossFade(green);
  crossFade(blue);
  crossFade(yellow);

  if (repeat) { // Do we loop a finite number of times?
    j += 1;
    if (j >= repeat) { // Are we there yet?
      exit(j);         // If so, stop.
    }
  }
}

Content-Key: 325476

Url: https://administrator.de/contentid/325476

Printed on: April 19, 2024 at 12:04 o'clock

Mitglied: 131381
131381 Jan 04, 2017 updated at 21:38:05 (UTC)
Goto Top
Zeile 2 ist eine geschweifte Klammer zu viel, die steht schon in Zeile 1 face-wink

Gruß mik
Member: Zerou24
Zerou24 Jan 04, 2017 at 21:38:21 (UTC)
Goto Top
Behoben gleiches problem andere zeile :C
Member: BassFishFox
BassFishFox Jan 04, 2017 updated at 21:40:25 (UTC)
Goto Top
Hallo,

Die einzige Differenz die ich sehe zwischen Deinen beiden Codes ist ein zusaetzliches { am Ende der ersten Zeile.

void loopFade(){

Ohne den Rest zu kennen. Da fehlt irgendwo ein viertes } oder ein { zuviel.

BFF
Member: Zerou24
Zerou24 Jan 04, 2017 at 21:41:31 (UTC)
Goto Top
Hier der rest des codes für loopFade:
void loopFade() 
 {
  crossFade(red);
  crossFade(green);
  crossFade(blue);
  crossFade(yellow);

  if (repeat) { // Do we loop a finite number of times?
    j += 1;
    if (j >= repeat) { // Are we there yet?
      exit(j);         // If so, stop.
    }
  }
}


int calculateStep(int prevValue, int endValue) {
  int step = endValue - prevValue; // What's the overall gap?  
  if (step) {                      // If its non-zero, 
    step = 1020/step;              //   divide by 1020
  } 
  return step;
}

/* The next function is calculateVal. When the loop value, i,
*  reaches the step size appropriate for one of the
*  colors, it increases or decreases the value of that color by 1. 
*  (R, G, and B are each calculated separately.)
*/

int calculateVal(int step, int val, int i) {

  if ((step) && i % step == 0) { // If step is non-zero and its time to change a value,
    if (step > 0) {              //   increment the value if step is positive...
      val += 1;           
    } 
    else if (step < 0) {         //   ...or decrement it if step is negative
      val -= 1;
    } 
  }
  // Defensive driving: make sure val stays in the range 0-255
  if (val > 255) {
    val = 255;
  } 
  else if (val < 0) {
    val = 0;
  }
  return val;
}

/* crossFade() converts the percentage colors to a 
*  0-255 range, then loops 1020 times, checking to see if  
*  the value needs to be updated each time, then writing
*  the color values to the correct pins.
*/

void crossFade(int color[3]) {
  // Convert to 0-255
  int R = (color * 255) / 100;
  int G = (color[1] * 255) / 100;
  int B = (color[2] * 255) / 100;

  int stepR = calculateStep(prevR, R);
  int stepG = calculateStep(prevG, G); 
  int stepB = calculateStep(prevB, B);

  for (int i = 0; i <= 1020; i++) {
    redVal = calculateVal(stepR, redVal, i);
    grnVal = calculateVal(stepG, grnVal, i);
    bluVal = calculateVal(stepB, bluVal, i);

    analogWrite(redPin, redVal);   // Write current values to LED pins
    analogWrite(grnPin, grnVal);      
    analogWrite(bluPin, bluVal); 

    delay(wait); // Pause for 'wait' milliseconds before resuming the loop  

    if (DEBUG) { // If we want serial output, print it at the 
      if (i == 0 or i % loopCount == 0) { // beginning, and every loopCount times
        Serial.print("Loop/RGB: #");  
        Serial.print(i);
        Serial.print(" | ");  
        Serial.print(redVal);
        Serial.print(" / ");  
        Serial.print(grnVal);
        Serial.print(" / ");    
        Serial.println(bluVal); 
      } 
      DEBUG += 1;
    }
  }
  // Update current values for next loop
  prevR = redVal; 
  prevG = grnVal; 
  prevB = bluVal;
  delay(hold); // Pause for optional 'wait' milliseconds before resuming the loop  
}
 
 // this routine loops indefinitely
void loopMood() {
    for (float x=0;x < PI; x = x + 0.000004){
      redM = brightness * abs(sin(x*(180/PI)));           // calculate red brightness
      greenM = brightness * abs(sin((x+PI/3)*(180/PI)));    // calculate green brightness
      blueM = brightness * abs(sin((x+(2*PI)/3)*(180/PI)));// calculate the blue brightness
      analogWrite(led0, red);   // send the value to the LED
      analogWrite(led1, green);   // send the value to the LED 
      analogWrite(led2, blue);   // send the value to the LED
    }

}
Member: BassFishFox
BassFishFox Jan 04, 2017 updated at 21:52:18 (UTC)
Goto Top
Danke,

Das ist doch von hier?
https://www.arduino.cc/en/Tutorial/ColorCrossfader ?

Zaehle mal genau die { und } durch. Nicht das beim Kopieren irgendwas verschollen ist oder im Original schon etwas fehlt.

BFF
Member: Zerou24
Zerou24 Jan 04, 2017 at 22:46:03 (UTC)
Goto Top
das original gunktioniert ja nur das kopierte nich komischer weise
Member: BassFishFox
BassFishFox Jan 04, 2017, updated at Jan 05, 2017 at 03:14:41 (UTC)
Goto Top
A function definition is not allowed here before "{" token

Zeile 17 :
int calculateStep(int prevValue, int endValue) {

Entferne das Leerzeichen und bringe das { mit einem ENTER eine Zeile tiefer.

int calculateStep(int prevValue, int endValue)
{ 

Das hast Du ein paar mal in Deinem Code. Koennte helfen.

BFF