I have a program in which when
Serial.print(((millis()-previousMillis)+((timeansw))*1000)/1000);
gets executed, the whole timeansw seems to break. The serial output seems to ignore that variable, and the countdown timer it is running stops counting. Here is all of my code. Why would it be doing this and how do I fix this?
#include <SoftwareSerial.h>
// These are the Arduino pins required to create a software seiral
// instance. We'll actually only use the TX pin.
const int softwareTx = 7;
const int softwareRx = 6;
SoftwareSerial s7s(softwareRx, softwareTx);
// constants won't change. Used here to set a pin number :
const int p1=2; //button 1
const int p2=3;
int p1answ=1;
int p2answ=1;
int reading1;
int previous1=HIGH;
int reading2;
int previous2=HIGH;
long p1time=0;
long p2time=0;
long debounce=200;
int timeansw=16;
int led1=10;
int led2=11;
//button 2
// Variables will change :
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change :
const long interval = 1000; // interval at which to blink (milliseconds)
void setup() {
pinMode(p1, INPUT_PULLUP); //buttons configured as inputs but with a pullup
pinMode(p2, INPUT_PULLUP);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
s7s.begin(9600);
Serial.begin(9600);
s7s.write(0x76);
s7s.print("-HI-");
delay(1000);
s7s.write(0x76);
Serial.println("HiQ buzzer practice program");
Serial.println("Written by Alex Rodgers 2016");
}
void loop() {
reading1=digitalRead(p1);
if (reading1 == LOW && previous1 == HIGH && p1answ == 1){
p1answ=2;
if (timeansw=0){
Serial.print("p1 is ");
Serial.print((millis()-previousMillis)/1000);
Serial.print(".");
Serial.print((millis()-previousMillis)%1000, 4);
Serial.print(" seconds late");
} else{
Serial.print("p1 is ");
Serial.print(((millis()-previousMillis)+((timeansw))*1000)/1000);
Serial.print(".");
Serial.print(((millis()-previousMillis)+((timeansw))*1000)%1000, 4);
Serial.print(" seconds early");
}
}
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval && timeansw>0) {
if (timeansw>0){
timeansw = timeansw-1;
}
previousMillis = currentMillis;// save the last time you blinked the LED
s7s.write(0x76); //clear display
s7s.write(0x79); //set cursor
s7s.write(0x2); //cursor position
s7s.write(timeansw/10); //write the tens place of time
if (timeansw >= 10){ //check if time left is more than 10
s7s.write(0x79); //set cursor
s7s.write(0x3); //cursor position
s7s.write(timeansw - 10); //write the one's place of time
} else{ //if time left is less than 10
s7s.write(0x79); //set cursor
s7s.write(0x3); //cursor position
s7s.write(timeansw); //set time
}
}
}