Go Down

### Topic: Need help with a Boolean operation and simplification on LED Keypad (Read 994 times)previous topic - next topic

#### Rich_P

##### Mar 22, 2013, 12:56 pm
Hi All, i am contructing a CAN based keypad for a motorsport vehicle,

Currently i am merging some code to read a keypad and switch on a corresponding LED above it

I am having trouble with the Boolean "i" ststement as it requires 2 pushes of a keypad to extinguish or illuminate an led, if the same operation has been perfomed on a diffrerent key

I understand why it is doing it, as it needs to switch from i = 1 to i = 0 before it can switch an LED on again, i just cant seem to get my head round doing it, also i would like to simplify it a little but not sure how.

Hope you are able to help

Many thanks

Rich

Code: [Select]
`#include <Keypad.h> // Include Keypad library#include <Ucs1903.h> // Include LED Controller Library#define NUM_LEDS 4  // Defines Number of LED's#define PIN 13      // Defines Data Pin for LED'sUcs1903 ledStrip(PIN, NUM_LEDS);boolean i = 0; //Defines Switch Stateint j = 0;   // Variable to Define which LED to Switchconst byte ROWS = 5; //defines number of rowsconst byte COLS = 3; //defines number of columnschar keys[ROWS][COLS] = {  //define the symbols on the buttons of the keypads as an array  {'1','2','3'},  {'4','5','6'},  {'7','8','9'},  {'0','A','B'},  {'C','D','E'},};byte rowPins[ROWS] = {9, 8, 7, 6, 5}; //connect to the row pinouts of the keypadbyte colPins[COLS] = {12, 11, 10,}; //connect to the column pinouts of the keypad Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );void setup(){keypad.addEventListener(keypadEvent); //Listen for a keypad Press}  void loop(){  char key = keypad.getKey();    if (key) {}       if (i==1){  ledStrip.setLed(j, 0, 0, 75);  // Turn on LED if Putton has been Pressed  }     else    ledStrip.setLed(j, 0, 0, 0);   // Else Turn LED Off        ledStrip.show();   // Send Data to LED Controller    }void keypadEvent(KeypadEvent key){         //What to do when a particular Key is pressed  switch (keypad.getState()){    case PRESSED:      switch (key){        case '1':  i = !i, j= 0; break;        case '2':  i = !i, j= 1; break;        case '3':  i = !i, j= 2; break;        case '4':  i = !i, j= 3; break;        case '5':  break;        case '6':  break;        case '7':  break;        case '8':  break;        case '9':  break;      }    break;  }    }`

#### robtillaart

#1
##### Mar 22, 2013, 08:37 pm

your indenting makes the program very hard to read. Then you miss code flaws like

if (key) {
}

if a key is pressed do nothing otherwise nothing. Probably not what you meant

Reformat your code so it better reflect the structure you want. TIP use CTRL-T to auto format your code in the IDE.

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up

Please enter a valid email to subscribe

### Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy