'MODE' was not declared in this scope

algemene C code
Berichten: 6
Geregistreerd: 18 Feb 2018, 21:20

'MODE' was not declared in this scope

Berichtdoor Haytema » 18 Feb 2018, 21:27

Hallo,

Wanneer ik de code wil uploaden naar mijn Arduino Uno krijg ik de volgende vervelende error:
Arduino: 1.8.5 (Windows 10), Board:"Arduino/Genuino Uno"

C:\Users\gebruiker\Documents\Arduino\light_oh_ino_ino\light_oh_ino_ino.ino: In function 'void loop()':

light_oh_ino_ino:34: error: 'MODE' was not declared in this scope

while (MODE < 2) {

^

light_oh_ino_ino:39: error: 'else' without a previous 'if'

} else {

^

light_oh_ino_ino:40: error: 'MODE' was not declared in this scope

MODE = 0;

^

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


En hier de code:
Code: Alles selecteren
#include <Adafruit_NeoPixel.h>

#define BUTTON_PIN 9 // Button pin
#define MIC_PIN A0 // Microphone pin
#define LED_PIN 3 // NeoPixel LED strip pin
#define N_PIXELS 30 // Number of pixels in LED strip

Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_PIXELS, LED_PIN, NEO_GRB + NEO_KHZ800); //Neopixel strip declaration

int NSAMPLES = 30; //Number of samples the microphone takes
int samples = 0; //Samples initialization and starting value
int noise = 0; //Noise initialization and starting value
int RED = 0; //Red color initialization and starting value
int GREEN = 0; //Green color initialization and starting value
int BLUE = 0; //Blue color initialization and starting value

int buttonState = 0;


// the setup routine runs once when you press reset:
void setup() {
  Serial.begin(9600); // Initialize serial communication at 9600 bits per second
  strip.begin(); // Initialize LED strip
  strip.show(); // Initialize all pixels to 'off'

  samples = NSAMPLES; //Declare value of "samples"
  pinMode(BUTTON_PIN, INPUT);

}

// the loop routine runs over and over again forever:
void loop() {
  buttonState = digitalRead(BUTTON_PIN);
  while (MODE < 2) {
    if (buttonState == LOW) {
      // turn LED on:
      MODE = MODE + 1;
    }
  } else {
    MODE = 0;
  }

  //Process to get the "noise" value according to the taken samples:
  if (samples > 0) {
    noise = noise + abs(analogRead(A0) + 22);
    samples = samples - 1;
  } else {
    noise = noise / NSAMPLES;
    samples = NSAMPLES;

    Serial.println(noise); //Noise value visualization in order to calibrate the microphone

    updatePixels(noise, RED, GREEN, BLUE); //Call to updatePixels function
    delay(20); //Delay of 20 miliseconds between pixels illumination

  }

  //Color declaration depending on the noise value. This is useful to change pixels colors in an easy way:
  if (noise < 570) {
    RED = 0;
    GREEN = 50;
    BLUE = 0;
  }
  if (noise < 550) {
    RED = 50;
    GREEN = 50;
    BLUE = 0;
  }
  if (noise < 540) {
    RED = 50;
    GREEN = 0;
    BLUE = 0;
  }
}

