I need your help , thanks !

okay my problem is that i want to assign an input to a pin and that would be my output and my input would be from a keypad after that i want to clear and choose a delay … my first key press is to choose the load then clear and my second key press to choose the delay and then i will have an out put i choose with the delay i chose … thanks in advance

final_code.ino (1.85 KB)

Please edit your post and add code tags
Before the code, type
** **[code]** **

After the code, type
** **[/code]** **

So it looks like your code

It makes it easier for people to look at (not everybody always uses a PC while visiting this site :wink: )

i want to assign an input to a pin and that would be my output

What?

Are you saying that you are going to plug both an LED and a pushbutton into the same header socket on your arduino?

Actually - before anything else you should read the sticky post, particularly that bit that says you should use code tags when posting code in this forum.

-- EDIT --

No, I think that what you are saying is that you want to read an input from one pin and write it to another. Variables in your code are assigned. Hardware pins are read and written.

Having said that, what's the actual problem?

here is my code so far

Oh! I get it! I'm with you, now! You want us to write the rest of your code for you!

Ain't going to happen, kid.

If you have a specific problem or something you don't understand, this sub is the place to come. If you want someone else to write your code for you, got to the "Gigs and Collaborations" sub.

-- EDIT --

Having said that, maybe I can suggest how to go about it.

Each time through the loop, a key may or may not have just been pressed. But the process of a user hitting keyprpresses may take several seconds - thousands of passes through the loop().

This means that you need global variables that store where you are "up to now". You need some variables whose state allow you to record "the user has selected the load A, and the delay B, but they haven't selected an output yet". Ech time through the loop, you read the key. If a key was pressed, you refer to the state variables to work out what that keypress is about. If it was the final key of your three-key sequence, then you go ahead and do whatever it is and then you reset everything back to "waiting for that first keypress".

So your first keypress selects pin10, pin11, pin12 or pin13, the second one selects the delay?

First things first; if you start numbering variables, you’re often better of using an array. E.g.

int outputPins[] = {10, 11, 12, 13};

And you can initialise them in setup() using a loop

 for(int cnt=0;cnt<sizeof(outputPins) / sizeof(outputPins[0]);cnt++)
  {
    pinMode(outputPins[cnt], OUTPUT);
  }

And you can e.g. make pin 12 low using

  digitalWrite(outputPins[2], LOW);

Next you need to keep track in your code what you are entering on the keypad

void loop()
{
  // the mode that our code is in
  // 0 -> select pin
  // 1 -> select delay
  // 2 -> do the action
  // due to the use of the 'static' keyword it will be remembered
  // initialized to 'select pin'
  static byte mode = 0;


  // read the keypad
  char keypressed = myKeypad.getKey();
  if (mode == 0)
  {
    // handle keypress to select output pin
  }
  else if (mode == 1)
  {
    // handle keypress to select delay
  }
  else if (mode == 2)
  {
    // delay and write the outputs
  }
}

Now you can implement the blocks; if a step is finished, you will change the mode to the next one. First we need a variable to remember the pin that the user selects in mode 0 so when the mode changes to 1 it will not be lost.

void loop()
{
  // the mode that our code is in
  // 0 -> select pin
  // 1 -> select delay
  // 2 -> do the action
  // due to the use of the 'static' keyword it will be remembered
  // initialized to 'select pin' (0)
  static byte mode = 0;

  // remember the pin that the user selected; this will be the index in the outputPins array
  static byte selectedPin;

  // variable to store the delay time
  unsigned long delayTime = 0;

  // read the keypad
  char keypressed = myKeypad.getKey();
   ...
   ...

I also declared a variable that holds the selected delay time

Next the implementation for the 3 modes

  if (mode == 0)
  {
    // if valid pin selection
    if (keypressed >= '1' && keypressed <= '4')
    {
      // set the selected index in the outputPins array
      selectedPin = keypressed - '1';

      // update the mode
      mode = 1;
    }
  }

The characters ‘0’ … ‘9’ have the values 48 … 57; the code subtracts the value for ‘1’ (49) from keypressed to get to a number from 0…3 that can be used as the index in the outputPins array.

Once a valid keypress was detected, the mode is changed so in the next iteration of loop you can selet the delay.

  else if (mode == 1)
  {
    switch (keypressed)
    {
      case '1':
        delayTime = 5000;
        mode = 2;
        break;
      case '2':
        delayTime = 6000;
        mode = 2;
        break;
      case '3':
        delayTime = 7000;
        mode = 2;
        break;
      case '4':
        delayTime = 8000;
        mode = 2;
        break;
    }
  }

I used a switch statement which is basically an if/else if/else if/… The only thing that this part does is set the delay time based on the pressed key and next changes the mode to 2.

And the last part does the actual work setting the select output high.

  else if (mode == 2)
  {
    // delay
    delay(delayTime);

    // set all outputs low
    for (int cnt = 0; cnt < sizeof(outputPins) / sizeof(outputPins[0]); cnt++)
    {
      digitalWrite(outputPins[cnt], LOW);
    }
    // set the selected output pin high
    digitalWrite(outputPins[selectedPin], HIGH);

    // reset mode to 0 so we can go through the sequence again
    mode = 0;
    
  }

What is implemented is called a finite state machine; it has three states (modes).

The code can be improved but I think that this is easier for you to follow :wink:

I hope this helps you on the way.

@PaulMurrayCbr
I think you’re not quite fair; looking at the post count I think OP needs (and hopefully wants) to learn.

Thank you it really helped me , i appreciate your help , i would never have thought in it that way if it is not for your help , thank you