Hi! Just ought my first Arduino UNO and have started programming it.
Its the first time that I work with Arduino, first time with a microcontroller at all actually…
Im going to use it to control a road barrier.
For this, I will use this digital pins:
3 - Output - Barrier up
4 - Output - Barrier down
5 - Input - Barrier down microswitch
6 - Input - Barrier up microswitch
7 - Input - Open barrier button
8 - Input - Offbutton (not yet a part of the program.)
9 - Input - Movementsensor
I want it to work like this:
When I press Open barrier button (7) the road barrier goes ut by output 3.
When its open (input6) arduino counts to 20sec and then close it again. If the sensor is triggered during this time, the counter will start over again.
After 20sec the barrier will go down by activating output 4 until input 5 is triggered.
I have made a program (first time, maybe totally wrong but im new in this) and it works. First time.
When everything is finished and I press the open button again, it stop working and looks like its jumping inside my code (serial monitoring so I can see where it is).
But if I wait for maybe 10-20 seconds after the first time before I press it again, it works.
Its like it need time to rest before I can start the sequence again.
Really need help with this to get it stable.
Someone who can help me??
Here is the code:
// barrieropen = digitalWrite(3)
// barrierclose = digitalWrite(4)
// inputbarrierclosed = digitalRead(5)
// inputbarrieropen = digitalRead(6)
// inputopenbutton = digitalRead(7)
// inputoff = digitalRead(8)
// inputsensor = digitalRead(9)
int t = 0;
void setup() {
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);
Serial.begin(9600);
}
void loop() {
//Input 7 is the pushbutton for opening the barrier
if (digitalRead(7) == 1)
{
Serial.print("Openbutton");
openup:
//Drive the road barrier up until input 6 (barrier open switch) is on.
do
{
Serial.print("Opening");
digitalWrite(3, HIGH);
delay(30);
} while(digitalRead(6) == 0);
//Stops the motor when the barrier is fully open.
digitalWrite(3, LOW);
//Counts to 20 before closing the barrier again.
do
{
delay(1000);
//If the sensor is triggered, this will reset the counter again.
if (digitalRead(9) == 1)
{
t = 0;
Serial.print("Sensor");
}
else
{
t = t + 1;
Serial.print(t);
}
} while(t<20);
//Reset the counter until next time.
t = 0;
//Closing the barrier by driving the engine until input 5 is high. (Barrier closed switch)
do
{
//Reset ewerything if sensor is triggered and goes back to open the barrier.
if (digitalRead(9) == 0)
{
Serial.print("Closing");
digitalWrite(4, HIGH);
delay(30);
}
else
{
goto openup;
}
} while(digitalRead(5) == 0);
//Stopping the engine when barrier is open.
digitalWrite(4, LOW);
}
else;
{
Serial.print(digitalRead(7));
delay(30);
}
}
Moderator edit: Quote box replaced with code box to eliminate smilies