Everything runs fine except when i divide the reaction time by 1000 i only get like 0.25.
I would like to get thousans of a second. Ex: 0.257 seconds instead of 0.25 seconds. I have tried different data
types for the reaction_timeGreen and reaction_timeRed and so far my best result is with float.
//#include <Arduino.h>
int stageLeds1 = 13;
int stageLeds2 = 12;
int yellowLed1 = 11;
int yellowLed2 = 10;
int yellowLed3 = 9;
int greenLed = 8;
int redLed = 7;
int reactionButton = 16;
int elapsed_time = 0;
float reaction_timeGreen = 0;
float reaction_timeRed = 0;
#define reactionButton 2
// macro for detection of rising edge and debouncing
/*the state argument(which must be a variable) records the current
*and the last 7 reads by shifting one bit to the left at each read.
*if the value is 15(=0b00001111)we have one rising edge followed by
*4 consecutive 1's.That would qualify as a rising edge*/
#define DRE(signal, state) (state=(state<<1)|signal)==B00001111
// Rising state variables for each button
byte reactionButtonRisingState;
// macro for detection of falling edge and debouncing
/*the state argument(which must be a variable) records the current
*and the last 7 reads by shifting one bit to the left at each read.
*if the value is 240(=0b11110000)we have one falling edge followed by
*4 consecutive 0's.That would qualify as a falling edge*/
#define DFE(signal, state) (state=(state<<1)|signal)==B11110000
// Falling state variables for each button
byte reactionButtonFallingState;
void setup() {
// Set pin modes
Serial.begin(9600);
pinMode(stageLeds1, OUTPUT);
pinMode(stageLeds2, OUTPUT);
pinMode(yellowLed1, OUTPUT);
pinMode(yellowLed2, OUTPUT);
pinMode(yellowLed3 ,OUTPUT);
pinMode(greenLed ,OUTPUT);
pinMode(redLed ,OUTPUT);
pinMode(reactionButton ,INPUT_PULLUP);
}
boolean readButtons(){
// Read button states every 5ms(debounce time)
static unsigned long lastDebounce;
if (millis(); - lastDebounce >=5){
lastDebounce = millis();}
// Rising edge(if switch is released)
if (DRE(digitalRead(reactionButton) ,reactionButtonRisingState)){
//Serial.print("Rising edge (pulled high by internal pullup resistor). State variable: ");
//Serial.println(reactionButtonRisingState);
}
// Rising edge(if switch is released)
if (DFE(digitalRead(reactionButton) ,reactionButtonFallingState)){
// Serial.print("Falling edge (pulled low by switch). State variable: ");
//Serial.println(reactionButtonFallingState);
}
}
void loop() {
// Read Buttons
readButtons();
unsigned long int current_millis = millis();
unsigned long int previous_millis = 0;
unsigned long int new_millis = (current_millis - previous_millis);
unsigned long int elapsed_time = millis();
if ((new_millis >= 1000.) && (new_millis <= 2000.))
{
digitalWrite(stageLeds1 ,HIGH); // First stage lights On
}
if ((new_millis >= 2000.) && (new_millis <= 3000.))
{
digitalWrite(stageLeds2 ,HIGH); // Second stage ligths On
}
if ((new_millis >= 3000.) && (new_millis <= 3500.))
{
digitalWrite(yellowLed1 ,HIGH); // First yellow On and start elapsed time
}
if ((new_millis >= 3500.) && (new_millis <=4000.))
{
digitalWrite(yellowLed1 ,LOW); // First yellow Off
digitalWrite(yellowLed2 ,HIGH); // and Second yellow light On
}
if ((new_millis >= 4000.) && (new_millis <= 4500.))
{
digitalWrite(yellowLed2 ,LOW); // Second yellow Off
digitalWrite(yellowLed3 ,HIGH); // and third yellow light On
}
if ((new_millis >= 4500.) && (new_millis <=5000.))
{
digitalWrite(yellowLed3 ,LOW); // Third yellow Off
digitalWrite(greenLed ,HIGH); // Turn green light On
}
if ((reactionButtonRisingState == B00001111 && current_millis < 4500.)) //If reactionButton is pressed and reaction time is less then 4500 millis
{
digitalWrite(greenLed ,LOW); //Green light Off
digitalWrite(redLed ,HIGH); //Red light On
reaction_timeRed = (millis() - 4500.); //Millis - reaction time is stored in reaction_timeRed
Serial.print("You lose!!!");
Serial.println(reaction_timeRed/1000.); //Converts reaction_timeRed in seconds
Serial.print(millis());
}
if ((reactionButtonRisingState == B00001111 && current_millis >= 4500.)) //If reactionButton is pressed and reaction time is more then 4500 millis
{
digitalWrite(greenLed , HIGH); //Green light On
reaction_timeGreen = (millis() - 4500.); //Millis - reaction time is stored in reaction_timeGreen
Serial.print("You Win!!");
Serial.println(reaction_timeGreen /1000.); //Converts reaction_timeGreen in seconds
Serial.print(millis());
}
}
From Serial.println() - Arduino Reference
Serial.println(val, format)
Parameters
Serial: serial port object. See the list of available serial ports for each board on the Serial main page.
val: the value to print. Allowed data types: any data type.
format: specifies the number base (for integral data types) or number of decimal places (for floating point types).
The default number of decimal places is 2.
pert:
From https://www.arduino.cc/reference/en/language/functions/communication/serial/println/The default number of decimal places is 2.
So if i read properlly what you where kind enough to point to me. Then 2 numbers after the decimal is applicable for the serial communicaton and the math still adds up in the arduino.
Yes, the second parameter of Serial.println() only controls how many decimal places are printed.
If you're curious, you can see the code here:
// Extract digits from the remainder one at a time
while (digits-- > 0)
{
remainder *= 10.0;
unsigned int toPrint = (unsigned int)remainder;
n += print(toPrint);
remainder -= toPrint;
}
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.