my code:
int buttonPin = 5;`
int GREEN = 2;
int YELLOW = 3;
int RED = 4;
int DELAY_GREEN = 1000;
int DELAY_YELLOW = 1000;
int DELAY_RED = 1000;
// basic functions
void setup()
{
pinMode(GREEN, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(RED, OUTPUT);
}
void loop()
{
int green_light();
delay(DELAY_GREEN);
int yellow_light();
delay(DELAY_YELLOW);
int red_light();
delay(DELAY_RED);
}
void green_light()
{
digitalWrite(GREEN, HIGH);
digitalWrite(YELLOW, LOW);
digitalWrite(RED, LOW);
}
void yellow_light()
{
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, HIGH);
digitalWrite(RED, LOW);
}
void red_light()
{
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, LOW);
digitalWrite(RED, HIGH);
}
void loop()
{
if(digitalRead(buttonPin) == LOW)
{
void green_light()
{
digitalWrite(GREEN, HIGH);
digitalWrite(YELLOW, LOW);
digitalWrite(RED, LOW);
}
void yellow_light()
{
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, HIGH);
digitalWrite(RED, LOW);
}
void red_light()
{
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, LOW);
digitalWrite(RED, HIGH);
}
}
}
The error i am getting:
Arduino: 1.8.13 (Windows 10), Board: "Arduino Uno"
nuclear_thing_idk:51:3: error: expected unqualified-id before 'if'
if(digitalRead(buttonPin) == LOW)
^~
nuclear_thing_idk:75:3: error: expected declaration before '}' token
}
^
exit status 1
expected unqualified-id before 'if'
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
I'm sorry if this post isnt really well put together its 1AM i have school and my english is very bad.
loik-kun:
void yellow_light()
You already defined the function, remove the void keyword from where you use them inside loop().
Hi,
Welcome to the forum.
What do you want your code to do?
Apart from what @anon57585045 has said you may need to read up on the format of functions and how to call them?
Learn how to write and use functions with the Arduino in sketches. In this part of the programming course, functions are explained - calling a function, passing a value to and returning a value from a function.
Tom...
jimLee
April 14, 2021, 5:06am
4
Looks to me like you are trying to write something like this..
int buttonPin = 5;
int GREEN = 2;
int YELLOW = 3;
int RED = 4;
int DELAY_GREEN = 1000;
int DELAY_YELLOW = 1000;
int DELAY_RED = 1000;
// basic functions
void setup() {
pinMode(GREEN, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(RED, OUTPUT);
}
void loop() {
green_light();
delay(DELAY_GREEN);
yellow_light();
delay(DELAY_YELLOW);
red_light();
delay(DELAY_RED);
}
void green_light() {
digitalWrite(GREEN, HIGH);
digitalWrite(YELLOW, LOW);
digitalWrite(RED, LOW);
}
void yellow_light() {
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, HIGH);
digitalWrite(RED, LOW);
}
void red_light() {
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, LOW);
digitalWrite(RED, HIGH);
}
/*
// TROUBLE HERE, YOU CAN ONLY HAVE ONE loop().
void loop() {
if(digitalRead(buttonPin) == LOW) {
green_light();
yellow_light();
red_light();
}
}
*/
But I may be wrong. Also you can only have one loop() function. You tried to write two of them.
Looks like code tags are different here.. I guess at some point the'll sort that one out.
-jim lee
thanks for your help but it is not exactly what im trying to do.
I want to, whenever i press the button, it will start the green, yellow, red thing.
sorry if i'm asking for to much
Hi,
loik-kun:
I want to, whenever i press the button, it will start the green, yellow, red thing.
Can you post your circuit?
A hand drawn and posted image will be fine.
How have you got your button wired?
Did you try @jimLee 's code without the last void loop?
Tom....
jimLee
April 14, 2021, 5:57am
7
Oh! Like this?
int buttonPin = 5;
int GREEN = 2;
int YELLOW = 3;
int RED = 4;
int DELAY_GREEN = 1000;
int DELAY_YELLOW = 1000;
int DELAY_RED = 1000;
// basic functions
void setup() {
pinMode(GREEN, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(RED, OUTPUT);
}
void loop() {
if(digitalRead(buttonPin) == LOW) {
green_light();
delay(DELAY_GREEN);
yellow_light();
delay(DELAY_YELLOW);
red_light();
delay(DELAY_RED);
}
}
void green_light() {
digitalWrite(GREEN, HIGH);
digitalWrite(YELLOW, LOW);
digitalWrite(RED, LOW);
}
void yellow_light() {
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, HIGH);
digitalWrite(RED, LOW);
}
void red_light() {
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, LOW);
digitalWrite(RED, HIGH);
}
-jim lee
im writing it but it might be long cuz im bad a this.
thanks for helping me!
Hi loik,
Take a look into this tutorial:
Arduino Programming Course
It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.
best regards Stefan
Hi loik,
here is a version of your code that compiles.
I added some comments to explain what the different parts of the code do.
Though my comments do not explain all details.
int buttonPin = 5;
int GREEN = 2;
int YELLOW = 3;
int RED = 4;
int DELAY_GREEN = 1000;
int DELAY_YELLOW = 1000;
int DELAY_RED = 1000;
// define function named green_light
void green_light() {
digitalWrite(GREEN, HIGH);
digitalWrite(YELLOW, LOW);
digitalWrite(RED, LOW);
}
// define function named yellow_light
void yellow_light() {
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, HIGH);
digitalWrite(RED, LOW);
}
// define function named red_light
void red_light() {
digitalWrite(GREEN, LOW);
digitalWrite(YELLOW, LOW);
digitalWrite(RED, HIGH);
}
// basic functions
void setup() {
pinMode(GREEN, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(RED, OUTPUT);
}
void loop() {
if (digitalRead(buttonPin) == LOW) {
green_light(); // execute commands of function green_light
delay(DELAY_GREEN);
yellow_light(); // execute commands of function yellow_light
delay(DELAY_YELLOW);
red_light(); // execute commands of function red_light
delay(DELAY_RED);
}
}
best regards Stefan
jimLee
April 14, 2021, 8:36pm
11
@StefanL38 You missed the button bit.
-jim lee
YOu are right. I added the if-condition for the button.
best regards Stefan
system
Closed
August 13, 2021, 6:34am
16
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.