Well I'm back to bother you guys some more.
I manged to get all four switches controlling their own LED, push on, push off, held buttons ignored. So I'm excited about that. However I still have the obvious issue of the switches bouncing. I've debounced single switches using delay() but I've been told this is bad practice. On top of that I don't really see that being feasible with this switch array. Also with it all being through one analog pin I don't see a hardware solution. I know to you guys this is probably a simple problem but I feel that if I don't really learn and understand these basics I'm just shooting myself in the foot before I learn to walk.
Here is my current code, any comments are always welcome, and of course examples. Thanks so much!
// Define Constants
const int switcharrayPin=A0; // Set switch array pin to analog pin 0
const int ledonePin=11; // Set LED pin to 11
const int ledtwoPin=10; // Set LED two pin to 10
const int ledthreePin=9;// Set LED three to pin 9
const int ledfourPin=6; // Set LED four to pin 6
// Define Variables
long int switcharrayvalue=0; // Keep track of the switch value between 0 and 1280
boolean switchonecurrent=0; // Boolean to hold current switch one position
boolean switchonelast=0; // Boolean to hold last switch one position
boolean switchtwocurrent=0; //Hold switch twos position
boolean switchtwolast=0; //Track switch twos position
boolean switchthreecurrent=0; //hold switch threes position
boolean switchthreelast=0; //track switch threes position
boolean switchfourcurrent=0; //hold switch fours position
boolean switchfourlast=0; //track switch threes position
void setup()
{
pinMode (switcharrayPin, INPUT); //set switch array pin to an input
pinMode (ledonePin, OUTPUT); // set LED one pin to output
pinMode (ledtwoPin, OUTPUT); // set LED two pin to output
pinMode (ledthreePin, OUTPUT); //set LED three pin to output
pinMode (ledfourPin, OUTPUT); //set LED four pin to output
}
void loop()
{
// LED ONE CHECK AND SWITCH CODE
switcharrayvalue=analogRead(switcharrayPin); // Read Switch Array
if(switcharrayvalue > 350 && switcharrayvalue < 550 && switchonelast==false)// See if switch one is pressed when it was not pressed before
{
switchonecurrent = !switchonecurrent; // If so, flip switch ones value
switchonelast=true; // If so, make switch ones last state to true
}
else
{
if(switcharrayvalue < 350 || switcharrayvalue > 550)// See if button one is not pressed
{
switchonelast=false; // If it's not, set switch ones last state to false
}
}
digitalWrite(ledonePin,switchonecurrent); // Write switch ones current state to LED One
// LED TWO CHECK AND SWITCH CODE
switcharrayvalue=analogRead(switcharrayPin); // Read Switch Array
if(switcharrayvalue > 320 && switcharrayvalue < 350 && switchtwolast==false)// See if switch two is pressed when it was not pressed before
{
switchtwocurrent = !switchtwocurrent; // If so, flip switch twos value
switchtwolast=true; // If so, make switch twos last state to true
}
else
{
if(switcharrayvalue < 320 || switcharrayvalue > 350)// See if button two is not pressed
{
switchtwolast=false; // If it's not, set switch twos last state to false
}
}
digitalWrite(ledtwoPin,switchtwocurrent); // Write switch twos current state to LED two
// LED THREE SWITCH AND CODE
switcharrayvalue=analogRead(switcharrayPin); // Read Switch Array
if(switcharrayvalue > 240 && switcharrayvalue < 260 && switchthreelast==false)// See if switch three is pressed when it was not pressed before
{
switchthreecurrent = !switchthreecurrent; // If so, flip switch threes value
switchthreelast=true; // If so, make switch threes last state to true
}
else
{
if(switcharrayvalue < 240 || switcharrayvalue > 260)// See if button three is not pressed
{
switchthreelast=false; // If it's not, set switch threes last state to false
}
}
digitalWrite(ledthreePin,switchthreecurrent); // Write switch threes current state to LED three
// LED FOUR CHECK AND SWITCH CODE
switcharrayvalue=analogRead(switcharrayPin); // Read Switch Array
if(switcharrayvalue > 190 && switcharrayvalue < 220 && switchfourlast==false)// See if switch four is pressed when it was not pressed before
{
switchfourcurrent = !switchfourcurrent; // If so, flip switch fours value
switchfourlast=true; // If so, make switch fours last state to true
}
else
{
if(switcharrayvalue < 190 || switcharrayvalue > 220)// See if button four is not pressed
{
switchfourlast=false; // If it's not, set switch fours last state to false
}
}
digitalWrite(ledfourPin,switchfourcurrent); // Write switch fours current state to LED four
}