//Pixel control function. It makes pixels illuminate depending on the noise value (yes, it's a bit long):
void updatePixels(int noise, int RED, int GREEN, int BLUE) {
  if (noise < 566) {
    strip.setPixelColor(0, RED, GREEN, BLUE); // Meaning: (number of pixel to illuminate, red color value, green color value, blue color value)
  } else {
    strip.setPixelColor(0, 0, 0, 0); // And finally this sets all pixels to off if there's no noise in the room
  }
  if (noise < 565.5) {
    strip.setPixelColor(1, RED, GREEN, BLUE);
    strip.setPixelColor(2, RED, GREEN, BLUE);

  } else {
    strip.setPixelColor(1, 0, 0, 0);
    strip.setPixelColor(2, 0, 0, 0);
  }
  if (noise < 565) {
    strip.setPixelColor(3, RED, GREEN, BLUE);
    strip.setPixelColor(4, RED, GREEN, BLUE);

  } else {
    strip.setPixelColor(3, 0, 0, 0);
    strip.setPixelColor(4, 0, 0, 0);

  }
  if (noise < 564.5) {
    strip.setPixelColor(5, RED, GREEN, BLUE);
    strip.setPixelColor(6, RED, GREEN, BLUE);

  } else {
    strip.setPixelColor(5, 0, 0, 0);
    strip.setPixelColor(6, 0, 0, 0);

  }
  if (noise < 564) {
    strip.setPixelColor(7, RED, GREEN, BLUE);
    strip.setPixelColor(8, RED, GREEN, BLUE);

  } else {
    strip.setPixelColor(7, 0, 0, 0);
    strip.setPixelColor(8, 0, 0, 0);

  }
  if (noise < 563.5) {
    strip.setPixelColor(9, RED, GREEN, BLUE);
    strip.setPixelColor(10, RED, GREEN, BLUE);

  } else {
    strip.setPixelColor(9, 0, 0, 0);
    strip.setPixelColor(10, 0, 0, 0);

  }
  if (noise < 563) {
    strip.setPixelColor(11, RED, GREEN, BLUE);
    strip.setPixelColor(12, RED, GREEN, BLUE);

  } else {
    strip.setPixelColor(11, 0, 0, 0);
    strip.setPixelColor(12, 0, 0, 0);

  }
  if (noise < 562.5) {
    strip.setPixelColor(13, RED, GREEN, BLUE);
    strip.setPixelColor(14, RED, GREEN, BLUE);

  } else {
    strip.setPixelColor(13, 0, 0, 0);
    strip.setPixelColor(14, 0, 0, 0);

  }
  if (noise < 562) {
    strip.setPixelColor(15, RED, GREEN, BLUE);
    strip.setPixelColor(16, RED, GREEN, BLUE);

  } else {
    strip.setPixelColor(15, 0, 0, 0);
    strip.setPixelColor(16, 0, 0, 0);

  }
  if (noise < 561.5) {
    strip.setPixelColor(17, RED, GREEN, BLUE);
    strip.setPixelColor(18, RED, GREEN, BLUE);

  } else {
    strip.setPixelColor(17, 0, 0, 0);
    strip.setPixelColor(18, 0, 0, 0);

  }
  if (noise < 561) {
    strip.setPixelColor(19, RED, GREEN, BLUE);
    strip.setPixelColor(20, RED, GREEN, BLUE);

  } else {
    strip.setPixelColor(19, 0, 0, 0);
    strip.setPixelColor(20, 0, 0, 0);

  }
  if (noise < 560.5) {
    strip.setPixelColor(20, RED, GREEN, BLUE);
  } else {
    strip.setPixelColor(20, 0, 0, 0);
  }
  if (noise < 560) {
    strip.setPixelColor(11, RED, GREEN, BLUE);
  } else {
    strip.setPixelColor(11, 0, 0, 0);
  }
  if (noise < 553) {
    strip.setPixelColor(12, RED, GREEN, BLUE);
  } else {
    strip.setPixelColor(12, 0, 0, 0);
  }
  if (noise < 552) {
    strip.setPixelColor(13, RED, GREEN, BLUE);
  } else {
    strip.setPixelColor(13, 0, 0, 0);
  }
  if (noise < 551) {
    strip.setPixelColor(14, RED, GREEN, BLUE);
  } else {
    strip.setPixelColor(14, 0, 0, 0);
  }
  if (noise < 550) {
    strip.setPixelColor(15, RED, GREEN, BLUE);
  } else {
    strip.setPixelColor(15, 0, 0, 0);
  }
  if (noise < 549) {
    strip.setPixelColor(16, RED, GREEN, BLUE);
  } else {
    strip.setPixelColor(16, 0, 0, 0);
  }
  if (noise < 548) {
    strip.setPixelColor(17, RED, GREEN, BLUE);
  } else {
    strip.setPixelColor(17, 0, 0, 0);
  }
  if (noise < 547) {
    strip.setPixelColor(18, RED, GREEN, BLUE);
  } else {
    strip.setPixelColor(18, 0, 0, 0);
  }
  if (noise < 546) {
    strip.setPixelColor(19, RED, GREEN, BLUE);
  } else {
    strip.setPixelColor(19, 0, 0, 0);
  }
  if (noise < 545) {
    strip.setPixelColor(20, RED, GREEN, BLUE);
  } else {
    strip.setPixelColor(20, 0, 0, 0);
  }
  if (noise < 544) {
    strip.setPixelColor(21, RED, GREEN, BLUE);
  } else {
    strip.setPixelColor(21, 0, 0, 0);
  }
  if (noise < 543) {
    strip.setPixelColor(22, RED, GREEN, BLUE);
  } else {
    strip.setPixelColor(22, 0, 0, 0);
  }
  if (noise < 542) {
    strip.setPixelColor(23, RED, GREEN, BLUE);
  } else {
    strip.setPixelColor(23, 0, 0, 0);
  }
  if (noise < 541) {
    strip.setPixelColor(24, RED, GREEN, BLUE);
  } else {
    strip.setPixelColor(24, 0, 0, 0);
  } if (noise < 540) {
    strip.setPixelColor(25, RED, GREEN, BLUE);
  } else {
    strip.setPixelColor(25, 0, 0, 0);
  }
  if (noise < 539) {
    strip.setPixelColor(26, RED, GREEN, BLUE);
  } else {
    strip.setPixelColor(26, 0, 0, 0);
  }
  if (noise < 538) {
    strip.setPixelColor(27, RED, GREEN, BLUE);
  } else {
    strip.setPixelColor(27, 0, 0, 0);
  }
  if (noise < 537) {
    strip.setPixelColor(28, RED, GREEN, BLUE);
  } else {
    strip.setPixelColor(28, 0, 0, 0);
  }
  if (noise < 536) {
    strip.setPixelColor(29, RED, GREEN, BLUE);
  } else {
    strip.setPixelColor(29, 0, 0, 0);
  }
  strip.show();


}


