Ok, maybe I should give you more details.
So, I'm in the 4th year at Electric Engineering and Computer Science and my bachelor degree examination is coming in 3 months and I was planning to make a project which consists of a double leaf bascule bridge system - fully automated, using sensors/micro-servo motors and audio and visual warnings.
Basically, I use 2x Sharp proximity sensors placed one in the left side of the bridge and the other one in the right side.
For example:
- if the left sensor detects movement (that means a cargo-boat is aproaching the bridge)
- then wait 5 seconds, start the audio ( one mini-speaker set at a specific frequency) and visual warnings on the bridge (2 red LEDs blinking)
- after 5 more seconds lower the tollbooths,
- after 5 more seconds lift the bridge leafs and then wait in this position untill the right sensor detects movement
- when the right sensor detects movemet, that means the cargo-boat passed under the bridge and reached the safe distance from the bridge
- wait 5 more seconds to be sure, then lower the bridge
- wait 5 seconds, lift up the tollbooths and shut off the audio and visual warnings (and maybe turn on a green LED)
This is my idea for the project, I bought all components but I'm newbie at programming.Despite that, I want to make it on my own, not paying someone to do it for me (because it's common practice here in Romania). I'm eager and want to make it on my own, keeping intact my ego and proudness.
Getting back at my problem, I don't know if I can use delays between actions (for ex: blinking 2 led's at 700ms) because I'm doing other actions in the same time, or at least it seems that in "IF"-s statements the delays are summing themselves and i need for ex: 2 secs delay for LEDs, 5 secs delay for the bridge, 10 secs for tolbooths...etc. Maybe I'm wrong, that's why I came here to learn from more experienced people.
For ex, when the left sensor detects movement it turns ON led_l (used as marker - for the examiners to know that the left sensor detected movement) and the LED stays on. After that when the right sensor detects movement (that means the cargo-boat passed under the bridge), another LED (led_r) turns ON for visual confirmation. After this I want both marker LEDs to stay on in the same time for 3 seconds (regardless if the sensors are detecting movement or not in this 3 second interval) and after that to turn them OFF, lower the bridge, lifting the tolbooths and turning off the audio warning and tolboots warning red-LEDs.
Here is my problem, I don't know how to do that without using a big delay of 3 seconds because I think it would interact with the other delays (if i use delays).
I wanted to use counters, and at the end of the loop to put a small delay of 10 ms but I can't manage to make it work no matter how hard I sweat..
As you can see in the code below, for "red LED's warning of tolbooths" i used the millis() function but i cannot use the millis() function for turning off the marker LEDs of the sensors because I don't know how much it takes to a cargo-boat to pass from left sensor to right sensor, it could vary from one type of boat to another (30 secs - 2 minutes)...and the examiners might take this into consideration. [ And as i know, the millis() function start counting when the program start running, so, in this case can't help me much, only in "red LED's warning of tolbooths" case). ] <- here is my problem.
const int ledPin13 = 13; // the number of the LED pin
const int ledPin12 = 12;
int ledState13 = LOW; // ledState used to set the LED
int ledState12 = LOW;
long previousMillis = 0; // will store last time LED was updated
long previousMillis2 = 0;
long interval = 700;
long interval2 = 5000;
//sharp proximity sensor
const int LED_L = 10; // warning led
const int LED_R = 11; // warning led
const int Lstate = 2; // pin 2 - read sensor
const int Rstate = 4; // pin 4 - read sensor
int var_Lstate = 0; // reset to zero the variable used to read the state of the OUT pin of the sensor
int var_Rstate = 0;
int read_LED_L = 0;
int read_LED_R = 0;
void setup()
{
pinMode(ledPin13, OUTPUT); // set the digital pin as output:
pinMode(ledPin12, OUTPUT); // set the digital pin as output:
//senzor
pinMode (LED_L, OUTPUT); // left warning led
pinMode (LED_R, OUTPUT); // right warning led
pinMode (Lstate, INPUT); // sets pin 2 as digital input
pinMode (Rstate, INPUT); // pin 4
}
void loop()
{
// here is where you'd put code that needs to be running all the time.
var_Lstate = digitalRead(Lstate); // reads the status of the left sensor
var_Rstate = digitalRead(Rstate); // reads the status of the right sensor
read_LED_L = digitalRead(10);
read_LED_R = digitalRead(11);
unsigned long currentMillis = millis(); // check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
if(var_Lstate == LOW)
digitalWrite (LED_L, HIGH); // turn on the LED - LEFT
else
if(var_Rstate == LOW)
digitalWrite (LED_R, HIGH); // turn on the LED - RIGHT
// if sensor L or R is detecting movement (regardless the duration) start audio and light warning
if(read_LED_L == HIGH || read_LED_R == HIGH)
{
if(currentMillis - previousMillis > interval)
{
previousMillis = currentMillis; // save the last time you blinked the LED
if (ledState13 == LOW) // if the LED is off turn it on and vice-versa:
{
ledState13 = HIGH;
ledState12 = LOW;
tone(8, 2200, 250);
}
else
{
ledState13 = LOW;
ledState12 = HIGH;
tone(8, 1800, 250);
}
digitalWrite(ledPin13, ledState13); // set the LED with the ledState of the variable:
digitalWrite(ledPin12, ledState12);
// TOLBOOTS's MOTORS function for LOWERING - in progress..
// BRIDGE MOTORS function for lifting it - in progress..
}
}
// turning the sensor's LEDs OFF if they're ON on the same time, after a while - HERE IS MY PROBLEM..
unsigned long currentMillis2 = millis();
if(read_LED_L == HIGH && read_LED_R == HIGH)
{
if(currentMillis2 - previousMillis2 > interval2)
{
previousMillis2 = currentMillis2;
ledState12 = LOW;
ledState13 = LOW;
digitalWrite(ledPin13, ledState13); // set the LED with the ledState of the variable:
digitalWrite(ledPin12, ledState12);
noTone(8);
digitalWrite(LED_L, LOW);
digitalWrite(LED_R, LOW);
}
// BRIDGE MOTORS function for LOWERING - in progress..
// TOLBOOTS's MOTORS function for LIFT UP - in progress..
}
}
For the micro-servo motors (for both bridge and tollbooths - the difference between bridge and tollboots is only in the matter of choosing the degrees), I tought at:
// bridge
int pos = 20;
void loop
{
while(pos < 81)
{
pos++; // it goes from 20 -> 80 degrees
myservo.write(pos);
delay(70); // delay between steps of the micro-servo motor
}
pos2=pos;
while(pos2>20) // it's actually 80
{
pos2--; // lower to 20 degrees
myservo.write(pos2);
delay(70);
}
pos=pos2;
}
I haven't got the time yet but i wan't to try the counter here to see if I can replace the delay, if not, maybe the visible delay won't be so noticed.
Today I tought at using SWITCH-CASE statement and re-do the whole code, maybe in this case I can use delays (implemented in cases...or don't know... I could really use a more experienced people opinions/advices or help.
I'm sorry for my english if I made some huge mistakes but hopefully after this whole roman-novel of a post someone could help me. 