Push buttons Sequence

Hello Everyone,

I am working on a project of a programmable robot (coding robot for kids).
and to save some digital pins I use one analog pin (A0 in my case) for 7 pushbuttons each button has a value or range of analog reading as I set in the code to show if the button is pressed or not. ( i tested it and it works well).
I tried to make an array with a size of 10 to record the sequence of 4 buttons from the set (up, right, down, left buttons) then playback it when one of the buttons is pressed (go button in my case).
I need to return an array with 10 values like below:
For example: if the user pressed (up, up, right, up, left, up) so I need the array to be
SeqArr[]={'f', 'f','r','f','l','f','s','s','s','s'}
f: for forward
r: for turn right
l: for turn left
s: for stop
then I playback with the above sequence.

Please advise if I am in the right way or need to change my direction or learn new things to achieve my goal from this project.

Here is my circuit:
image
my circuit is like this but adding two buttons and 2 1k ohm resistors.
Here is my code:

#include "pitches.h"
int melody[] = {NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, NOTE_G3, NOTE_B3, NOTE_C4};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {4, 8, 8, 4, 4, 4, 4, 4};

#define BtnsPad A0
int BtnValue;
char SeqArr[10] = "";
int upcount = 0;
int rightcount = 0;
int downcount = 0;
int leftcount = 0;
int gocount = 0;


void setup() {
  pinMode(BtnsPad, INPUT);
  Serial.begin(115200);
}

void loop() {
  Recording();
  Playback();
}

/////////////////////Record/////////////////////////////////////////////////////////
void Recording() {
 for (int i = 0; i < sizeof(SeqArr); i++);

  int i = 0;
  while (i < sizeof(SeqArr)) {

    if (  analogRead(BtnsPad) > 620 &  analogRead(BtnsPad) < 660) {
      SeqArr[i] = 'f';
      Serial.println(SeqArr[i]);
      //delay(500);
      toneplay();
      while (  analogRead(BtnsPad) > 620 &  analogRead(BtnsPad) < 660) {
        //Do nothing
      }
    }

    else if (  analogRead(BtnsPad) > 720 &  analogRead(BtnsPad) < 750) {
      SeqArr[i] = 'b';
      Serial.println(SeqArr[i]);
      //delay(500);
      toneplay();
      while (  analogRead(BtnsPad) > 720 &  analogRead(BtnsPad) < 750) {
        //Do nothing
      }
    }

    else if (  analogRead(BtnsPad) > 770 &  analogRead(BtnsPad) < 810) {
      SeqArr[i] = 'r';
      Serial.println(SeqArr[i]);
      //delay(500);
      toneplay();
      while (  analogRead(BtnsPad) > 770 &  analogRead(BtnsPad) < 810) {
        //Do nothing
      }

    }
    else if (  analogRead(BtnsPad) > 680 &  analogRead(BtnsPad) < 710) {

      SeqArr[i] = 'l';
      Serial.println(SeqArr[i]);
      //delay(500);
      toneplay();
      while (  analogRead(BtnsPad) > 680 &  analogRead(BtnsPad) < 710) {

      }
    }
    else {
      SeqArr[i] = 'n';
    }
    i++;
  }

  while (! analogRead(BtnsPad)) {
    //Do nothing

  }
}



/////////////////PlayBack/////////////////////////////////////////////////////////
void Playback() {
  for (int i = 0; i < sizeof(SeqArr); i++) {
  if (analogRead(BtnsPad) > 910 & analogRead(BtnsPad) < 950) {
  char x = SeqArr[i];
  if (x == 'f') {
    Serial.println("Go Forward");
    while (x == 'f') {
      //Do Nothing
    }
  }
  else if (x == 'b') {
    Serial.println("Go Backward");
    while (x == 'b') {
      //Do Nothing
    }
  }
  else if (x == 'r') {
    Serial.println("Turn Right");
    while (x == 'r') {
      //Do Nothing
    }
  }
  else if (x == 'l') {
    Serial.println("Turn Left");
    while (x == 'l') {
      //Do Nothing
    }
  }
  else if (x == 's') {
    Serial.println("Stop");
    while (x == 's') {
      //Do Nothing
    }

  }
  
  }
  i++;
  }
}


/////////////////Reset SeqArr/////////////////////////////////////////////////////////
void Reset() {
  if (analogRead(BtnsPad) > 1010 & analogRead(BtnsPad) < 1040) {
    int n=0;
    for(n=0;n<sizeof(SeqArr);n++)SeqArr[n] = 0 ;
    Serial.println(SeqArr[n]);
  }


}

/////////////////Tone/////////////////////////////////////////////////////////
void toneplay() {
  tone(12, melody[5], 500);
  delay(200);
  tone(12, 0, 300);
  delay(500);
}




thanks

This construction will be halting the sketch for good.

1 Like

@Railroader Thanks for your reply. you are right I removed these while loops from Playback() function and the freeze issue disappeared but what I faced is the wrong result when I pressed the go button (its value : analogRead(BtnsPad) > 910 & analogRead(BtnsPad) < 950)
when the array from Record() function SeqArr={'f', 'f','r','f','l','f','s','s','s','s'}
the result shall be:
"Go Forward"
"Go Forward"
"Turn Right"
"Go Forward"
"Turn Left"
"Go Forward"
"Stop"
"Stop"
"Stop"
"Stop"

