Hi guys;
I just finish my programing excercise or "homework". It a school bus alternating light simulator. It simply the same way when you operated a school bus warning lights system and with a stop arm and crossing gate. I did have a few problems when I program it, because when I close the door, the master is still on, and then close the master and re-open the door, I have the lights on, but it not supposed to. Because of that problem, I re-wrote the code and I have no problem ( so far ) My solution was to check for the master in all the while() loop and to use if ( (state==1) && (state2==1)) . The Master Switch is reponsible to turn on the school bus warning system. Sorry that my code look a bit "confusing". I was logical when I program it.
Anyway, feel free to comments, to "correct" my mistakes and teacher , please mark me and tell me if I could do it better.
I drive a school bus for a living in Toronto, ON Canada, I know in the USA, they also use school buses. But I don't if they use school buses in the UK, France, Germany, Itally, ( Europe ) Russia, Asia ( Japan , China ) , Argentina, Brazil, Mexico ( South America ) , or in Australia ? Just wonderring
Here my code :
/*
size : 1776 bytes
Version 1.1
file name : schoolbussignal.pde
School Bus Alternating Light Simulator
It simulate the activation system to activate the alternating
red light, stop sign and crossing gate of the school bus.
Here the parts you need :
4 - 330 ohms limiting resistors for the LED's
2 - Red LED
2 - Green KED
3 - 10 K resistor pull-down resistor
2 - SPDT or SPST switch or DIP switch
1 - Push-on button switch
How it work :
1. To activate the system, turn on MASTER switch.
2. Press the push button to activate the Red Lights ONLY.
3. When the door is open, a swtich is close ( the door switch is ON )
and activate the stop sign ( left side of the bus )
and crossing ( front of the bus ) gate.
4. Close the door ( turn the door off ) to turn off everythings.
5. When open the door ( turn on/off door switch ) and
Master switch is OFF, nothing will happen.
6. Even you press the push button and the Master switch is OFF,
nothing will happen.
Program by Serge J Desjardins aka techone
Compile and Tested
*/
// input pin
// pin 9 and 8 are not use in the program except in void setup()
byte businpin[5]={12,11,10,9,8};
/*
pin 12 : Master Switch
pin 11 : Door Switch
pin 10 : Push button to activate
pin 9 : Front gard gate switch
pin 8 : Stop Sign switch
*/
// output pin
// pin 5 and 3 are not use by the program except in void setup()
byte busoutpin[6]={7,6,5,4,3,2};
/*
pin 7 : Alterning Light A
pin 6 : Alterning Light B
pin 5 : Front gard gate motor direction
pin 4 : Front gard gate motor on/off
pin 3 : Stop sign motor direction
pin 2 : Stop sign motor on/off
*/
byte i;
boolean master=0; // Master Switch
boolean door=0; // Door Switch
boolean pushbutton=0; // Push Button
boolean buttonstate=0; // State Control
boolean doorstate=0;
void setup()
{ // init out & in pin
for (i=0;i<5;i++)
{
pinMode(businpin[i],INPUT);
pinMode(busoutpin[i],OUTPUT);
}
pinMode(busoutpin[5],OUTPUT);
for (i=0;i<6;i++)
{
digitalWrite(busoutpin[i], LOW);
}
buttonstate=0;
doorstate=0;
turnoff();
}
void loop()
{
master=digitalRead(businpin[0]);
delay (100);
// stay in the loop and check the master switch
while (master==0)
{
master=digitalRead(businpin[0]);
delay (100);
}
// check the push button
pushbutton=digitalRead(businpin[2]);
delay (100);
if ((pushbutton==1) && (master==1))
{
// a push button is press
buttonstate=1;
}
// check the door switch
door=digitalRead(businpin[1]);
delay (100);
if ((door==1) && (master==1))
{
// door switch is ON
doorstate=1;
buttonstate=1;
}
if ((door==1) && (master==0))
{
// door is open AND master switch is OFF
doorstate=0;
buttonstate=0;
}
// stay in the loop if master switch is on
// check for the push button ON or door switch ON
while ((buttonstate==0) && (doorstate==0))
{
// check the master switch again
master=digitalRead(businpin[0]);
delay (100);
// check push button again
pushbutton=digitalRead(businpin[2]);
delay (100);
if ((pushbutton==1) && (master==1))
{
buttonstate=1;
}
// check the door switch again
door=digitalRead(businpin[1]);
delay (100);
if ((door==1) && (master==1))
{
doorstate=1;
buttonstate=1;
}
// when door is open and master switch is off
if ((door==1) && (master==0))
{
doorstate=0;
buttonstate=0;
}
}
// stay in loop when the master id ON AND the push button is press
while ((master==1) && (buttonstate==1))
{
// check the master switch again
master=digitalRead(businpin[0]);
delay (100);
if ((master==1) && (buttonstate==1))
{
// push button is press AND master is ON
alternating();
}
// check the door again
door=digitalRead(businpin[1]);
delay(100);
// the door is open with master on
if ((door==1) && (master==1))
{
doorstate=1;
alternating();
motorson();
alternating();
}
// after the door was open and closing the door
if ((door==0) && (doorstate==1))
{
buttonstate=0;
}
}
// turn off everythings when master is off or door is close
if ((master==0) || (door==0))
{
doorstate=0;
buttonstate=0;
motorsoff();
turnoff();
}
}
// alternating red light routine
void alternating()
{
digitalWrite(busoutpin[0], HIGH);
digitalWrite(busoutpin[1], LOW);
delay(500);
digitalWrite(busoutpin[0], LOW);
digitalWrite(busoutpin[1], HIGH);
delay (500);
}
// turn off everythings routine
void turnoff()
{
for (i=0; i<6;i++)
{
digitalWrite(busoutpin[i], LOW);
}
}
//deploy stop sign motor and crossing gate motor
void motorson()
{
digitalWrite(busoutpin[3], HIGH);
digitalWrite(busoutpin[5], HIGH);
}
//re-track back stop sign and crossing gate
void motorsoff()
{
digitalWrite(busoutpin[3], LOW);
digitalWrite(busoutpin[5], LOW);
}