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:

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
