Good evening all!
New on this forum, I hope i don't post at the wrong location!
I want to make a dummy EV charger. This is based on triggers on the 5V and a potentiometer to get to the specific state (on pin A2). This works and I get into the correct states.
My issue is the part where the BUZZER is triggered on a statechange. This buzzer needs to run for 5 seconds (500ms) and then be turned off.
I did put up a variable called previousState to keep track of the old state. This is compared by the current state. If its not the same, it's a statechange and means I want to trigger the buzzer and change some leds.
This same function is also taking the current millis() into currentMillis to keep track when the buzzer started.
I've put the timer itself inside the statechange function. I have tried it outside that function but the same results appear.
The 'buzzer' doesnt turn itself off after 500ms. It will if i keep changing the states, the first time its 500ms later than the first statechange, it actually does turn off.
Anyone know what i am missing or doing wrong in my thought process? I really appreciate any help that might push me into the right direction!
Specific code:
if (state != previousState)
{
// Update leds and turn the buzzer on because state has changed
updateLeds(state);
//digitalWrite(13,HIGH);
Serial.println("Start Buzzing");
// Start timer to keep track of how long the buzzer is on
currentMillis = millis();
Serial.print("begin buzzing: "); Serial.println(currentMillis);
if (currentMillis - previousMillis >= interval)
{
Serial.println("Buzzer off!");
Serial.print("end buzzing: "); Serial.println(currentMillis);
previousMillis = currentMillis;
previousState = state;
}
}
Full code:
char state = '\0';
char previousState = '\0';
void updateLeds();
const long interval = 500;
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
bool ledofftime = false;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
//pinMode(13,OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A2);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
//Serial.println(sensorValue);
if (5 >= voltage && voltage > 3.75)
{
state = 'A';
}
// If voltage = 3.75 -> state B (9 / 12 * 5 = 3.75)
else if (3.75 >= voltage && voltage > 2.5)
{
state = 'B';
}
// If voltage = 2.50 -> state C (6 / 12 * 5 = 2.50)
else if (2.5 >= voltage && voltage > 1.25)
{
state = 'C';
}
// If the state is changed (AKA not the same as previous)
if (state != previousState)
{
// Update leds and turn the buzzer on because state has changed
updateLeds(state);
//digitalWrite(13,HIGH);
Serial.println("Start Buzzing");
// Start timer to keep track of how long the buzzer is on
currentMillis = millis();
Serial.print("begin buzzing: "); Serial.println(currentMillis);
if (currentMillis - previousMillis >= interval)
{
Serial.println("Buzzer off!");
Serial.print("end buzzing: "); Serial.println(currentMillis);
previousMillis = currentMillis;
previousState = state;
}
}
}
void updateLeds(char state)
{
switch (state)
{
case 'A':
{
Serial.println("A!");
break;
}
case 'B':
{
Serial.println("B!");
break;
}
case 'C':
{
Serial.println("C!");
break;
}
default: { return; } // If its not A,B or C -> reject.
}
}