'runTheLeds' was not declared in this scope

Hi Guys,

I am trying to have the servo move randomly and when it is in its delay i want to be able to push the button and turn the LED on. I tried to put millis code, but it doesnt seem to work on arduino(Servo is moving rapidly, i onlly want to have it move every 5 seconds and be able to push the button for the LED on and off).

It also gave me this error message:

'runTheLeds' was not declared in this scope

Does anyone have a idea how i can manage this, or what i did wrong?

Thanks alot!

int time;
#include <Servo.h>
long randomNumber;
long servoNumber;
Servo myservo;
int pos = 0;

int buttonPin = 7;
int ledPin = 8;

int buttonstate = 0;

boolean canBePressed = false;

unsigned long previousMillis = 0;
const long interval = 5000;

void setup() {
  myservo.attach(9);
  Serial.begin(9600);
//  randomSeed(analogRead(0));

  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
}

void loop() {
//  static unsigned long previousMillis = millis();
//  if (millis() - previousMillis >= interval) {
//
//    buttonstate = digitalRead(buttonPin);
//
//    if (buttonstate == HIGH) {
//      if (on == true) {
//        on = false;
//      }  else {
//        on = true;
//      }
//    }
//  }
//  if (on == true) {
//    digitalWrite(ledPin, HIGH);
//  }
//  else {
//    digitalWrite(ledPin, LOW);
//  }
  unsigned long currentMillis = millis();
  int timer = 0;

  if(timer = 1000){
    runTheLeds();
  }
  if(timer = 1001){
    timer = 0;
  }
  serial.println(timer);
  public void runTheLeds(){
    previousMillis = currentMillis;
    
    randomNumber = random(1000);
    servoNumber = map(randomNumber, 0, 1000, 0, 180);
    myservo.write(servoNumber);
//    Serial.println(servoNumber);
  
    buttonstate = digitalRead(buttonPin);
//    Serial.println(buttonstate);

    // We checken of de hoek 90° is.
    if(servoNumber == 90){
//      Serial.println("EMIEL RAM OP DE KNOP"); 
      canBePressed = true;
    } else {
      canBePressed = false;
    }

    // als de hoek 90° is dan kun je op de button drukken en zet deze de led aan.
    if (canBePressed){
      if (buttonstate == HIGH) {
        digitalWrite(ledPin, HIGH);
      } else {
        digitalWrite(ledPin, LOW);
      }
    } 
  }
  timer++;
  
//  previousMillis += interval;

//  {
//    static unsigned long previousMillis = millis();
//    if (millis() - previousMillis >= interval)
//   
//    randomNumber = random(1000);
//
////    Serial.print("random:");
////    Serial.println(randomNumber);
//
//    servoNumber = map(randomNumber, 0, 1000, 0, 180);
////      randomNumber = 90;
//
////    Serial.print("servo:");
//    Serial.println(servoNumber);
//    myservo.write(servoNumber);
//    /*
//      if (millis() - previousMillis >= interval) {
//      for (pos = 0; pos <= 180; pos += 1)
//      myservo.write(pos);
//      if (millis() - previousMillis >= interval) {
//      previousMillis += interval;
//
//      if (millis() - previousMillis >= interval) {
//      for (pos = 180; pos >= 0; pos -= 1)
//      myservo.write(pos);
//      previousMillis += interval;
//    */
//    previousMillis += interval;
//  }
//  delay(1000);
}

Have good look at where the runTheLeds() function is situated

Why is it in the middle of the loop() function ?

“public” ?

UKHeliBob:
Have good look at where the runTheLeds() function is situated

Why is it in the middle of the loop() function ?

Hi, I dont know where to place the else? at this moment i dont have any idea what i am doing.

You have placed the definition of a function inside another function

What you probably want is to call the function where you currently have the definition.

The public keyword is not appropriate here.

It would be polite not to post all the code that is commented-out.

EmilOlpinski:
Hi, I dont know where to place the else? at this moment i dont have any idea what i am doing.

You can call the function from practically anywhere like this

   runTheLeds();

but the actual definition of the function

void runTheLeds(){
    previousMillis = currentMillis;
    
    randomNumber = random(1000);
    servoNumber = map(randomNumber, 0, 1000, 0, 180);
    myservo.write(servoNumber);
etc
etc

must be outside of a function

The problem with your code might be more apparent if you Auto Format it in the IDE, put each { and } on its own line and as suggested, remove the commented out portions of the program.