Help in programming of 4 channel touch sensor

Hi, I have made a 4 channel touch sensor which is connected to led's. When i touch the sensor the light turns onn and then when I lift my finger, it turns off. I want to program it to not make the led turn off till I touch the sensor again. I am writing the code which I am using now and I want someone to help me in what to change to make it work like that.

int LD = 200; // Loop Delay. Do not change.

void setup() {
Serial.begin(9600);
// out pins
pinMode(10, OUTPUT);// LED for button 1
pinMode(11, OUTPUT);// LED for button 2
pinMode(12, OUTPUT);// LED for button 3
pinMode(13, OUTPUT);// LED for button 4

// input pins
pinMode(2, INPUT);// Button 1 input pin 2  
pinMode(3, INPUT);// Button 2 input pin 3  
pinMode(4, INPUT);// Button 3 input pin 4  
pinMode(5, INPUT);// Button 4 input pin 5              


Serial.println(LD);

}

void loop() {

// button 1 action
if(digitalRead(2)){
  Serial.println("Button 1 Touched "); 
  digitalWrite(10, HIGH); // Turn the LED ON     
  delay(LD); 
}else{
  digitalWrite(10, LOW);// Turn OFF the LED
}

// button 2 action
if(digitalRead(3)){
  Serial.println("Button 2 Touched "); 
  digitalWrite(11, HIGH); // Turn the LED ON
  delay(LD); 
}else{
  digitalWrite(11, LOW);// Turn OFF the LED
}   

// button 3 action
if(digitalRead(4)){
  Serial.println("Button 3 Touched "); 
  digitalWrite(12, HIGH); // Turn the LED ON       
  delay(LD); 
}else{
  digitalWrite(12, LOW);// Turn OFF the LED
}     

// button 4 action
if(digitalRead(5)){
Serial.println("Button 4 Touched ");
digitalWrite(13, HIGH); // Turn the LED ON
delay(LD);
}else{
digitalWrite(13, LOW);// Turn OFF the LED
}

}// loop

The pictures are attached below.
Thanks

Code tags learn how to use them.

void setup() {
Serial.begin(9600);
// out pins
pinMode(10, OUTPUT);// LED for button 1
pinMode(11, OUTPUT);// LED for button 2
pinMode(12, OUTPUT);// LED for button 3
pinMode(13, OUTPUT);// LED for button 4
// input pins
pinMode(2, INPUT);// Button 1 input

Something like a toggle?

Make a variable to hold the last button pressed state and toggle the variable with each button press. Use the variable state to determine if light on or light off.

bool avariablethingy = false;

voiding loopy()
{
if (buttonispressed)
{
avariablethingy= !avariablethingy;
}
if avariablethingy
ledon
else
ledoff
}

Learn about state machines and it will also help when your needs change

3 posts were split to a new topic: If I change the delay time, code not working

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.