but what happened is totally different it gives me like this:
for first press on go button:
"b"
if i press again it gives:
"f"
if i press again it gives:
"Go Forward"
if i press again it gives:
"B"

and so on..

here is my code updated

type #include "pitches.h"
int melody[] = {NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, NOTE_G3, NOTE_B3, NOTE_C4};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {4, 8, 8, 4, 4, 4, 4, 4};

#define BtnsPad A0
int BtnValue;
char SeqArr[10] = "";
int upcount = 0;
int rightcount = 0;
int downcount = 0;
int leftcount = 0;
int gocount = 0;


void setup() {
  pinMode(BtnsPad, INPUT);
  Serial.begin(9600);
}

void loop() {
  Recording();
  Playback();
}

/////////////////////Record/////////////////////////////////////////////////////////
void Recording() {
  for (int i = 0; i < sizeof(SeqArr); i++);

  int i = 0;
  while (i < sizeof(SeqArr)) {

    if (  analogRead(BtnsPad) > 620 &  analogRead(BtnsPad) < 660) {
      SeqArr[i] = 'f';
      Serial.println(SeqArr[i]);
      //delay(500);
      toneplay();
      while (  analogRead(BtnsPad) > 620 &  analogRead(BtnsPad) < 660) {
        //Do nothing
      }
    }

    else if (  analogRead(BtnsPad) > 720 &  analogRead(BtnsPad) < 750) {
      SeqArr[i] = 'b';
      Serial.println(SeqArr[i]);
      //delay(500);
      toneplay();
      while (  analogRead(BtnsPad) > 720 &  analogRead(BtnsPad) < 750) {
        //Do nothing
      }
    }

    else if (  analogRead(BtnsPad) > 770 &  analogRead(BtnsPad) < 810) {
      SeqArr[i] = 'r';
      Serial.println(SeqArr[i]);
      //delay(500);
      toneplay();
      while (  analogRead(BtnsPad) > 770 &  analogRead(BtnsPad) < 810) {
        //Do nothing
      }

    }
    else if (  analogRead(BtnsPad) > 680 &  analogRead(BtnsPad) < 710) {

      SeqArr[i] = 'l';
      Serial.println(SeqArr[i]);
      //delay(500);
      toneplay();
      while (  analogRead(BtnsPad) > 680 &  analogRead(BtnsPad) < 710) {

      }
    }
    else {
      SeqArr[i] = 'n';
    }
    i++;
  }

  while (! analogRead(BtnsPad)) {
    //Do nothing

  }
}



/////////////////PlayBack/////////////////////////////////////////////////////////
void Playback() {

  if (analogRead(BtnsPad) > 910 & analogRead(BtnsPad) < 950) {
    for (int i = 0; i < sizeof(SeqArr); i++) {

      char x = SeqArr[i];
      if (x == 'f') {
        Serial.println("Go Forward");

      }
      else if (x == 'b') {
        Serial.println("Go Backward");

      }
      else if (x == 'r') {
        Serial.println("Turn Right");

      }
      else if (x == 'l') {
        Serial.println("Turn Left");

      }
      else if (x == 's') {
        Serial.println("Stop");


      }
      i++;
    }

  }
}


/////////////////Reset SeqArr/////////////////////////////////////////////////////////
void Reset() {
  if (analogRead(BtnsPad) > 1010 & analogRead(BtnsPad) < 1040) {
    int n = 0;
    for (n = 0; n < sizeof(SeqArr); n++)SeqArr[n] = 0 ;
    Serial.println(SeqArr[n]);
  }


}

/////////////////Tone/////////////////////////////////////////////////////////
void toneplay() {
  tone(12, melody[5], 500);
  delay(200);
  tone(12, 0, 300);
  delay(500);
}paste code here

Go back to the recording function. There are plenty of the same "while".
There's also no handling for that button >910 and <950.

this button >910 and <950 is to play the SeqArr, not for recording
firstly I'd like to record all instructions then play it when go button(>910 and <950 ) is pressed

Okey.
I'm not prepared to dry run Your code.
I suggest You use the excellent tool, the combination of Serial.print, in the code, and the serial monitor in the IDE to display the happening.
At every place something is recorded, Serial.print "X recorded" etc.
The same will be helpful for every button action.
Believe me, that's a tool I used during plenty of years being thrown into large and unknown systems trying to resolve malfunctioning code.

So what do you suppose S5 does? :roll_eyes:

@Paul_B
This is my actual circuit:

Each pushbutton has an analog value as below:

S1: 646
S2:690
S3:740
S4:793
S5:858
S6:933
S7:1023

So in my code, I made a range for each button for any deviation in the readings.

[quote="Paul_B, post:7, topic:876394"] '
So what do you suppose S5 does? :roll_eyes:
[/quote]

Looks like it did what is now the roll of S7, provide another distinct reading.

Although it looks like the OP has them numbered backwards?

a7

Ahem. :roll_eyes:

How does it provide a reading distinct from a 10k to ground?

Magic, no doubt. Cough.

Back to the drawing board… or voltage dividers 1.01 for me. THX.

a7

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