[int32_t frequency = 20;
#include <PWM.h>
#define state_button 2 // Pushbutton for driving the device into the differents states
#define enA 9 // PWM pin
int reading_state; // current reading from the input pin of the state button
int previous_state = LOW; // previous reading from the input pin of the state button
int state = 0;
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time_state = 0; // the last time the output pin was toggled
long time_ampl = 0;
long time_freq = 0;
long debounce = 300; // the debounce time, increase if the output flickers
long time_state0 = 0;
long time_state1 = 0;
long time_state2 = 0;
void setup() {
Serial.begin(9600);
pinMode(state_button, INPUT);
pinMode(enA, OUTPUT);
InitTimersSafe();
bool success = SetPinFrequencySafe(enA, frequency);
}
void loop() {
reading_state = digitalRead(state_button);
if(reading_state==HIGH && previous_state == LOW && millis()-time_state>debounce)
{
state++;
if(state > 2)
{
state = 0;
}
time_state = millis();
}
previous_state = reading_state;
//Serial.println(state);
if(state == 0 && millis() > 0) // State1: Soft Mode
{
time_state0 = millis(); // initial time
if (time_state0 % 10000 == 1000){
pwmWrite(enA,10);
Serial.println("01");
}
if (time_state0 % 10000 == 2000){
pwmWrite(enA,50);
Serial.println("02");
}
if (time_state0 % 10000 == 4000){
pwmWrite(enA,100);
Serial.println("03");
//time_state0 = 0;
}
if (time_state0 % 10000 == 7000){
pwmWrite(enA,150);
Serial.println("04");
}
}
millis()== 0;
if(state == 1 && millis() > 0) //State2: Intermediate mode
{
time_state1 = millis(); // initial time
if (time_state1 % 12000 == 1000){
pwmWrite(enA,10);
Serial.println("11");
}
if (time_state1 % 12000 == 2000){
pwmWrite(enA,50);
Serial.println("12");
}
if (time_state1 % 12000 == 4000){
pwmWrite(enA,100);
Serial.println("13");
//time_state0 = 0;
}
if (time_state1 % 12000 == 7000){
pwmWrite(enA,150);
Serial.println("14");
}
if (time_state1 % 12000 == 9000){
pwmWrite(enA,200);
Serial.println("15");
}
}
millis()== 0;
if(state == 2 && millis() > 0) // State3: Intense mode
{
time_state2 = millis(); // initial time
if (time_state0 % 12000 == 1000){
pwmWrite(enA,10);
Serial.println("21");
}
if (time_state2 % 12000 == 2000){
pwmWrite(enA,50);
Serial.println("22");
}
if (time_state2 % 12000 == 4000){
pwmWrite(enA,100);
Serial.println("23");
//time_state0 = 0;
}
if (time_state2 % 12000 == 7000){
pwmWrite(enA,150);
Serial.println("24");
}
if (time_state2 % 12000 == 9000){
pwmWrite(enA,200);
Serial.println("25");
}
}
millis()== 0;
}
]
Hi guys I have written a simple programm whcih have 3 states which can be changed by a button press.
three states have different pwm values which will loop with differernt time interval.
so for eg If the code is in 1 state the PWM values changes after time interval. I am using millis() for time record.
The problem is when I am changing the state the loop of 2 state is not starting from 0 it takes the previous time at start at specific position.
The output is like:
01
02
03
04
01
02 --- button pressed
12
13
14---- button pressed
24
25
21
22
What I want is:
01
02
03
04
11
12 -- button pressed
21
22
23 -- button pressed
01
02
03 --- button pressed
11
12
13
the loop should start from 1st if statment but is not working.
Can some one help Please.
Thanks alot