Random function not recognized, and 2 coding errors

int myDelay = 0;
const int button = A0;        // on off button in anolog pin 0 
int ledflag=0;                   // LED status flag

void setup() {
    pinMode(2,OUTPUT);  // PINS 2 - 12 are output leds, each pin is connected to 1 or 2  led's 
    pinMode(3,OUTPUT);
    pinMode(4,OUTPUT);
    pinMode(5,OUTPUT);
    pinMode(6,OUTPUT);
    pinMode(7,OUTPUT);
    pinMode(8,OUTPUT);
    pinMode(9,OUTPUT);
    pinMode(10,OUTPUT);
    pinMode(11,OUTPUT);
    pinMode(12,OUTPUT);
   pinMode(13,INPUT);
 
 digitalWrite(2,LOW) ;  // all LED's are off to start 
 digitalWrite(3,LOW) ;  
 digitalWrite(4,LOW) ;
 digitalWrite(5,LOW) ;
 digitalWrite(6,LOW) ;
 digitalWrite(7,LOW) ;
 digitalWrite(8,LOW) ;
 digitalWrite(9,LOW) ;
 digitalWrite(10,LOW) ;
 digitalWrite(11,LOW);
digitalWrite(12,LOW);
 digitalWrite(13,LOW);
}

 void loop ()  {

if (digitalRead(button)==HIGH){    // if button is on ( button connected to pin 13 and 12 v pin )
    if (ledflag==0) {             // and the status flag is LOW
      ledflag=1;                  // make status flag HIGH
       
      }                           // 
    else {                        // otherwise...
      ledflag=0;                  // make status flag LOW
        
}}}

