Turn on leds with simple switches

Hi everybody

I need to ask somethig, I want to turn on 6 leds, evey one has his own switch (attached pictures), you do not care about the white button, when I reach to turn on the leds I going to give it his userful funtion

On the right you can see the pin`s leds

Over here, you can see the tracks connections, the led share negative tracks current, the switches share 5V current. I test it with a tester on the tracks, nothings is wrong in there,the switches open and close the circuit when I press them.

Leds are in HIGH status, to prove how that circuit works.

This is the idea: when I press shk1 must turn on Led1, and successively with the others till shk6;Led6. In this moment I press , e.g, the shk1 I can not turn on the Led1.

I do not know that I am doing wrong, could you help me please.

P.D: Sorry for my english if I wrote it incorrectly.

//Declaración de Leds
int Led1=8; //Led delantero Izquierdo
int Led2=9; //Led delantero Derecho
int Led3=10; //Led central Izquierdo
int Led4=11; //Led central Derecho
int Led5=12; //Led trasero Izquierdo
int Led6=13; //Led trasero Derecho

//Declaración de sensores de impacto

int shk1=2; //Sensor delantero Izquierdo
int shk2=3; //Sensor delantero Derecho
int shk3=4; //Sensor central Izquierdo
int shk4=5; //Sensor central Derecho
int shk5=6; //Sensor trasero Izquierdo
int shk6=7; //Sensor trasero Derecho

int val;

void setup() {
  //Leds como salidas
pinMode(Led1,OUTPUT);
pinMode(Led2,OUTPUT);
pinMode(Led3,OUTPUT);
pinMode(Led4,OUTPUT);
pinMode(Led5,OUTPUT);
pinMode(Led6,OUTPUT);
//Sensores como entrada
pinMode(shk1,INPUT);
pinMode(shk2,INPUT);
pinMode(shk3,INPUT);
pinMode(shk4,INPUT);
pinMode(shk5,INPUT);
pinMode(shk6,INPUT);
}

void loop() {
digitalWrite(Led1,LOW);
digitalWrite(Led2,LOW);
digitalWrite(Led3,LOW);
digitalWrite(Led4,LOW);
digitalWrite(Led5,LOW);
digitalWrite(Led6,LOW);
digitalRead(shk1);
digitalRead(shk2);
digitalRead(shk3);
digitalRead(shk4);
digitalRead(shk5);
digitalRead(shk6);

 val=digitalRead(shk1);
 if(shk1==HIGH)
   {
    digitalWrite(Led1,HIGH);
   }
 else
  {
    digitalWrite(Led1,LOW);
  }
} //Cierre loop

Well, first of all, looks like all the LEDs are grounded through 3 parallel 220 Ohm resistors, that is only 73 Ohms. That will kill your Arduino outputs AND your LEDs. Each LED should have its own resistor (at least 220 Ohm).
Get that fixed, make a drawing of how it's wired (the picture is no good), come back, THEN we will talk about your program.

Besides the current draw (amps) issue that you have to fix before grilling your pins, then I would suggest to get rid of all the digitalRead() that are useless at the begining of the loop and rename things that are pins to have to word PinNb in the variable name and things that are states (HIGH Or LOW) to have the State word in the variable name

For example your code for this

val=digitalRead(shk1);
if(shk1==HIGH)

would become

shk1State = digitalRead(shk1PinNb);
if (shk1PinNb == HIGH)

And here suddenly your problem would become very obvious... (or should)

J-M-L offers good advice!

Everyone has there own naming conventions.

//Declaración de Leds
const uint8_t       pinLED_1 =  8;  //Led delantero Izquierdo
const uint8_t       pinLED_2 =  9;  //Led delantero Derecho
const uint8_t       pinLED_3 = 10;  //Led central Izquierdo
const uint8_t       pinLED_4 = 11;  //Led central Derecho
const uint8_t       pinLED_5 = 12;  //Led trasero Izquierdo
const uint8_t       pinLED_6 = 13;  //Led trasero Derecho

//Declaración de sensores de impacto

const uint8_t       pinSHK_1 = 2;   //Sensor delantero Izquierdo
const uint8_t       pinSHK_2 = 3;   //Sensor delantero Derecho
const uint8_t       pinSHK_3 = 4;   //Sensor central Izquierdo
const uint8_t       pinSHK_4 = 5;   //Sensor central Derecho
const uint8_t       pinSHK_5 = 6;   //Sensor trasero Izquierdo
const uint8_t       pinSHK_6 = 7;   //Sensor trasero Derecho

void setup()
{
    //Leds como salidas
    pinMode(pinLED_1, OUTPUT);
    pinMode(pinLED_2, OUTPUT);
    pinMode(pinLED_3, OUTPUT);
    pinMode(pinLED_4, OUTPUT);
    pinMode(pinLED_5, OUTPUT);
    pinMode(pinLED_6, OUTPUT);
//Sensores como entrada
    pinMode(pinSHK_1, INPUT);
    pinMode(pinSHK_2, INPUT);
    pinMode(pinSHK_3, INPUT);
    pinMode(pinSHK_4, INPUT);
    pinMode(pinSHK_5, INPUT);
    pinMode(pinSHK_6, INPUT);
}

void loop()
{
    digitalWrite(pinLED_1, LOW);
    digitalWrite(pinLED_2, LOW);
    digitalWrite(pinLED_3, LOW);
    digitalWrite(pinLED_4, LOW);
    digitalWrite(pinLED_5, LOW);
    digitalWrite(pinLED_6, LOW);

    digitalRead(pinSHK_1);
    digitalRead(pinSHK_2);
    digitalRead(pinSHK_3);
    digitalRead(pinSHK_4);
    digitalRead(pinSHK_5);
    digitalRead(pinSHK_6);

    int val = digitalRead(pinSHK_1);
    if ( val == HIGH )
    {
        digitalWrite(pinLED_1, HIGH);
    }
    else
    {
        digitalWrite(pinLED_1, LOW);
    }
}

Yes starting pin variable names with the work pin works too :slight_smile:

Still need to get rid of the totally useless

digitalRead(pinSHK_1);
...
digitalRead(pinSHK_6);

In the loop.

Agreed as well, mine was quick example of what applying a constant naming of symbols would allow.

By the way - For beginners (and more advanced) I prefer to be really clear that the name is a PIN number rather than just a PIN something because in common language it's pretty easy to say "if the PIN is HIGH" so then it becomes less obvious to see the mistake in "if (pin_shk1 == HIGH)"

Here’s a diagram that might help, you should connect 1 side of your switches to ground and use pinMode(shk?,INPUT_PULLUP) in void setup.

LEDs.png

outsider:
Here’s a diagram that might help, you should connect 1 side of your switches to ground and use pinMode(shk?,INPUT_PULLUP) in void setup.

LEDs.png

Thanks dude, actually I followed your advice and worked perfectly (I must reasign the circuit desing, I known now). Also I changed the elements names in the code:

//Cristopher López, Año 2016

//Declaración de Leds
int Led_1=8; //Led delantero Izquierdo
int Led_2=9; //Led delantero Derecho
int Led_3=10; //Led central Izquierdo
int Led_4=11; //Led central Derecho
int Led_5=12; //Led trasero Izquierdo
int Led_6=13; //Led trasero Derecho

//Declaración de sensores de impacto

int SHK_1=2; //Sensor delantero Izquierdo
int SHK_2=3; //Sensor delantero derecho
int SHK_3=4; //Sensor central Izquierdo
int SHK_4=5; //Sensor central derecho
int SHK_5=6; //Sensor trasero Izquierdo
int SHK_6=7; //Sensor trasero Izquierdo

int val;


void setup() {
  //Salidas analogas
pinMode(A0,OUTPUT);
pinMode(A1,OUTPUT);
pinMode(A2,OUTPUT);
pinMode(A3,OUTPUT);
pinMode(A4,OUTPUT);
pinMode(A5,OUTPUT);
  //Leds como salidas
pinMode(Led_1,OUTPUT);
pinMode(Led_2,OUTPUT);
pinMode(Led_3,OUTPUT);
pinMode(Led_4,OUTPUT);
pinMode(Led_5,OUTPUT);
pinMode(Led_6,OUTPUT);
//Sensores como entrada
pinMode(SHK_1,INPUT_PULLUP);
pinMode(SHK_2,INPUT_PULLUP);
pinMode(SHK_3,INPUT_PULLUP);
pinMode(SHK_4,INPUT_PULLUP);
pinMode(SHK_5,INPUT_PULLUP);
pinMode(SHK_6,INPUT_PULLUP);
}

void loop() {
  //Salidas analogas
digitalWrite(A0,LOW);
digitalWrite(A1,LOW);
digitalWrite(A2,LOW);
digitalWrite(A3,LOW);
digitalWrite(A4,LOW);
digitalWrite(A5,LOW);
   //Salidas digitales
digitalWrite(Led_1,LOW);
digitalWrite(Led_2,LOW);
digitalWrite(Led_3,LOW);
digitalWrite(Led_4,LOW);
digitalWrite(Led_5,LOW);
digitalWrite(Led_6,LOW);
digitalRead(SHK_1);
digitalRead(SHK_2);
digitalRead(SHK_3);
digitalRead(SHK_4);
digitalRead(SHK_5);
digitalRead(SHK_6);

  if (digitalRead(SHK_1) == HIGH) { //Led 1
         digitalWrite(Led_1, HIGH);
           }else{
            digitalWrite(Led_1, LOW);
            }                       //Cierre Led 1
  if (digitalRead(SHK_2)==HIGH){     //Led 2
          digitalWrite(Led_2,HIGH);
           }else{
            digitalWrite(Led_2,LOW);
            }                       //Cierre Led 2
  if (digitalRead(SHK_3)==HIGH){     //Led 3
          digitalWrite(Led_3,HIGH);
           }else{
            digitalWrite(Led_3,LOW);
           }                       //Cierre Led 3
   if (digitalRead(SHK_4)==HIGH){   //Led 4
           digitalWrite(Led_4,HIGH);
           }else{
            digitalWrite(Led_4,LOW);
           }                       //Cierre Led 4
   if (digitalRead(SHK_5)==HIGH){   //Led 5
           digitalWrite(Led_5,HIGH);
           }else{
            digitalWrite(Led_5,LOW);
           }                      //Cierre Led 5
   if (digitalRead(SHK_6)==HIGH){   //Led 6
           digitalWrite(Led_6,HIGH);
           }else{
            digitalWrite(Led_6,LOW);
           }                       //Cierre Led 6
//**************************************************************
       //Combinatorios    
    if(digitalRead(SHK_1)&& digitalRead(SHK_2)==HIGH){
        digitalWrite(A0,HIGH);
         }
    if(digitalRead(SHK_2)&& digitalRead(SHK_4)==HIGH){
         digitalWrite(A1,HIGH);     
         }
    if(digitalRead(SHK_4)&& digitalRead(SHK_6)==HIGH){
         digitalWrite(A2,HIGH);
         }
    if(digitalRead(SHK_6)&& digitalRead(SHK_5)==HIGH){
         digitalWrite(A3,HIGH);
         }
    if(digitalRead(SHK_3)&& digitalRead(SHK_5)==HIGH){
         digitalWrite(A4,HIGH);
         }
    if(digitalRead(SHK_1)&& digitalRead(SHK_3)==HIGH){
         digitalWrite(A5,HIGH);
         }
//***************************************************************          
} //Cierre loop

So, now I had to add new leds (I used all enable pins of arduino uno) and modified the code, I could make the comparasion between two sensors and I turned on a third led, e.g, if I press SHK_1 and SHK_2 y can turn on Led_1 and Led_2 AND TOO A0.

The problem is this; when I press SHK_1 and SHK_2 at the same time I just need turn on A0, not Led_1 and Led_2, I tried of many ways, but I could not do that…

The easiest might be to create a lookup table with functions to execute when a certain condition is met.

const byte SHK_1 = 2;
const byte SHK_2 = 3;

void action1()
{
  Serial.println("SHK1");
}
void action2()
{
  Serial.println("SHK2");
}
void action3()
{
  Serial.println("SHK1 and SHK2");
}
void action4()
{
  Serial.println("SHK2 and SHK3");
}

// lookup table of functions
// needs entries for ALL possible combinations
void (*actions[64])() =
{
  NULL,       // 0x00 no buttons pressed, do nothing
  action1,    // 0x01 first button pressed
  action2,    // 0x02 second button pressed
  action3,    // 0x03 first and second button pressed
  NULL,       // 0x04
  NULL,       // 0x05
  action4,    // 0x06 second and third button pressed
};

void setup()
{
  ...
  ...
}

Next in loop you read the buttons, combine them to a value and execute the function that is associated with the button combination.

void loop()
{
  byte buttonValues = 0;

  // read buttons
  if (digitalRead(SHK_1))
  {
    buttonValues |= 0x01;
  }
  if (digitalRead(SHK_2))
  {
    buttonValues |= 0x02;
  }

  Serial.print("buttonValues: ");
  Serial.println(buttonValues);

  // if an action is specified for the combination of buttons
  if (actions[buttonValues] != NULL)
  {
    // execute the action
    actions[buttonValues]();
  }
}

And in the next step you can use an array of button pins and do the reading of them in a for-loop within loop().

You have to implement the actionN() functions.

Ask if you don't understand.