I've been using this code with the Arduino Uno for a while and wanted to use a Arduino compatible Pro Micro (no name). But when I try to start any of the 'programs' I've made the program stops/hangs.
It's supposed to start one of the programs corresponding with number of times pin 8 is pressed. When I press pin 8 X times the code recognizes this and it shows in the serial monitor the correct number. But when I press pin 14 to start the selected program the serial output is repeating this line over and over untill it hangs:
'Program nr: XX'
int redLED4 = 9;
int greenLED5 = 15;
int blinkLED = 16;
int keepAlive = A0;
int redVal = 220;
int lampeSjekkTid = 80;
int timer;
int venteTid = 15; //x sekunder før rødt lys tennes / klar
int redOnTime = 7000; //tiden i ms rødt lys lyser før seriestart/mellom duellskudd
int greenOnTime; //hentes fra program
int redOffTime; //hentes fra program
int numSerier; //hentes fra program
int cmdId; //ProgramId -> brukes i velgProgram()
String programNavn;
const int buttonPin = 8;
const int buttonPinStart = 14;
int buttonPushCounter = 0; // counter for the number of button presses
int buttonPushCounterStart = 0;
int buttonState = 0; // current state of the button
int buttonStateStart = 0;
int lastButtonState = 0; // previous state of the button
int lastButtonStateStart = 0;
void setup() {
// Definere LED som output
pinMode(redLED4, OUTPUT);
pinMode(greenLED5, OUTPUT);
pinMode(blinkLED, OUTPUT);
pinMode(keepAlive, OUTPUT);
// Initiere trykk knapp for programvelger
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buttonPinStart, INPUT_PULLUP);
//Start Serial kun for debugging
Serial1.begin(9600);
while (!Serial);
//Fancy blinking ved oppstart
lampeSjekk();
} //lukke setup
void(* resetFunc) (void) = 0;//reset funksjon @ address 0
void loop() {
digitalWrite(keepAlive, HIGH);
checkStatus();
} // lukker void loop
void showState() {
Serial.print("Program nr: ");
Serial.println(cmdId);
}
void nedTelling() {
delay(50);
timer = venteTid;
while (timer >= 1) {
Serial.println(timer);
digitalWrite(blinkLED, HIGH);
delay(300);
digitalWrite(blinkLED, LOW);
delay(700);
timer = timer - 1;
}
}
void velgProgram( int buttonPushCounter) {
//Start med det lengste tidsintervallet først
if (buttonPushCounter == 9) {
//RFP Trening 1,6 sek førsteskudd
cmdId = 9; //program id -> henter switch
showState();//debug seriell
checkStatus();//status på knapp
nedTelling();//teller ned til neste runde
finnProgram(); //kjører lysstyring
}
else if (buttonPushCounter == 8) {
//CODE REMOVED DUE TO MAX 9000 CHARS
}
else {
//ved feil valgt program indiker med lampesjekk
Serial.println("Reset ");
delay(140);
resetFunc(); //Kjører reset
}//lukker if trykk-tid
}
void checkStatus() {
buttonState = digitalRead(buttonPin);
buttonStateStart = digitalRead(buttonPinStart);
// sammenligner mot forrige tilstand (av/på)
if (buttonState != lastButtonState) {
// dersom tilstand har endret seg:
if (buttonState == HIGH) {
Serial.print("Antall trykk: ");
Serial.println(buttonPushCounter);
} else {
// Hvis tilstand er LOW har knappen gått fra på til av:
buttonPushCounter++;
}
// Delay for å unngå bouncing
delay(50);
}
// lagre tilstand til neste loop
lastButtonState = buttonState;
/*Aktivere valgt program*/
//Sammenligner mot forrige tilstand (av/på)
if (buttonStateStart != lastButtonStateStart) {
// dersom tilstand har endret seg:
if (buttonStateStart == HIGH && buttonPushCounterStart != 0) {
// start valgt program
//buttonPushCounter = buttonPushCounter -1;
velgProgram(buttonPushCounter);
Serial.println("Valgt program");
buttonPushCounter = 0;
} else {
buttonPushCounterStart++;
// Ingen program er aktivert ennå
Serial.println("Ikke valgt program");
//Serial.print("buttonPushCounterStart: ");
//Serial.println(buttonPushCounterStart);
}
delay(80);
}
// lagre tilstand til neste loop
lastButtonStateStart = buttonStateStart;
}
DFU_2.4_Leonardo.ino (6.94 KB)
lysStyring.ino (5.04 KB)
programData.ino (2.81 KB)
You appear to be using pin numbers above 13 to reference Analog Input pins. The Analog Input pins don't get the same numbers on all boards. The Leonardo/Pro Micro analog input pins start at 18, not at 14 like on the UNO. You should ALWAYS use the names: A0, A1, A2...
... and NEVER use the names 0, 1, 2... to refer to analog pins.
Thanks, but the problem seems to be when this function is executed:
void checkStatus() {
/*Leser programvelger knappens antall trykk*/
buttonState = digitalRead(buttonPin);
buttonStateStart = digitalRead(buttonPinStart);
// sammenligner mot forrige tilstand (av/på)
if (buttonState != lastButtonState) {
// dersom tilstand har endret seg:
if (buttonState == HIGH) {
Serial.print("Antall trykk: ");
Serial.println(buttonPushCounter);
} else {
// Hvis tilstand er LOW har knappen gått fra på til av:
buttonPushCounter++;
}
// Delay for å unngå bouncing
delay(50);
}
// lagre tilstand til neste loop
lastButtonState = buttonState;
/*Aktivere valgt program*/
//Sammenligner mot forrige tilstand (av/på)
if (buttonStateStart != lastButtonStateStart) {
// dersom tilstand har endret seg:
if (buttonStateStart == HIGH && buttonPushCounterStart != 0) {
// start valgt program
//buttonPushCounter = buttonPushCounter -1;
velgProgram(buttonPushCounter);
Serial.println("Valgt program");
buttonPushCounter = 0; //tømmer antall trykk på programvelger
} else {
buttonPushCounterStart++;
// Ingen program er aktivert ennå
Serial.println("Ikke valgt program");
//Serial.print("buttonPushCounterStart: ");
//Serial.println(buttonPushCounterStart);
}
// Delay to avoid bouncing
delay(80);
}
// store instance for next loop
lastButtonStateStart = buttonStateStart;
}
innertier:
Thanks, but the problem seems to be when this function is executed:
void checkStatus() {
/Leser programvelger knappens antall trykk/
buttonStateStart = digitalRead(buttonPinStart);
/Aktivere valgt program/
//Sammenligner mot forrige tilstand (av/på)
if (buttonStateStart != lastButtonStateStart) {
// dersom tilstand har endret seg:
if (buttonStateStart == HIGH && buttonPushCounterStart != 0) {
// start valgt program
//buttonPushCounter = buttonPushCounter -1;
velgProgram(buttonPushCounter);
Serial.println("Valgt program");
buttonPushCounter = 0; //tømmer antall trykk på programvelger
} else {
buttonPushCounterStart++;
// Ingen program er aktivert ennå
Serial.println("Ikke valgt program");
//Serial.print("buttonPushCounterStart: ");
//Serial.println(buttonPushCounterStart);
}
// Delay to avoid bouncing
delay(80);
}
// store instance for next loop
lastButtonStateStart = buttonStateStart;
}
Yes, and 14 is greater than 13:
const int buttonPinStart = 14;
On the Pro Micro, Pin 14 is PIN_SPI_MISO on the ICSP header, Not A0 like it is on the UNO. If your Start button is wired to A0 then using the number 14 will work on the UNO and not on the Pro Micro. Using the name A0 will work on both.
I'm sorry. I guess I'm not that good at describing the problem. The pins works, on the Pro Micro I have 14 and A0 is two different pins. But the problem is not the pins at all. The code gets executed when I press them but what works on the Uno does not work on this Pro Micro. If I press pin 8 4 times the serial output is:
Antall trykk: 1
Antall trykk: 2
Antall trykk: 3
Antall trykk: 4
So far, so good. But when I press pin 14 it outputs
'Ikke valgt program' which it should not and then a loooong row of:
Program nr: 4
Program nr: 4
Program nr: 4
Program nr: 4
Program nr: 4
Program nr: 4
Program nr: 4
Program nr: 4
Program nr: 4
Program nr: 4
Program nr: 4
This goes on until the Pro Micro stops. If I remove line #5 the code below works as it should 1 loop and then it stops:
else if (buttonPushCounter == 4) {
//Duell 5x3 sek
cmdId = 4; //program id -> henter switch
showState();//debug seriell
checkStatus();//status på knapp
nedTelling();//teller ned til neste runde
finnProgram(); //kjører lysstyring
}
So my guess is something goes wrong in the checkStatus() function somewhere
// bestemmer antall skudd basert på skyteprogram. Eks. duell har 999 runder, alle andre 1. Henter serieAntall fra valgt program
for (int numRunder = 1; numRunder <= numSerier; numRunder+1) {
Serial.print("Runder: ");
Did you get a warning for this mistake and fix it? "numRunder + 1" doesn't do anything. Did you mean "numRunder = numRunder + 1" (a.k.a "numRunder += 1" or "numRunder++")?
You have a recursion problem:
if (buttonStateStart == HIGH && buttonPushCounterStart != 0)
{
// start valgt program
//buttonPushCounter = buttonPushCounter -1;
velgProgram(buttonPushCounter);
Serial.println("Valgt program");
buttonPushCounter = 0; //tømmer antall trykk på programvelger
}
Notice that velgProgram() is being called by checkStatus()...
else if (buttonPushCounter == 4)
{
//Duell 5x3 sek
cmdId = 4; //program id -> henter switch
showState();//debug seriell
checkStatus();//status på knapp
nedTelling();//teller ned til neste runde
finnProgram(); //kjører lysstyring
}
...and checkStatus() is being called by velgProgram().
checkStatus() calls velgProgram() BEFORE it notes the new button state:
// lagre tilstand til neste loop
lastButtonStateStart = buttonStateStart;
When velgProgram() calls checkStatus() the 'lastButtonStateStart' has not been updated so it looks like another Start button press... which causes checkStatus() to call velgProgram() again.
To fix it:
Move these lines:
// lagre tilstand til neste loop
lastButtonStateStart = buttonStateStart;
to here:
//Sammenligner mot forrige tilstand (av/på)
if (buttonStateStart != lastButtonStateStart)
{
// lagre tilstand til neste loop
lastButtonStateStart = buttonStateStart;
// dersom tilstand har endret seg:
You should do the same on "lastButtonState = buttonState;", too.
Fantastic!! Thank you so much
I haven't had any errormessages and it worked on Uno. I guess I've looked at it so much that I became blind.