Hi! First of all thanks in advance for reading.
I'm taking first steps with Arduino. I'm trying to make a Simon Game (Please if it this already exist don't send me a link. i'm tryint to do it by my self).
To add new pin colors after succesful tries i've created a String to enlarge the led order. I choose string type because .charAt() function.
This is the relevant part of the code
int led[] = {9, 8, 7, 6};
int buttonPressed[4];
String randomStep; //string para manejar los random
unsigned int lastStep = 0;
void setup() {
Serial.begin(9600);
//Setup the LED :
for (int i = 0; i < (sizeof(led)/sizeof(int)); i++) {
pinMode(led[i], OUTPUT);
}
randomSeed(analogRead(0));
}
void loop() {
if (randomStep.length() == 0) {
// Startup Code //
}
if (randomStep.length() != lastStep) {
// [+++ here goes code with buttons ++++ Succesful attemp would increment lastStep
lastStep++;
}
}
} else {
//So here are my issues!!!
randomStep = randomStep + String(random(0,4));
for (int i=0; i < randomStep.length(); i++) {
int currentLed;
currentLed = (int)randomStep.charAt(i);
Serial.print(led[(char)currentLed]);
//if I send the variable to Serial it shows me 0, 1 2 or 3 which are the possible numbers
// but when I'm trying to get the ledPIN from led[] array, serial monitor shows me 14891 or 29281 (depending of value)
//I tried with (int) (long)
// using atoi always give me 0.
digitalWrite(led[currentLed],HIGH);
delay(500);
digitalWrite(led[currentLed],LOW);
delay(500);
}
}
}
What do I need to do to get the correct type to pass it to array as an array Index? Is there a better way to do this?
Please forgive my writing.
I'm not very sure if i'm explaining myself. English is not easy for me.
Regards
Rodrigo de los Santos