My design consists of two rotary switches set up like push buttons, three push buttons ('yes', 'no' for dial A and 'go' for dial B). Attached to a thermal printer. When turn to Question one on dial A the user is given a question, I want them to be able to answer yes or no accordingly to each individual question, they will be rewarded a point if they answer negatively and this will be printed out. Once they have answered the questions they will move dial B to the amount of points they have tallied, then press go and be given the relevant advice.
As i have the rotary switch wired like individual push buttons i had to incorporate state change detection so that the function only printed once.
the state change detection is stopping the statement at the very bottom of the code from working.
is it possible to drop the state change detection and make it so that the statement only prints once. if i do this i can make the buttons print individual statements when rotary high and push button high.
have a look at my code and let me know what you think
#include "Adafruit_Thermal.h"
#include "SoftwareSerial.h"
#define TX_PIN 6 // Arduino transmit YELLOW WIRE labeled RX on printer
#define RX_PIN 5 // Arduino receive GREEN WIRE labeled TX on printer
SoftwareSerial mySerial(RX_PIN, TX_PIN); // Declare SoftwareSerial obj first
Adafruit_Thermal printer(&mySerial); // Pass addr to printer constructor
int buttonState = 0; //variable for reading pushbuttonstate or current state of button
int buttonState2 = 0; //variable for reading pushbuttonstate or current state of button
int buttonState3 = 0; //variable for reading pushbuttonstate or current state of button
int buttonState4 = 0; //variable for reading pushbuttonstate or current state of button
int buttonState5 = 0; //variable for reading pushbuttonstate or current state of button
int buttonState6 = 0; //variable for reading pushbuttonstate or current state of button
int buttonState7 = 0; //variable for reading pushbuttonstate or current state of button
int buttonState8 = 0; //variable for reading pushbuttonstate or current state of button
int buttonState9 = 0; //variable for reading pushbuttonstate or current state of button
int buttonState10 = 0; //variable for reading pushbuttonstate or current state of button
int buttonStateYes = 0; //variable for reading pushbuttonstate or current state of button
int buttonStateNo = 0; //variable for reading pushbuttonstate or current state of button
int buttonPushCounter = 0; //counter number of times pressed
int buttonPushCounter2 = 0; //counter number of times pressed
int buttonPushCounter3 = 0; //counter number of times pressed
int buttonPushCounter4 = 0; //counter number of times pressed
int buttonPushCounter5 = 0; //counter number of times pressed
int buttonPushCounter6 = 0; //counter number of times pressed
int buttonPushCounter7 = 0; //counter number of times pressed
int buttonPushCounter8 = 0; //counter number of times pressed
int buttonPushCounter9 = 0; //counter number of times pressed
int buttonPushCounter10 = 0; //counter number of times pressed
int lastButtonState = 0; // previous state of button
int lastButtonState2 = 0; // previous state of button
int lastButtonState3 = 0; // previous state of button
int lastButtonState4 = 0; // previous state of button
int lastButtonState5 = 0; // previous state of button
int lastButtonState6 = 0; // previous state of button
int lastButtonState7 = 0; // previous state of button
int lastButtonState8 = 0; // previous state of button
int lastButtonState9 = 0; // previous state of button
int lastButtonState10 = 0; // previous state of button
int lastButtonStateYes = 0; // previous state of button
const int buttonPin1 = 2; //number of pushbutton pin
const int buttonPin2 = 3; //number of pushbutton pin
const int buttonPin3 = 4; //number of pushbutton pin
const int buttonPin4 = 5; //number of pushbutton pin
const int buttonPin5 = 7; //number of pushbutton pin
const int buttonPin8 = 10; //number of pushbutton pin
const int buttonPin9 = 11; //number of pushbutton pin
const int buttonPin10 = 12; //number of pushbutton pin
const int buttonPin11 = 13; //number of pushbutton pin
const int buttonPin12 = 14; //number of pushbutton pin
const int buttonPinYes = 8; //number of pushbutton pin
const int buttonPinNo = 9; //number of pushbutton pin
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void setup() {
pinMode(7, OUTPUT); digitalWrite(7, LOW);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(5, INPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);
pinMode(10, INPUT);
pinMode(11, INPUT);
pinMode(12, INPUT);
pinMode(13, INPUT);
pinMode(14, INPUT);
mySerial.begin(9600);
printer.begin();
//always print at start
printer.justify('C');
printer.setSize('L');
printer.println(F("S.A.H.D"));
printer.justify('C');
printer.setSize('S');
printer.println(F("Stoic Art of Happiness Device"));
printer.feed(1);
printer.justify('L');
printer.setSize('S');
printer.println(F("One in four people suffer from\ndepression. It effects us all\nwether it be at home or on a\nsocietal level.\n\nYou will be asked several\nquestions to find a Stoic\nMeditation best suited to you.\n\nTurn dial A to Q1 then answer\nyes or no accordingly, repeat."));
printer.println(F("________________________________"));
printer.sleep(); // Tell printer to sleep
// delay(3000L); // Sleep for 3 seconds
// printer.wake(); // MUST wake() before printing again, even if reset
// printer.setDefault(); // Restore printer to defaults
}
////-----------------------------------------------------------------------------------------------------------------------------------------------------------------
void loop() {
buttonState = digitalRead(buttonPin1);
if (buttonState != lastButtonState){
if (buttonState == HIGH){
buttonPushCounter++;
printer.setSize('M');
printer.println(F("Q1"));
printer.justify('L');
printer.setSize('S');
printer.println(F("Do you prefer to stay at home\nrather than going out to\nsocialise and try new things?"));
printer.feed(1);
lastButtonState = buttonState;
if (buttonPushCounter % 1 == 0){
digitalWrite(7, HIGH);}
}
}
buttonState2 = digitalRead(buttonPin2);
if (buttonState2 != lastButtonState2){
if (buttonState2 == HIGH){
buttonPushCounter++;
printer.setSize('M');
printer.println(F("Q2"));
printer.justify('L');
printer.setSize('S');
printer.println(F("Are you satisfied with your\nlife, do you often feel content?"));
printer.feed(1);
lastButtonState2 = buttonState2;
if (buttonPushCounter2 % 1 == 0){
digitalWrite(7, HIGH);}
}
}
buttonState4 = digitalRead(buttonPin4);
if (buttonState4 != lastButtonState4){
if (buttonState4 == HIGH){
buttonPushCounter++;
printer.setSize('M');
printer.println(F("Q4"));
printer.justify('L');
printer.setSize('S');
printer.println(F("Are you affraid that something\nbad is going to happen to you?"));
printer.feed(1);
lastButtonState4 = buttonState4;
if (buttonPushCounter4 % 1 == 0){
digitalWrite(7, HIGH);}
}
}
buttonState5 = digitalRead(buttonPin5);
if (buttonState5 != lastButtonState5){
if (buttonState5 == HIGH){
buttonPushCounter++;
printer.setSize('M');
printer.println(F("Q5"));
printer.justify('L');
printer.setSize('S');
printer.println(F("Do you feel happy most of the\ntime?"));
printer.feed(1);
printer.justify('L');
printer.setSize('S');
printer.println(F("Now tally your awarded points\nand turn dial B to the\ncorrosponding notch then press\nprint"));
printer.feed(1);
printer.println(F("________________________________"));
lastButtonState5 = buttonState5;
if (buttonPushCounter5 % 1 == 0){
digitalWrite(7, HIGH);}
}
}
}
buttonStateYes = digitalRead(buttonPin8);
buttonState = digitalRead(buttonPin1);
if ( buttonState == HIGH && buttonStateYes == HIGH) {
printer.setSize('L');
printer.println(F("Stoic Philosophy"));
}
}