0

I am trying to work with arduino timer1 interrupts to make an led blink every bit on an 8 bit number, depending on whether its a 1 or 0. However, im trying to make a basic blinking program, and it does not seem to be working.

#include "TimerOne.h"
bool val=0;
void setup()
{
 pinMode(13, OUTPUT);
 Timer1.initialize(500000); // initialize timer1, and set a 1/2 second period
 Timer1.attachInterrupt(callback); // attaches callback() as a timer overflow interrupt
}
void callback()
{
 val=1;
}
void loop()
{
 digitalWrite(13,val);
 while(!val);
 val=0;
}

The LED doesnt blink at all! Any help would be appreceitated! thanks!

Majenko
106k5 gold badges81 silver badges139 bronze badges
asked Oct 29, 2015 at 13:47

2 Answers 2

1

Try this instead.

#include "TimerOne.h"
bool val=0;
void setup()
{
 pinMode(13, OUTPUT);
 Timer1.initialize(500000); // initialize timer1, and set a 1/2 second period
 Timer1.attachInterrupt(callback); // attaches callback() as a timer overflow interrupt
}
void callback()
{
 val=!val;
}
void loop()
{
 digitalWrite(13,val);
}
answered Oct 29, 2015 at 18:17
0

I'm not surprised the LED doesn't blink - the entirety of your logic in the loop() function is so bizarre as to elicit the question:

WTF?

Let's take a look at it line by line:

digitalWrite(13,val);

Set the LED to whatever val is - val starts as 0, so the LED is off.

while(!val);

While val is not anything except 0, so while val is zero, do nothing. (The interrupt would escape you from this if your val was volatile)

val=0;

Set val to 0.

So how can val be anything except 0 when you do the digitalWrite?

answered Oct 29, 2015 at 15:07

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.