Hello. I wrote this simple USB communication program for my arduino UNO:
int waitPin = 13;
int ACK = 255;
int incByte;
int BLINK_DELAY = 250;
void setup(){
for (int i = 4; i < 9; i++){
pinMode(i, INPUT);
}
Serial.begin(9600);
}
void loop(){
//if any pin is HIGH send it to serial and wait for ACK
for (int i = 4; i < 9; i++){
if (digitalRead(i)){
Serial.println("Pin" + i);
waitForAcknowledgement();
break;
}
}
//delay(1000);
}
void waitForAcknowledgement(){
digitalWrite(waitPin, HIGH);
//wait for response
while (Serial.available() < 0){
delay(100);
}
incByte = Serial.read();
if (incByte != ACK){
blinkLed(10);
}
digitalWrite(waitPin, LOW);
}
//this will be used when no ACK recieved
void blinkLed(int times){
for (int i = 0; i < times; i++){
digitalWrite(waitPin, LOW);
delay(BLINK_DELAY);
digitalWrite(waitPin, HIGH);
delay(BLINK_DELAY);
}
}
So this should wait for any of pins 4 - 8 to be HIGH and then send a message about which pin is HIGH to Serial and wait for acknowledgement. If the incoming message is not the acknowledgement blink the LED. I hooked up all the pins to GND (next to pin 13 on the board). First time i started the program it kept occasionally sending "Pin " without any number(even though all the pins were held LOW), then about 3rd try it started sending garbage, and now its not sending anything. Do you have any idea what I have done wrong?
I dont really see any problems with my code. I purposely lock the program in the while loop while there is nothing in the Serial buffer (available() less than 0) and then do things when that has changed. Nevertheless i changed the code to this:
int waitPin = 13;
int ACK = 255;
int incByte;
int BLINK_DELAY = 250;
boolean waitForACK;
void setup(){
for (int i = 7; i < 9; i++){
pinMode(i, INPUT);
}
Serial.begin(9600);
waitForACK = false;
}
void loop(){
//if any pin is HIGH send it to serial and wait for ACK
if (waitForACK){
digitalWrite(waitPin, HIGH);
checkForAcknowledgement();
} else {
for (int i = 7; i < 9; i++){
if (digitalRead(i)){
Serial.println("Pin" + i);
waitForACK = true;
break;
}
}
}
}
void checkForAcknowledgement(){
if (Serial.available() > 0){
incByte = Serial.read();
if (incByte != ACK){
blinkLed(10);
} else {
digitalWrite(waitPin, LOW);
}
}
}
//this will be used when no ACK recieved
void blinkLed(int times){
for (int i = 0; i < times; i++){
digitalWrite(waitPin, LOW);
delay(BLINK_DELAY);
digitalWrite(waitPin, HIGH);
delay(BLINK_DELAY);
}
}
This code won't work well - if at all. There are delay()s all over the place.
void waitForAcknowledgement(){
digitalWrite(waitPin, HIGH);
//wait for response
while (Serial.available() < 0){
delay(100);
}
incByte = Serial.read();
if (incByte != ACK){
blinkLed(10);
}
digitalWrite(waitPin, LOW);
}
Use one of the examples in serial input basics. They all receive without blocking. And use millis() to manage your blink timing so that that does not block. The demo several things at a time illustrates that.
And shouldn't you have while (Serial.available() < 1) {
I dont really see any problems with my code. I purposely lock the program in the while loop while there is nothing in the Serial buffer (available() less than 0)
Do you understand what "less than zero" means ? Why would there ever be less than zero bytes available ? The smallest number of bytes available, that you can have, is zero.
michinyon:
Do you understand what "less than zero" means ? Why would there ever be less than zero bytes available ? The smallest number of bytes available, that you can have, is zero.
You cannot ever have -1 bytes available.
Oooh sorry, I thought that it returns -1 when there is nothing available, but now i see it doesnt make any sense.
Robin2:
I had missed the updated code in Reply #3, - but it still has delay() in the blinkLed() function
And it looks like there is no place in your code where waitForACK is set back to false.
...R
Derp. I added the reset for waitForACK variable at the end of checkForAcknowledgement(), and now the LED is on all he time, RX and TX diodes are blinking rapidly and the java program keeps spamming empty strings. As before it seems like some pins are high (even though i grounded them all) and Serial.println("Pin" + i); doesnt work at all.
Also the delays in blinkLED() are harmless, they only make the LED blink in case the board doesnt receive ACK, which never happens in my current setup.
And indeeed, Serial.println("Pin" + i); is not a vallid statement (at least not for what you try to do). You can just print 1 thing. Or a variable or a string. Change it to:
Serial.print("Pin");
Serial.println(i);
And locking a program somewhere to wait for response is usual not a good idea...
septillion:
Please repost the sketch if you made changes.
And indeeed, Serial.println("Pin" + i); is not a vallid statement (at least not for what you try to do). You can just print 1 thing. Or a variable or a string. Change it to:
Serial.print("Pin");
Serial.println(i);
And locking a program somewhere to wait for response is usual not a good idea...
Thank you! That was the biggest problem. With that fixed i narrowed down the pin that was giving me trouble. Its weird that the compiler didn't catch that line. Good to remember.