Hi everyone!
I’m having some trouble making this project I want. Here you go.
I need to activate two outputs alternately, for 15s each. But this will only start when a signal comes in a input I picked. So far so good. Then, I want these outputs looping forever doing this while this input is HIGH, until an external interrupt stops this loop right away. When the interruption comes, my input that enable the pulses (the two outputs) will go off, so the pulses won’t continue after the interruption go away (this will be made with external circuitry, no problem here).
So, I made a code and was testing it but something is not working. The outputs activate without my input signal, and when I make the interruption, they won’t start again either. I will put the code here, see if someone understood and can help me.
I simulated the enable input for the pulses and the external interruption with buttons.
==========================================================================
int inButton = 31;
void setup()
{
// put your setup code here, to run once:
TCCR2B = (TCCR2B & 0xF8) | 0x03; //Configuração frequencia PWM, TIMER 2, pinos 9 e 10
//This is to change the PWM frequency from 490Hz to 980Hz.
// I need this value of frequency
Serial.begin(9600); //Serial just to check if the button is working
pinMode (9,OUTPUT);
pinMode (10,OUTPUT);
pinMode (2,INPUT_PULLUP);
pinMode (8,INPUT);
pinMode (inButton,INPUT);
pinMode (33,INPUT);
attachInterrupt (0, stop, LOW); //The interruption will execute a routine to clear the outputs
}
void loop()
{
int button = digitalRead(inButton);
Serial.println (button);
while (button == 1); //I want the outputs working while the button is pressed
{ //With the interruption, the button will release and the outputs will stop
void pulse(); //Until I hold the button again
}
}
void stop() //This is for clearing the outputs
{
analogWrite (9,0);
analogWrite (10,0);
}
void pulse() //These are the pulses, looping forever
{ //Just stop with the interrupt
analogWrite (9,153);
delay(15000);
analogWrite (9,0);
delay(1000);
analogWrite (10,153);
delay(15000);
analogWrite (10,0);
delay(1000);
}