//respond to input from computer and write to serial monitor which button is at the top of the queue
if (Serial.available() > 0) { //if there is input from the serial monitor
if(!queue.isEmpty()) {
byte popped = queue.pop(); //pop the byte from the front of the queue and label it popped
Serial.print (popped); //print the value of 'popped' to the serial monitor
Serial.println (); // print end of line character.
}
else
Serial.println("No balloons here to pop, boss");
}
}
It would also appear that you've grasped the gist of the program's intended function.
Serial.print (popped); //print the value of 'popped' to the serial monitor
Serial.println (); // print end of line character.
The println() method prints what's in the parentheses AND the carriage return/line feed. So you could just do:
Serial.println (popped); //print the value of 'popped' to the serial monitor
Once something is sent to the serial port, every value on the stack will be popped and printed, not just the next one, since you don't actually read() the value sent. So, it doesn't go away, and Serial.available() will always report that there is one or more bytes to read. Might need to fix that.
Once something is sent to the serial port, every value on the stack will be popped and printed, not just the next one, since you don't actually read() the value sent. So, it doesn't go away, and Serial.available() will always report that there is one or more bytes to read. Might need to fix that.
Yes, thanks for that.
The circuit is built now and it was as you had said. I adjusted the read serial monitor code (at the end of my code) a bit, to take that into account. It seems to be working better now. I tested it through 30 queue items, which it reported accurately followed by a return to the 'no balloons...boss'. Hooray!
A small problem: it seems that once a button(the switches are fine) is pressed, it pops out of the queue a number of times, relative to the length of time I hold the button down for. How can I adjust this? by checking that button's number is already in the queue? Or somehow disabling the button for say 2 secs? My experience of time delay things is that the rest of the program will also be on hold during that time, which would be an issue.
#include <QueueArray.h>
#define SWI1 13 // switch1 connected to digital pin 13
#define SWI2 12 // switch2 connected to digital pin 12
#define BUT3 11 // button3 connected to digital pin 11
#define SWI4 10 // switch4 connected to digital pin 10
#define BUT5 9 // button5 connected to digital pin 9
//for communication with computer
int incomingByte = 0; // for incoming serial data
int state = 0; // to read the value of the serial input from my computer
//for reading buttons and switches
int s1val; //val will Be used to store the state of the input pin 13
int s1val2; //to check the state of the input pin is constant
int s1switchState; //variavle to hold the switch state
int s2val; //val will Be used to store the state of the input pin 12
int s2val2; //to check the state of the input pin is constant
int s2switchState; //variavle to hold the switch state
int b3val; //val will Be used to store the state of the input pin 11
int b3val2; //to check the state of the input pin is constant
int b3buttonState; //variavle to hold the button state
int s4val; //val will Be used to store the state of the input pin 10
int s4val2; //to check the state of the input pin is constant
int s4switchState; //variavle to hold the switch state
int b5val; //val will Be used to store the state of the input pin 9
int b5val2; //to check the state of the input pin is constant
int b5buttonState = 0; //variavle to hold the button state
QueueArray <byte> queue;
void setup()
{
Serial.begin(9600); // set up Serial library at 9600 bps
pinMode(SWI1, INPUT); //sets the digital pin as output
pinMode(SWI2, INPUT); //sets the digital pin as output
pinMode(BUT3, INPUT); //sets the digital pin as output
pinMode(SWI4, INPUT); //sets the digital pin as output
pinMode(BUT5, INPUT); //sets the digital pin as output
}
void loop() // run over and over again
{
//assign each button a queue position when pressed
s1val = digitalRead(SWI1); //read input value of SWI1 and store it
delay(10); // 10 milliseconds is a good amount of time
s1val2 = digitalRead(SWI1); // read the SWI1 input again to check for bounces
if (s1val == s1val2) { // make sure we got 2 consistant readings!
if (s1val != s1switchState) { // the button state of SWI1 has changed!
if (s1val == HIGH) { // check if the button is high
queue.push (1); //send a 1 to the queue
s1switchState = 1; //change the switchState so that it matches with the s1val
}
if (s1val == LOW) { // check if the button is low
queue.push (1); //send a 1 to the queue
s1switchState = 0; //change the switchState so that it matches the new s1val
}
}
}
s2val = digitalRead(SWI2); //read input value of SWI2 and store it
delay(10); // 10 milliseconds is a good amount of time
s2val2 = digitalRead(SWI2); // read the SWI2 input again to check for bounces
if (s2val == s2val2) { // make sure we got 2 consistant readings!
if (s2val != s2switchState) { // the button state of SWI2 has changed!
if (s2val == HIGH) { // check if the button is pressed
queue.push (2); //send a 2 to the queue
s2switchState = 1; //change the switchState to match the s2val
}
if (s2val == LOW) {
queue.push(2);
s2switchState = 0;
}
}
}
b3val = digitalRead(BUT3); //read input value of BUT3 and store it
delay(10); // 10 milliseconds is a good amount of time
b3val2 = digitalRead(BUT3); // read the BUT3 input again to check for bounces
if (b3val == b3val2) { // make sure we got 2 consistant readings!
if (b3val != b3buttonState) { // the button state of BUT3 has changed!
if (b3val == LOW) { // check if the button is pressed (for push buttons)
queue.push (3);
}
}
}
s4val = digitalRead(SWI4); //read input value of SWI4 and store it
delay(10); // 10 milliseconds is a good amount of time
s4val2 = digitalRead(SWI4); // read the SWI4 input again to check for bounces
if (s4val == s4val2) { // make sure we got 2 consistant readings!
if (s4val != s4switchState) { // the button state of SWI4 has changed!
if (s4val == HIGH) { // check if the button is pressed
queue.push (4);
s4switchState = 1;
}
if (s4val == LOW) { // check if the button is pressed
queue.push (4);
s4switchState = 0;
}
}
}
b5val = digitalRead(BUT5); //read input value of BUT5 and store it
delay(10); // 10 milliseconds is a good amount of time
b5val2 = digitalRead(BUT5); // read the BUT5 input again to check for bounces
if (b5val == b5val2) { // make sure we got 2 consistant readings!
if (b5val != b5buttonState) { // the button state of BUT5 has changed!
if (b5val == HIGH) { // check if the button is pressed (for push buttons)
queue.push (5);
b5buttonState = 0;
}
}
}
//respond to input from computer and write to serial monitor which button is at the top of the queue
if (Serial.available() > 0) { //if there is input from the serial monitor
{
state = Serial.read(); // used to read incoming data
switch(state)// see what was sent to the board
{
case '1': // if the the one was sent
if(!queue.isEmpty()) {
byte popped = queue.pop(); //pop the byte from the front of the queue and label it popped
Serial.println (popped); //print the value of 'popped' to the serial monitor
}
else
Serial.println("No balloons here to pop, boss");
}
}
}
}