Hello, I'm just starting with Arduino and was coding a way to make a stoplight that relies on a sensor and a timer. Basically, the lights will change depending on whether an object passes through the sensor before the timer is up. This is the code:
/*
*/
unsigned long currentTime;
unsigned long loopTime;
int g1 = 10; // the pin that the LED is atteched to
int y1 = 9;
int r1 = 8;
int g2 = 13;
int y2 = 12;
int r2 = 11;
int g3 = 7;
int y3 = 6;
int r3 = 5;
int sensor = 2; // the pin that the sensor is attached to
int state = LOW; // by default, no motion detected
int val = 0; // variable to store the sensor status (value)
void setup() {
currentTime = millis();
loopTime = currentTime;
pinMode(r1, OUTPUT); // initalize LED as an output
pinMode(r2, OUTPUT);
pinMode(r3, OUTPUT);
pinMode(y1, OUTPUT);
pinMode(y2, OUTPUT);
pinMode(y3, OUTPUT);
pinMode(g1, OUTPUT);
pinMode(g2, OUTPUT);
pinMode(g3, OUTPUT);
digitalWrite(g2, HIGH);
digitalWrite(g3, HIGH);
digitalWrite(r1, HIGH);
pinMode(sensor, INPUT); // initialize sensor as an input
Serial.begin(9600); // initialize serial
// initialize serial
void loop() {
if (digitalRead(sensor) == HIGH) // Switch is closed to start LED timer
{
currentTime = millis();
if (digitalRead(sensor) == HIGH)
{
if ( mils - wasmils >= 1000 );
{
seconds ++;
wasmils = mils;
digitalWrite(g1, HIGH);
digitalWrite(r2, HIGH);
digitalWrite(r3, HIGH);
digitalWrite(g2, LOW);
digitalWrite(g3, LOW);
digitalWrite(r1, LOW);
}
}
else
delay(10000);
digitalWrite(g1, HIGH);
digitalWrite(r2, HIGH);
digitalWrite(r3, HIGH);
digitalWrite(g2, LOW);
digitalWrite(g3, LOW);
digitalWrite(r1, LOW);
}
}
Whenever I try to verify it, I always get the error "a function-definition is not allowed here before '{' token". Can anyone tell me how to fix the error? Thank you in advance :)))))