I feel like I understand arrays and how they work fairly well... used them a lot way back in my VB days. But I'm stumped here.
const int btn1 = 19;
const int btn2 = 18;
const int btn3 = 2;
const int btn4 = 3;
const int btnRst = 4;
const int lt1 = 8; //Setting a bunch of Pin Constants
const int lt2 = 9;
const int lt3 = 10;
const int lt4 = 11;
const int ltOk = 6;
const int ltErr = 5;
const int bzz = 7;int btn1State = 0;
int btn2State = 0;
int btn3State = 0; // initial setting of button states
int btn4State = 0;
int btnRstState = 0;int btnRstLastState = 0;
int btn1LastState = 0;
int btn2LastState = 0; // initial setting of last state for if statements
int btn3LastState = 0;
int btn4LastState = 0;int Level = 4; // initial "level" which will determine array size
void setup() {
pinMode(lt1, OUTPUT);
pinMode(lt2, OUTPUT);
pinMode(lt3, OUTPUT);
pinMode(lt4, OUTPUT);
pinMode(ltOk, OUTPUT);
pinMode(ltErr, OUTPUT);// Load(); // Load pretty display and sounds - commented out to speed up testing
Serial.begin(9600);
}
void loop() {
btn1State = digitalRead(btn1);
btn2State = digitalRead(btn2);
btn3State = digitalRead(btn3); // Check to see if anyone has pressed a button
btn4State = digitalRead(btn4);
btnRstState = digitalRead(btnRst);int rdmNum[Level]; // initialize the random number Array and assign it a size based on the "Level" (starts off 4)
if (btnRstState != btnRstLastState){ // I only want the state change and I don't want this to continue to loop while the button is still pressed
if (btnRstState == 1){
for (int i = 0; i < Level; i++){ // Loading the array with a random sequence of numbers 1 to 4
randomSeed(analogRead(0));
rdmNum* = random(1, 4);*
}*
}*
}*
btnRstLastState = btnRstState; // Save the state so I can detect the change next time around*
if (btn2State != btn2LastState){ // I only want the state change and I don't want this to continue to loop while the button is still pressed*
if (btn2State == 1){*
for (int i = 0; i < Level; i++){ // Testing to just see what was loaded in the array.*
_ Serial.println(rdmNum*);_
_ }_
_ Serial.println();
Serial.println(sizeof(rdmNum)); // more testing about Array_
_ }_
_ }_
_ btn2LastState = btn2State; // Save the state so I can detect the change next time around*_
* delay(150);*}
// Pretty lights and sounds for startup
void Load() {
* tone(bzz, 50, 400);*
* digitalWrite(lt1, HIGH);*
* digitalWrite(ltOk, HIGH); *
* delay(500);*
* digitalWrite(lt1, LOW);*
* digitalWrite(ltOk, LOW);*
* tone(bzz, 500, 400); //Good Error tone*
* digitalWrite(lt2, HIGH);*
* digitalWrite(ltErr, HIGH);*
* delay(500);*
* digitalWrite(lt2, LOW);*
* digitalWrite(ltErr, LOW);*
* tone(bzz, 1000, 400);*
* digitalWrite(lt3, HIGH);*
* digitalWrite(ltOk, HIGH);*
* delay(500); *
* digitalWrite(lt3, LOW);*
* digitalWrite(ltOk, LOW);*
* tone(bzz, 2000, 400); //Good Ok tone*
* digitalWrite(lt4, HIGH);*
* digitalWrite(ltErr, HIGH);*
* delay(500);*
* digitalWrite(lt4, LOW);*
* digitalWrite(ltErr, LOW);*
}
[/quote]
Quick and dirty explanation is this will be a Simon type of program/device... just trying to test my programming skills (coming up pretty short) for lack of parts to work on my real project right now.
What is happening is that my array size keeps coming up as "8" even though it should be 4 and then when I hit button 2 it spits out what looks like possibly the right numbers for 0,1,2 of the array but then when it hits the fourth spot (index 3) it spits out something odd like "5633". If I keep hitting button 2 it will kick out the same thing a few times then all of a sudden it starts giving odd random numbers like:
8448
130
30211
5633
I have my buttons debounced with 100nF caps, I also have 10k pull down resistors pulling down the digital input pin side to ground when in the off position. When testing it seemed to read find with a steady stream of correct states (that's why there is a lingering delay(150); in there).
I'll even get random negative numbers out of the array, like -19834!