0

I am new to Arduino and C++ and I was practicing with a traffic light project.

This is my code:

int red = 10;
int yellow = 9;
int green = 8;

void setup() {
  pinMode(red,OUTPUT);
  pinMode(yellow,OUTPUT);
  pinMode(green,OUTPUT);
}

void loop() {
  changelights();
  delay (15000);
}

void changelights{
  // 1st action: green off to yellow
  digitalWrite(green, LOW);
  digitalWrite(yellow, HIGH);
  delay (3000)
  // 2nd action: tellow to red 
  digitalWrite(yellow, LOW);
  digitalWrite(red, HIGH);
  //3rd action: caution light 
  digitalWrite(yellow, HIGH);
  delay (3000)
  //4th action: caution to go 
  digitalWrite(red,LOW);
  digitalWrite(yellow, LOW);
  digitalWrite(green, HIGH);
}

I tried compiling it and returned an error message that said that I did not declare my "changelights" function. What should I do to fix this?

Thank you very much

3
  • I recommend to put only 1 instruction in 1 line. that improves readability by a lot and makes it easier to find errors. Commented Apr 30, 2021 at 6:42
  • 2
    You forgot the parentheses ( ) for the parameters after the definition of changelights. You need these, even if you don't provide any parameters. Though this is a pure C/C++ syntax question, thus I'm voting to close. Fixing such problems doesn't help any other people, thus I'm always helping in the comments and then vote to close. Commented Apr 30, 2021 at 7:38
  • 2
    Missing semi-colons after the delay(3000) calls. Commented Apr 30, 2021 at 12:13

1 Answer 1

3

You forgot to put the parenthenses () after the name of the function changelights. They are there to declare variables you can use later in the code like that:

void function(int a){
  a+=1;
  return a;
}

To fix your error just change

void changelights{

to

void changelights(){

Keep in mind if you want to program C++ (which would be Off-Topic here, but you mentioned it in your question) you have to declare the function in the beginning or mention it.

3
  • 2
    The Arduino IDE takes care of adding the forward declarations for you. Commented Apr 30, 2021 at 7:09
  • 1
    The problem is with the missing parentheses at the function definition. The forward declaration of the functions is done by the Arduino IDE. And I think we can assume, that he uses it. At that point working with Arduino that is the simplest way. Commented Apr 30, 2021 at 7:39
  • @chrisl I see, I will edit the answer. Commented Apr 30, 2021 at 7:45

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.