Weet iemand wat er fout gaat en hoe ik dit op kan lossen?

Alvast bedankt,

Advertisement

Gebruikers-avatar
Berichten: 4836
Geregistreerd: 13 Mei 2013, 20:57
Woonplaats: Heemskerk

Re: 'MODE' was not declared in this scope

Berichtdoor nicoverduin » 18 Feb 2018, 22:20

Duidelijk.... waar komt MODE vandaan? Moet je ff declareren heh:)
Docent HBO Software Engineering, Embedded ontwikkelaar & elektronicus
http://www.verelec.nl

Berichten: 6
Geregistreerd: 18 Feb 2018, 21:20

Re: 'MODE' was not declared in this scope

Berichtdoor Haytema » 18 Feb 2018, 22:34

nicoverduin schreef:Duidelijk.... waar komt MODE vandaan? Moet je ff declareren heh:)

Het stukje code komt van deze website: https://create.arduino.cc/projecthub/ma ... -oh-1532ea
Bij diegene werkt het gewoon met deze code.

Gebruikers-avatar
Berichten: 4836
Geregistreerd: 13 Mei 2013, 20:57
Woonplaats: Heemskerk

Re: 'MODE' was not declared in this scope

Berichtdoor nicoverduin » 18 Feb 2018, 22:43

Joh als je het beter weet......
Docent HBO Software Engineering, Embedded ontwikkelaar & elektronicus
http://www.verelec.nl

Berichten: 147
Geregistreerd: 15 Dec 2012, 21:02
Woonplaats: Delfzijl

Re: 'MODE' was not declared in this scope

Berichtdoor FPCUser » 18 Feb 2018, 23:37