if (ledflag =1){         // if led = 1 , button is on, if button is on run loop for random led 


void loop() {
    myDelay= analogRead(A0)      // Tells arduino to read analog pin 0 and store it in the variable called myDelay ( potentiometer connection to on off switch  )
    
    int Led = random ( 2 , 13 ) ;   // pick random number from 2-12, store value in variable called LED
    digitalWrite(Led, HIGH) ;     // turn on the random LED 
    delay(myDelay);               // leave on for # of milliseconds set by potentiometer ( 1-10 dial )
    digitalWrite(myLed,LOW);      // turn off that led
    delay(myDelay) ;              // delay off for amout of time it was on

Oops.

Please remember to use code tags when posting code.

Please post the the error message

  1. the second if statement has an error that says "expected unqualified-id before 'if' "

  2. the second loop said "function definition is not allowed here before '{' token "

  3. The random command isn't recognized, it is just plain text, rand glows orange but i cant use that because had too many variables or something

Please post the error message

if (ledflag =1){ // if led = 1 , button is on, if button is on run loop for random led

The third "if" doesn't appear to be in a function.

You also have two functions called loop.
Oops

ya i figured that the third statement was probably not in a function but wasn't sure how to fix that.

I also did know that you had to name loops.

if I put the second loop line (selecting random leds) above the third if statement and re-named it would that work ?

Post code, not pictures of code.

Post error messages, not pictures of error messages.

You don't name loop functions because there can be only one loop function.

You can call a function just about anything as long as it doesn't start with a number. Best to give it a descriptive name.

<
void loop ()  {

if (digitalRead(button)==HIGH){    // if button is on ( button connected to pin 13 and 12 v pin )
    if (ledflag==0) {             // and the status flag is LOW
      ledflag=1;                  // make status flag HIGH
       
      }                           // 
    else {                        // otherwise...
      ledflag=0;                  // make status flag LOW
        
}}}
  
  if (ledflag =1){         // if led = 1 , button is on, if button is on run loop for random led 
  
    myDelay= analogRead(A0)      // Tells arduino to read analog pin 0 and store it in the variable called myDelay ( potentiometer connection to on off switch  )
    
    int Led = random ( 2 , 13 ) ;   // pick random number from 2-12, store value in variable called LED
    digitalWrite(Led, HIGH) ;     // turn on the random LED 
    delay(myDelay);               // leave on for # of milliseconds set by potentiometer ( 1-10 dial )
    digitalWrite(myLed,LOW);      // turn off that led
    delay(myDelay) ;              // delay off for amout of time it was on  
    
    
} >

error:

error: expected unqualified-id before 'if'

exit status 1

Never, ever do that.

ok, but what can I do about the third if statement, is there another way i should write this ? is placing inside the loop valid ? Thanks for your help

= is assignment, == is equality - getting this wrong is a common error, learn to read the first as "becomes" and the second as "equals".

Auto indent the code to make sure the braces match correctly...

1 Like

It needs to be inside a function.

And '=' is an assignment, '==' is a compare; I doubt that you want to assign :wink:

1 Like

well my power turned off yesterday because of a storm and now my code just says

Compilation error: Error: 2 UNKNOWN: exit status 1

I have selected arduino uno, I have also gone to preferences and selected show verbose output for compile and upload.
still stuck with this error :slightly_frowning_face:

It probably said a bit more than that - try scrolling up.

1 Like
Using board 'uno' from platform in folder: C:\Users\mF\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.3
Using core 'arduino' from platform in folder: C:\Users\m\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.3
Detecting libraries used...
"C:\\Users\\m\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Users\\m\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.3\\cores\\arduino" "-IC:\\Users\\m\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.3\\variants\\standard" "C:\\Users\\MDEPET~1\\AppData\\Local\\Temp\\arduino-sketch-3B79A39C8F78772B6F73D29814459DD4\\sketch\\MF_rand_p.ino.cpp" -o nul
Generating function prototypes...
"C:\\Users\\m\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Users\\m\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.3\\cores\\arduino" "-IC:\\Users\\m\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.3\\variants\\standard" "C:\\Users\\MDEPET~1\\AppData\\Local\\Temp\\arduino-sketch-3B79A39C8F78772B6F73D29814459DD4\\sketch\\MF_rand_p.ino.cpp" -o "C:\\Users\\MDEPET~1\\AppData\\Local\\Temp\\arduino-sketch-3B79A39C8F78772B6F73D29814459DD4\\preproc\\ctags_target_for_gcc_minus_e.cpp"
"C:\\Users\\m\\AppData\\Local\\Arduino15\\packages\\builtin\\tools\\ctags\\5.8-arduino11/ctags" -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives "C:\\Users\\MDEPET~1\\AppData\\Local\\Temp\\arduino-sketch-3B79A39C8F78772B6F73D29814459DD4\\preproc\\ctags_target_for_gcc_minus_e.cpp"
Compiling sketch...
"C:\\Users\\m\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\\Users\\m\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.3\\cores\\arduino" "-IC:\\Users\\m\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.3\\variants\\standard" "C:\\Users\\MDEPET~1\\AppData\\Local\\Temp\\arduino-sketch-3B79A39C8F78772B6F73D29814459DD4\\sketch\\MF_rand_p.ino.cpp" -o "C:\\Users\\MDEPET~1\\AppData\\Local\\Temp\\arduino-sketch-3B79A39C8F78772B6F73D29814459DD4\\sketch\\MF_rand_p.ino.cpp.o"
c:\Users\m\OneDrive - MCCR\Documents\Arduino\MF_rand_p\MF_rand_p.ino: In function 'void setup()':
c:\Users\m\OneDrive - MCCR\Documents\Arduino\MF_rand_p\MF_rand_p.ino:31:15: error: a function-definition is not allowed here before '{' token
  void loop () {
               ^
c:\Users\m\OneDrive - MCCR\Documents\Arduino\MF_rand_p\MF_rand_p.ino:56:5: error: expected '}' at end of input
     }
     ^
Compilation error: Error: 2 UNKNOWN: exit status 1

oh i see what your saying, i have these errors:

  1. a function-definition is not allowed here before '{' token
    void loop () {
  2. expected '}' at end of input
    }
    ^

any idea how i can fix them ? also why is it not coming up in read like before ?

Error messages.without the code they refer to is pretty pointless.

int myDelay = 0;

void setup()
 {
   int myDelay = 0;

void setup()
 {
    pinMode(2,OUTPUT );  // PINS 2 - 11 are output leds, each pin is connected to 3 led's because there are 30 lights and only 14 pins 
    pinMode(3,OUTPUT);
    pinMode(4,OUTPUT);
    pinMode(5,OUTPUT);
    pinMode(6,OUTPUT);
    pinMode(7,OUTPUT);
    pinMode(8,OUTPUT);
    pinMode(9,OUTPUT);
    pinMode(10,OUTPUT);
    pinMode(11,OUTPUT);
   pinMode(12,OUTPUT);
 
 digitalWrite(2,LOW) ;  // all LED's are off to start 
 digitalWrite(3,LOW) ;  
 digitalWrite(4,LOW) ;
 digitalWrite(5,LOW) ;
 digitalWrite(6,LOW) ;
 digitalWrite(7,LOW) ;
 digitalWrite(8,LOW) ;
 digitalWrite(9,LOW) ;
 digitalWrite(10,LOW) ;
 digitalWrite(11,LOW);
digitalWrite(12,LOW);
 
 

 void loop () { 
 
if (digitalRead(button)==HIGH){ // if button is on ( button connected to pin 12 and 12 v pin )
    if (ledflag==0) {             // and the status flag is LOW
      ledflag==1;                  // make status flag HIGH
      digitalWrite(lights,HIGH);     // and turn on the LED
      }                           // 
    else {                        // otherwise...
      ledflag=0;                  // make status flag LOW
      digitalWrite(lights,LOW);      // and turn off the LED

if (digitalRead(lights)==HIGH){    // if pin 13 is on then run led loop for random lights 


    myDelay= analogRead(AO)      // Tells arduino to read analog pin o and store it in the variable called myDelay 
    
    int Led = rand ( 2 , 13 ) ;   // pick random number from 2-12, store value in variable called LED
    digitalWrite(Led, HIGH) ;     // turn on the random LED 
    delay(myDelay);               // leave on for # of milliseconds set by potentiometer ( 1-10 dial )
    digitalWrite(myLed,LOW);      // turn off that led
    delay(myDelay) ;              // delay off for amout of time it was on  
    
}
}

ya, sorry . didnt include because its pretty much the same as the original

Your setup function needs a closing brace }

1 Like

thanks i just realized that my last version wasn't saved when the power went out