Help with simple interruption project

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);

}

Hello,

Please use the following code for your purpose.

[code]//Frequency of the PWM signal on most pins is approximately 490 Hz.So,Please refer this page https://www.arduino.cc/en/Reference/AnalogWrite
int inButton = 2;

void setup()
{

Serial.begin(9600); //Serial just to check if the button is working

pinMode (inButton,INPUT);

}

void loop()
{

int button = digitalRead(inButton);

if (button == 1)
{ Serial.println (“Button Pressed”);
Serial.println (“Pulse ON”); //If button is pressed the Pulse is ON
pulse();

}
else{
Serial.println(“Pulse OFF”); //If button is relased the Pulse is OFF
stop();
}

}

void stop() // clearing Output
{
analogWrite (9,0);
analogWrite (10,0);

}

void pulse()
{
digitalWrite (9,153);
delay(15000);
analogWrite (9,0);
analogWrite (10,153);
delay(15000);
analogWrite (10,0);
}
[/code]

The input pin is 2 in our code please modify it according to your requirements and let us know if it works.

Thanks and Regards

OK, thanks! I will test it and will reply soon.

Hi everyone!

So, I tested it and it worked with a minor problem. When I release the button, the pulses just stop after the end of the pulse routine like, after 30 seconds. This is why I was using external interruption. I will try to include this, but thanks a lot for the code!