a: zie opmerking Nico betreffende MODE declaratie.
b: bovenaan in de loop zie je een else staan met een sluitende accolade daarvoor. Dus } else
De bijbehorende openings accolade staat achter de while: while (MODE < 2) {
While..else is echter GEEN juiste constuctie.
Conclusie: code rammelt!

Gebruikers-avatar
Berichten: 1768
Geregistreerd: 06 Aug 2016, 01:03

Re: 'MODE' was not declared in this scope

Berichtdoor Koepel » 19 Feb 2018, 09:11

Dat gebeurt er wanneer de code op het allerlaatst nog even gewijzigd wordt en vergeten wordt om het te controleren.
Kunnen wij de code fixen ? Wat moet Haytema nu doen ?

Gebruikers-avatar
Berichten: 4836
Geregistreerd: 13 Mei 2013, 20:57
Woonplaats: Heemskerk

Re: 'MODE' was not declared in this scope

Berichtdoor nicoverduin » 19 Feb 2018, 10:09

Het fixen sec kan/moet hj zelf doen. De hamvraag is natuurlijk wat het doel van mode is. Ik ga dat uiteraard niet zoeken omdat ik vind dat mensen er zelf energie in moeten steken als ze al code van een ander gebruiken. Zoals al vaker aangegeven... ik wil best iemand leren vissen. Als ze alleen maar komen om vis te consumeren, dan moet je naar de winkel gaan.
Docent HBO Software Engineering, Embedded ontwikkelaar & elektronicus
http://www.verelec.nl

Berichten: 6
Geregistreerd: 18 Feb 2018, 21:20

Re: 'MODE' was not declared in this scope

Berichtdoor Haytema » 19 Feb 2018, 18:49

Het fixen en alles wil ik zelf best doen, het is alleen een beetje lastig als je vrij weinig verstand hebt van C.
Ik heb het hele project verder in elkaar, heb de code gewoon kant en klaar van internet gehaald, en dit werkt bij diegene wel. Ik kom er dus nu achter dat die code helemaal niet klopt?
Kan iemand een beetje uitleg geven wat ik nu het beste kan doen.

Alvast bedankt,

Berichten: 147
Geregistreerd: 15 Dec 2012, 21:02
Woonplaats: Delfzijl

Re: 'MODE' was not declared in this scope

Berichtdoor FPCUser » 19 Feb 2018, 20:27

Betreft MODE.
Ik heb het idee dat de makers met de drukknop de LED balk in- en uit wilden schakelen.
Een keer drukken, licht aan, nogmaals drukken, licht uit. Een toggle schakelaar dus.
Maar omdat de waarde van MODE verderop in het geheel niet wordt gebruikt heb ik deze er uit
gelaten. Het programma compileert nu tenminste.

Wat opvalt is dat er een een #define MIC_PIN A0 staat, terwijl later bij het afvragen
noise = noise + abs(analogRead(A0) + 22) staat.
Dan was de #define ook niet nodig geweest.

Of het programma werkt met de volgende code weet ik niet, maar het compileert wel.
Proberen maar.

csharp code
//Light-OH Arduino code
//The used microphone substracts values to the noise when sound increases, so the more sound it gets, the smaller the noise

#include <Adafruit_NeoPixel.h> //Neopixel library loading

#define BUTTON_PIN 9 // Button pin
#define MIC_PIN A0 // Microphone pin
#define LED_PIN 3 // NeoPixel LED strip pin
#define N_PIXELS 30 // Number of pixels in LED strip

Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_PIXELS, LED_PIN, NEO_GRB + NEO_KHZ800); //Neopixel strip declaration

int NSAMPLES = 30; //Number of samples the microphone takes
int samples = 0; //Samples initialization and starting value
int noise = 0; //Noise initialization and starting value
int RED = 0; //Red color initialization and starting value
int GREEN = 0; //Green color initialization and starting value
int BLUE = 0; //Blue color initialization and starting value

int buttonState = 0;


// the setup routine runs once when you press reset:
void setup() {
Serial.begin(9600); // Initialize serial communication at 9600 bits per second
strip.begin(); // Initialize LED strip
strip.show(); // Initialize all pixels to 'off'

samples = NSAMPLES; //Declare value of "samples"
pinMode(BUTTON_PIN, INPUT);

}

