Timer1
I struggle with the use of Timer1. Can someone explain to me how I can get this loop () to run only once every 10 seconds end? Using Timer1. In relation to this example, it has no purpose. But I will verify that Timer1 works. When this program will flash then wait 8 seconds before the loop () runs again. And then I will be able to use this principle.
example:
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
#include <TimerOne.h>
void setup(void) {
.....
Timer1.initialize( 10000000 ); //period in microseconds to call Your function
Timer1.attachInterrupt( FunctionName ); //attach the Timer1 interrupt to Your function
......
}
void loop()
{
.......
}
void FunctionName()
{
.......
}
Thanks, I'll test this once I am back home. The PC I have here will not ouploade program. Its not the first time this PC is messing withe me. PC bach home is somewhat better
But it sesames right:)
But i actually think I have to use Timer3, because I am using Arduino Mega...
Do you know way this dose not work!?
In this code the LED should light up whene the button is HIGH.
And wen the button is LOW the LED should go dark after at least 10 seckounds......
But now when the button is low the LED still lights up!
//timer1 will interrupt at 1Hz
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
Timer1.initialize( 1000000 ); // 10 sek //period in microseconds to call Your function
Timer1.attachInterrupt( loop );
}
// the loop function runs over and over again forever
void loop() {
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
//timer1 will interrupt at 1Hz
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
Timer1.initialize( 10000000 ); // 10 sek //period in microseconds to call Your function
Timer1.attachInterrupt( loop );
}
// the loop function runs over and over again forever
void loop() {
buttonState = digitalRead(buttonPin);
digitalWrite(ledPin, LOW);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
I couldn't use the timer on the loop.
I had to do as the first elsabz told about making an owne FunctionName. I dident understand the importens of that....
I have struggled a while, so now I am very happy thank you everyone!!!
//timer1 will interrupt at 1Hz
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
Timer1.initialize( 10000000 ); // 10 sek //period in microseconds to call Your function
Timer1.attachInterrupt( FunctionName );
}
// the loop function runs over and over again forever
void loop() {
buttonState = digitalRead(buttonPin);
digitalWrite(ledPin, LOW);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
}
void FunctionName() {
buttonState = digitalRead(buttonPin);
digitalWrite(LED_BUILTIN, LOW);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(LED_BUILTIN, HIGH);