Introduction: I am a first timer programming an Arduino. I have basic programming knowledge, nothing modern.
The reason for getting a few Arduino's besides having fun is that I'm doing the CarbageRun again next year (february 2021, driving to the polar circle!), and want to automate some stuff. My first goal is to play some custom melodies on our 6-tone-trainhorn. The trainhorns are controlled by solenoids.
The basic scheme I'm aiming for: Arduino-with-buttons --> 6-relaisboard --> 12v solenoids.
When I press button A, play melody A, etcetera.
I've written some basic code for the purpose of testing. I'll use the 6 Analog-in's as OUTPUT for controlling the relaisboard (matches perfect with the number of horns to control). For the testcase I've wired up 2 LEDs to A0 and A1, and two push-buttons to pin 3 & 4 (and did all the necessary stuff like pulldown resistors etc).
(The code, I can imagine, could be done better - for example: is there a way to make a variable-variable? For example, have several variables named Var1, Var2, Var3 etc and then call them in a loop by "Var"&i or something similar?)
Anyhow, the problem. In the code below, when I FIRST press the white button, it perfectly plays the white 'melody' (in this case, blinks my 2 test-LED's in the correct way). If I then press the red button, that one plays perfectly as well. If I would push the white button again, it plays the 'white button melody', and then the last note of the 'red button melody'. Also, iIt does not matter which values I enter for "SongLength" - if I would make SongLength=3 for the red-button-melody it would still play out all the notes, meaning that in the for-loop i goes all the way up untill at least 8 before exiting.
What obvious thing am I missing here?
const int buttonredPin = 3; // The red button
const int buttonwhiPin = 4; // The white button
const int Tone1 = A0;
const int Tone2 = A1;
const int Tone3 = A2;
const int Tone4 = A3;
const int Tone5 = A4;
const int Tone6 = A5;
const int Tone0 = 13; // internal LED
int buttonredState = 0; // variables for reading the pushbutton status
int buttonwhiState = 0;
int SongLength = 0;
int Note1 = 0; // variables for holding the tone to play
int Note2 = 0;
int Note3 = 0;
int Note4 = 0;
int Note5 = 0;
int Note6 = 0;
int Note7 = 0;
int Note8 = 0;
int Note9 = 0;
int Note10 = 0;
int Note11 = 0;
int Note12 = 0;
int Note13 = 0;
int Note14 = 0;
int Note15 = 0;
int Note16 = 0;
int Note17 = 0;
int Note18 = 0;
int Note19 = 0;
int Note20 = 0;
int NoteL1 = 0; // variables for holding how long the tone should play
int NoteL2 = 0;
int NoteL3 = 0;
int NoteL4 = 0;
int NoteL5 = 0;
int NoteL6 = 0;
int NoteL7 = 0;
int NoteL8 = 0;
int NoteL9 = 0;
int NoteL10 = 0;
int NoteL12 = 0;
int NoteL13 = 0;
int NoteL14 = 0;
int NoteL15 = 0;
int NoteL16 = 0;
int NoteL17 = 0;
int NoteL18 = 0;
int NoteL19 = 0;
int NoteL20 = 0;
int NoteNow = 0; // the current tone to play
int NoteLNow = 0; // the length of that current tone
int i; // the loop integer
void setup() {
pinMode(buttonredPin, INPUT);
pinMode(buttonwhiPin, INPUT);
pinMode(Tone1, OUTPUT);
pinMode(Tone2, OUTPUT);
pinMode(Tone3, OUTPUT);
pinMode(Tone4, OUTPUT);
pinMode(Tone5, OUTPUT);
pinMode(Tone6, OUTPUT);
pinMode(Tone0, OUTPUT); // internal LED
}
void loop() {
buttonredState = digitalRead(buttonredPin);
buttonwhiState = digitalRead(buttonwhiPin);
if (buttonredState == HIGH) {
SongLength = 8;
Note1 = Tone1;
NoteL1 = 250;
Note2 = Tone0;
NoteL2 = 250;
Note3 = Tone1;
NoteL3 = 250;
Note4 = Tone0;
NoteL4 = 250;
Note5 = Tone1;
NoteL5 = 250;
Note6 = Tone0;
NoteL6 = 250;
Note7 = Tone2;
NoteL7 = 2000;
Note8 = Tone0;
NoteL8 = 2000;
goto play;
} else if (buttonwhiState == HIGH) {
SongLength = 3;
Note1 = Tone1;
NoteL1 = 100;
Note2 = Tone2;
NoteL2 = 100;
Note3 = Tone1;
NoteL3 = 100;
Note4 = Tone2;
NoteL4 = 100;
Note5 = Tone0;
NoteL5 = 1000;
goto play;
} else {
SongLength = 0;
}
play:
for (i = 0; i < SongLength; i++) {
if (i = 1) {
NoteNow = Note1;
NoteLNow = NoteL1;
digitalWrite(NoteNow, HIGH);
delay(NoteLNow);
digitalWrite(NoteNow, LOW);
}
if (i = 2) {
NoteNow = Note2;
NoteLNow = NoteL2;
digitalWrite(NoteNow, HIGH);
delay(NoteLNow);
digitalWrite(NoteNow, LOW);
}
if (i = 3) {
NoteNow = Note3;
NoteLNow = NoteL3;
digitalWrite(NoteNow, HIGH);
delay(NoteLNow);
digitalWrite(NoteNow, LOW);
}
if (i = 4) {
NoteNow = Note4;
NoteLNow = NoteL4;
digitalWrite(NoteNow, HIGH);
delay(NoteLNow);
digitalWrite(NoteNow, LOW);
}
if (i = 5) {
NoteNow = Note5;
NoteLNow = NoteL5;
digitalWrite(NoteNow, HIGH);
delay(NoteLNow);
digitalWrite(NoteNow, LOW);
}
if (i = 6) {
NoteNow = Note6;
NoteLNow = NoteL6;
digitalWrite(NoteNow, HIGH);
delay(NoteLNow);
digitalWrite(NoteNow, LOW);
}
if (i = 7) {
NoteNow = Note7;
NoteLNow = NoteL7;
digitalWrite(NoteNow, HIGH);
delay(NoteLNow);
digitalWrite(NoteNow, LOW);
}
if (i = 8) {
NoteNow = Note8;
NoteLNow = NoteL8;
digitalWrite(NoteNow, HIGH);
delay(NoteLNow);
digitalWrite(NoteNow, LOW);
}
if (i = 9) {
NoteNow = Note9;
NoteLNow = NoteL9;
digitalWrite(NoteNow, HIGH);
delay(NoteLNow);
digitalWrite(NoteNow, LOW);
}
if (i = 10) {
NoteNow = Note10;
NoteLNow = NoteL10;
digitalWrite(NoteNow, HIGH);
delay(NoteLNow);
digitalWrite(NoteNow, LOW);
}
}
}