I don't know this code may give you some idea about the state of a switch and help you for some insight...
Here a code. The code is a gear shifter. It got a Up and Down button. The switch go from HIGH to LOW. I hope it give you some insight.
/*
size : 1774 byte
Version 2.1
file name : gearshifter.pde
It simulate a gear shifter using 2 push-button switch.
The circuit use a 7 segment display and 2 led for the
simulated selenoid.
Gear sequence: Gear Number A B
1 1 1
2 0 1
3 0 0
4 1 0
Here the parts you need :
1 Commun Anode 7 segment Display
2 Red LED
2 Push Button Switch
9 2N3904 NPN Transistor
11 1 K resistor
9 330 ohms resistor
How it work :
1. Init the gear at 1.
2. Press Upshift Button to increase the gears
3. Press Downshift Button to decrease the gears.
Program by Serge J Desjardins aka techone / tech37
Compile and Tested <-- Test on a breadboard
Disclaimer:
I am not responsible for any damages, losses, injuries or death.
The user is responsabe for any damages, losses, injuries or death.
And Murphy's Law is always there, bear that in mind.
*/
// Arduino Digital pins
const byte outpin[9] = {4,5,6,7,8,9,10,11,12};
const byte inpin[2] = {2,3};
// Selenoid and Display array pattern
boolean gearone[9] = {1,1,0,0,0,0,1,1,0};
boolean geartwo[9] = {0,1,1,0,1,1,0,1,1};
boolean gearthree[9] = {0,0,1,0,0,1,1,1,1};
boolean gearfour[9] = {1,0,1,1,0,0,1,1,0};
byte gear;
int upshift=1;
int downshift=1;
int oldupshift=1;
int olddownshift=1;
boolean state=0;
void setup()
{
// init the digital pins
for (int i=0; i<9; i++)
{
pinMode(outpin[i],OUTPUT);
}
pinMode(inpin[0],INPUT);
pinMode(inpin[1],INPUT);
// Put in gear one
gear=1;
for (int i=0;i<9;i++)
{
digitalWrite(outpin[i],gearone[i]);
}
}
void loop()
{
readswitch();
while(state==0)
{
readswitch();
}
// a switch as been pressed
// Check for the upshift
if ((upshift==0) && (state==1))
{
gear++;
if ((gear>=1) && (gear<=4))
{
switch(gear)
{
case 1: selectone();break;
case 2: selecttwo();break;
case 3: selectthree();break;
case 4: selectfour();break;
}
}
else gear=4;
}
// check for the downshift
if ((downshift==0) && (state==1))
{
gear--;
if ((gear>=1) && (gear<=4))
{
switch(gear)
{
case 1: selectone();break;
case 2: selecttwo();break;
case 3: selectthree();break;
case 4: selectfour();break;
}
}
else gear=1;
}
}
void selectone()
{
for (int i=0;i<9;i++)
{
digitalWrite(outpin[i],gearone[i]);
}
state=0;
}
void selecttwo()
{
for (int i=0;i<9;i++)
{
digitalWrite(outpin[i],geartwo[i]);
}
state=0;
}
void selectthree()
{
for (int i=0;i<9;i++)
{
digitalWrite(outpin[i],gearthree[i]);
}
state=0;
}
void selectfour()
{
for (int i=0;i<9;i++)
{
digitalWrite(outpin[i],gearfour[i]);
}
state=0;
}
/* The subroutine is a modify switch example in page
51 of the book "Getting Started with Arduino"
The switch is configure with a pull-up resistor,
so it always a 1, when press, it is a 0.
*/
void readswitch()
{
upshift=digitalRead(inpin[0]);
// check upshift transition
if ((upshift==0) && (oldupshift==1))
{
state=1;
delay(50);
}
oldupshift=upshift;
downshift=digitalRead(inpin[1]);
// check downshift transition
if ((downshift==0) && (olddownshift==1))
{
state=1;
delay(50);
}
olddownshift=downshift;
}