// the loop routine runs over and over again forever:
void loop() {
/*
Omdat er verderop niet gekeken wordt naar de waarde van MODE,
dit gedeelte er uitgehaald.

buttonState = digitalRead(BUTTON_PIN);
byte MODE;
while (MODE < 2) {
if (buttonState == LOW) {
// turn LED on:
MODE = MODE + 1;
}
} else {
MODE = 0;
}
*/

//Process to get the "noise" value according to the taken samples:
if (samples > 0) {
noise = noise + abs(analogRead(A0) + 22);
samples = samples - 1;
} else {
noise = noise / NSAMPLES;
samples = NSAMPLES;
Serial.println(noise); //Noise value visualization in order to calibrate the microphone
updatePixels(noise, RED, GREEN, BLUE); //Call to updatePixels function
delay(20); //Delay of 20 miliseconds between pixels illumination
}

//Color declaration depending on the noise value. This is useful to change pixels colors in an easy way:
if (noise < 570) {
RED = 0;
GREEN = 50;
BLUE = 0;
}
if (noise < 550) {
RED = 50;
GREEN = 50;
BLUE = 0;
}
if (noise < 540) {
RED = 50;
GREEN = 0;
BLUE = 0;
}
}


//Pixel control function. It makes pixels illuminate depending on the noise value (yes, it's a bit long):
void updatePixels(int noise, int RED, int GREEN, int BLUE) {
if (noise < 566) {
strip.setPixelColor(0, RED, GREEN, BLUE); // Meaning: (number of pixel to illuminate, red color value, green color value, blue color value)
} else {
strip.setPixelColor(0, 0, 0, 0); // And finally this sets all pixels to off if there's no noise in the room
}

if (noise < 565.5) {
strip.setPixelColor(1, RED, GREEN, BLUE);
strip.setPixelColor(2, RED, GREEN, BLUE);
} else {
strip.setPixelColor(1, 0, 0, 0);
strip.setPixelColor(2, 0, 0, 0);
}

if (noise < 565) {
strip.setPixelColor(3, RED, GREEN, BLUE);
strip.setPixelColor(4, RED, GREEN, BLUE);
} else {
strip.setPixelColor(3, 0, 0, 0);
strip.setPixelColor(4, 0, 0, 0);

}
if (noise < 564.5) {
strip.setPixelColor(5, RED, GREEN, BLUE);
strip.setPixelColor(6, RED, GREEN, BLUE);
} else {
strip.setPixelColor(5, 0, 0, 0);
strip.setPixelColor(6, 0, 0, 0);
}

if (noise < 564) {
strip.setPixelColor(7, RED, GREEN, BLUE);
strip.setPixelColor(8, RED, GREEN, BLUE);
} else {
strip.setPixelColor(7, 0, 0, 0);
strip.setPixelColor(8, 0, 0, 0);
}

if (noise < 563.5) {
strip.setPixelColor(9, RED, GREEN, BLUE);
strip.setPixelColor(10, RED, GREEN, BLUE);
} else {
strip.setPixelColor(9, 0, 0, 0);
strip.setPixelColor(10, 0, 0, 0);
}

if (noise < 563) {
strip.setPixelColor(11, RED, GREEN, BLUE);
strip.setPixelColor(12, RED, GREEN, BLUE);
} else {
strip.setPixelColor(11, 0, 0, 0);
strip.setPixelColor(12, 0, 0, 0);
}

if (noise < 562.5) {
strip.setPixelColor(13, RED, GREEN, BLUE);
strip.setPixelColor(14, RED, GREEN, BLUE);
} else {
strip.setPixelColor(13, 0, 0, 0);
strip.setPixelColor(14, 0, 0, 0);
}

if (noise < 562) {
strip.setPixelColor(15, RED, GREEN, BLUE);
strip.setPixelColor(16, RED, GREEN, BLUE);
} else {
strip.setPixelColor(15, 0, 0, 0);
strip.setPixelColor(16, 0, 0, 0);
}

if (noise < 561.5) {
strip.setPixelColor(17, RED, GREEN, BLUE);
strip.setPixelColor(18, RED, GREEN, BLUE);
} else {
strip.setPixelColor(17, 0, 0, 0);
strip.setPixelColor(18, 0, 0, 0);
}

if (noise < 561) {
strip.setPixelColor(19, RED, GREEN, BLUE);
strip.setPixelColor(20, RED, GREEN, BLUE);
} else {
strip.setPixelColor(19, 0, 0, 0);
strip.setPixelColor(20, 0, 0, 0);
}

if (noise < 560.5) {
strip.setPixelColor(20, RED, GREEN, BLUE);
} else {
strip.setPixelColor(20, 0, 0, 0);
}

if (noise < 560) {
strip.setPixelColor(11, RED, GREEN, BLUE);
} else {
strip.setPixelColor(11, 0, 0, 0);
}

if (noise < 553) {
strip.setPixelColor(12, RED, GREEN, BLUE);
} else {
strip.setPixelColor(12, 0, 0, 0);
}

if (noise < 552) {
strip.setPixelColor(13, RED, GREEN, BLUE);
} else {
strip.setPixelColor(13, 0, 0, 0);
}

if (noise < 551) {
strip.setPixelColor(14, RED, GREEN, BLUE);
} else {
strip.setPixelColor(14, 0, 0, 0);
}

if (noise < 550) {
strip.setPixelColor(15, RED, GREEN, BLUE);
} else {
strip.setPixelColor(15, 0, 0, 0);
}

if (noise < 549) {
strip.setPixelColor(16, RED, GREEN, BLUE);
} else {
strip.setPixelColor(16, 0, 0, 0);
}

if (noise < 548) {
strip.setPixelColor(17, RED, GREEN, BLUE);
} else {
strip.setPixelColor(17, 0, 0, 0);
}

if (noise < 547) {
strip.setPixelColor(18, RED, GREEN, BLUE);
} else {
strip.setPixelColor(18, 0, 0, 0);
}

if (noise < 546) {
strip.setPixelColor(19, RED, GREEN, BLUE);
} else {
strip.setPixelColor(19, 0, 0, 0);
}

if (noise < 545) {
strip.setPixelColor(20, RED, GREEN, BLUE);
} else {
strip.setPixelColor(20, 0, 0, 0);
}

if (noise < 544) {
strip.setPixelColor(21, RED, GREEN, BLUE);
} else {
strip.setPixelColor(21, 0, 0, 0);
}

if (noise < 543) {
strip.setPixelColor(22, RED, GREEN, BLUE);
} else {
strip.setPixelColor(22, 0, 0, 0);
}

if (noise < 542) {
strip.setPixelColor(23, RED, GREEN, BLUE);
} else {
strip.setPixelColor(23, 0, 0, 0);
}

if (noise < 541) {
strip.setPixelColor(24, RED, GREEN, BLUE);
} else {
strip.setPixelColor(24, 0, 0, 0);
}

if (noise < 540) {
strip.setPixelColor(25, RED, GREEN, BLUE);
} else {
strip.setPixelColor(25, 0, 0, 0);
}

if (noise < 539) {
strip.setPixelColor(26, RED, GREEN, BLUE);
} else {
strip.setPixelColor(26, 0, 0, 0);
}

if (noise < 538) {
strip.setPixelColor(27, RED, GREEN, BLUE);
} else {
strip.setPixelColor(27, 0, 0, 0);
}

if (noise < 537) {
strip.setPixelColor(28, RED, GREEN, BLUE);
} else {
strip.setPixelColor(28, 0, 0, 0);
}

if (noise < 536) {
strip.setPixelColor(29, RED, GREEN, BLUE);
} else {
strip.setPixelColor(29, 0, 0, 0);
}

strip.show();
}

Berichten: 2389
Geregistreerd: 16 Okt 2013, 14:31
Woonplaats: s hertogenbosch

Re: 'MODE' was not declared in this scope

Berichtdoor shooter » 19 Feb 2018, 21:08

biujna bovenaan staan een paar int regels, zet daar ook mode bij dus INT Mode=0;
gewoon de fout lezen en erop reageren, het is heel normaal als je een stel foutmeldingen krijgt hoor, niet in paniek raken. en zeker niet verwachten dat een programma klopt, want zelfs bij het overcopieren kunnen fouten ontstaan zoals haakjes en punten.
paul deelen
shooter@home.nl

Volgende

Terug naar C code

Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 1 gast