Hi, I have a problem.
I use a Mega and the folowing programm:
#include <TimerOne.h>
const int led = LED_BUILTIN;
const uint8_t bit_timings[2] = {116, 58};
const Uint8_t Number;
void setup(void)
{
pinMode(led, OUTPUT);
Timer1.initialize(500000);
Timer1.attachInterrupt(DoINT);
Serial.begin(57600);
}
int ledState = LOW;
volatile unsigned long blinkCount = 0;
void output_bit(uint8_t bit_val)
{
digitalWrite(In3, LOW);
digitalWrite(In4, HIGH);
delayMicroseconds(bit_timings[bit_val]);
digitalWrite(In3, HIGH);
digitalWrite(In4, LOW);
delayMicroseconds(bit_timings[bit_val]);
}
void DoInt(void)
{
if (ledState == LOW) {
ledState = HIGH;
blinkCount = blinkCount + 1; // increase when LED turns on
} else {
ledState = LOW;
}
digitalWrite(led, ledState);
for (i = 1; i < 16; i++) {
outputbit(1);
}
}
void loop(void)
{
unsigned long blinkCopy;
while (Serial.available() == 0 ) {
}
int Val = Serial.parseInt();
while (Serial.available() > 0 ) // While have something
{
int trash = Serial.read(); // Cleaner buffer
delayMicroseconds(10);
Number = Val;
}
}
When I use the funtion output_bit() in the loop it works perfect. But because i want to expand the loop section with some waiting for input etc. I would like to use a timer-interrupt.
Therefore I found the TimerOne.h and this makes the LED blinking. But the function output_bit() is not working correctly.
I think it's problem is the delay in the output_bit(). This timing is very preciese and it may be not work inside the timer-interrupt.
Has anyone a solution?
thanks Trebbie