1

I'm trying to turn on LEDs based on delays entered by keypad and the same for turning them off, but the LEDs are not responding. Here is the code I'm using:

#include <Keypad.h>

const byte ROWS = 4;//four Rows
const byte COLS = 3;//three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {13,10,9,8}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7,6,1}; //connect to the column pinouts of the keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


int x;
int y;
int z;
int e ; 
int w;
int r;
int Pin1 = A0;
int Pin2 = A1;
int Pin3 = A2;


void setup() {
  // initialize pins A0,A1 and A2 as an output.
  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);

}

void loop ()
{
  char key = kpd.getKey();

l1:  if (key)  // Check for a valid key.
  {
    switch(key)
    {
      case '1':
      w=1;
      break;
      case '2':
      w=2;
      break;
      case '3':
      w=3;
      break;
      case'4':
      w=4;
      break;
      case '5':
      w=5;
      break;
      case '6':
      w=6;
      break;
      case '7':
      w=7;
      break;
      case '8':
      w=8;
      break;
      case '9':
      w=9;
      break;
    }
  }
  else { goto l1;} // To guarantee that a key is pressed before moving on with the rest of code

  delay(w*1000*60); //To reach the time specified by the user before moving on
  digitalWrite(A2,HIGH);

l2: if (key)
{
  switch (key)
  {
    case '1':
        r=1;
        break;
        case '2':
        r=2;
        break;
        case '3':
        r=3;
        break;
        case '4':
        r=4;
        break;
        case '5':
        r=5;
        break;
        case '6':
        r=6;
        break;
        case '7':
        r=7;
        break;
        case '8':
        r=8;
        break;
        case '9':
        r=9;
        break;
      }
}
else {goto l2;} // To guarantee that a key is pressed before moving n with the rest of code

delay ((r-w)*1000*60); //To reach the time specified by the user before moving on
digitalWrite (A2,LOW);

l3: if (key)
{
  switch(key)
  {
    case '1':
        x=1;
        break;
        case '2':
        x=2;
        break;
        case '3':
        x=3;
        break;
        case '4':
        x=4;
        break;
        case '5':
        x=5;
        break;
        case '6':
        x=6;
        break;
        case '7':
        x=7;
        break;
        case '8':
        x=8;
        break;
        case '9':
        x=9;
        break;
      }
}

else { goto l3;} // To guarantee that a key is pressed before moving n with the rest of code
delay ((x-r)*1000*60);//To reach the time specified by the user before moving on
digitalWrite (A0,HIGH);

l4: if (key)
{
  switch (key)
  {
    case '1':
        y=1;
        break;
        case '2':
        y=2;
        break;
        case '3':
        y=3;
        break;
        case '4':
        y=4;
        break;
        case '5':
        y=5;
        break;
        case '6':
        y=6;
        break;
        case '7':
        y=7;
        break;
        case '8':
        y=8;
        break;
        case '9':
        y=9;
        break;
      }
}
else {goto l4;} // To guarantee that a key is pressed before moving n with the rest of code
delay((y-x)*1000*60); //To reach the time specified by the user before moving on
digitalWrite (A0, LOW );

l5:
if(key)
{
  switch(key)
  {
    case '1':
      z=1;
      break;
      case '2':
      z=2;
      break;
      case '3':
      z=3;
      break;
      case'4':
      z=4;
      break;
      case '5':
      z=5;
      break;
      case '6':
      z=6;
      break;
      case '7':
      z=7;
      break;
      case '8':
      z=8;
      break;
      case '9':
      z=9;
      break;
    }
}
else {goto l5;}// To guarantee that a key is pressed before moving n with the rest of code
delay ((z-y)*1000*60); //To reach the time specified by the user before moving on
digitalWrite(A1,HIGH);

l6: if (key)
{
  switch(key)
  {
    case '1':
        e=1;
        break;
        case '2':
        e=2;
        break;
        case '3':
        e=3;
        break;
        case '4':
        e=4;
        break;
        case '5':
        e=5;
        break;
        case '6':
        e=6;
        break;
        case '7':
        e=7;
        break;
        case '8':
        e=8;
        break;
        case '9':
        e=9;
        break;
  }
}
else{goto l6;}// To guarantee that a key is pressed before moving n with the rest of code
delay((e-z)*1000*60); //To reach the time specified by the user before moving on
digitalWrite (A1, LOW );

}

migrated from electronics.stackexchange.com Dec 13 '14 at 6:47

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

  • 1
    You'd probably find the code a lot easier to follow if you looked at some of the Arduino libraries, for example that last case statement could be replaced by toInt to convert a string to an integer. Goto statements are also best avoided and there's rarely any good reasons to use them. – PeterJ Dec 13 '14 at 6:46
  • @peterJ thanks for responding, but i used Goto statement so i guarantee that a key is pressed before moving on with the rest of code,, – A.M.H Dec 13 '14 at 7:48
  • 3
    Your goto statements do not guarantee anything because key is just read once during the whole loop(). There are logical flaws in your code, you'd better think it over from scratch and just forget those damn gotos. Goto is not needed in any Arduino program! – jfpoilpret Dec 13 '14 at 16:23
  • 1
    I would start with a much simpler design just to make sure you've got the fundamentals down. Can you turn any of the LEDs on in your program at all? Make them blink? – Daniel Dec 15 '14 at 4:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.