I have this code , I try to restart Timer5 when it out of time. But i can't, may you help me ?
#include <TimerFive.h>
#include <TimerOne.h>
#include <openGLCD.h>
#define OLDWAY
unsigned long timer5_started=0;
void setup() {
Serial.begin(9600);
Timer1.stop();
Timer1.detachInterrupt();
Timer1.initialize(500000);
Timer1.attachInterrupt(Timer1Handle);
Timer1.start();
Timer5.stop();
Timer5.detachInterrupt(); //detach interrupt
Timer5.initialize(100000);
Timer5.attachInterrupt(Timer5Handle);
Timer5.start();
///---
}
void loop() {
while(1){
Serial.println("loop...");
delay(500);
}
}
void Timer5Handle(){
timer5_started=millis();
Serial.println("timer 5...");
while(1){
int a=0;
}
}
void Timer1Handle(){
if(millis()-timer5_started>100000){
Serial.println("restart...");
Timer5.restart();
}
}
When Timer5 out of time, the Interupt is stop, how can i restart it ?
When Timer5 out of time, the Interupt is stop, how can i restart it ?
Well, I don't even understand what are you trying to do in that interrupt handler:
void Timer5Handle(){
timer5_started=millis();
Serial.println("timer 5...");
while(1){
int a=0;
}
}
However your interrupt handler is called in actual IRS, so everything else stops including all other Interrupts (including millis counting and delays). And because of infinite loop, it's forever. Interrupt handler should be as short as possible to ensure others are not missed or so.
Related
I have an application on to control ESP32 Light Dimmer from the temperature read from a temperature sensor. The rule is very simple, when my sensor reads the temperature of more than 27ºC the lamp should be turned off by the Dimmer. However, this is not happening.
What the code does is, when the system is turned on the lamp turns on and the temperature is read from time to time, but when the temperature exceeds 27ºC the dimmer does not turn off the lamp. I think it might be something I'm doing wrong in the zero_crosss_int routine, because when the temperature reaches its limit the message "TRIAC OFF" is displayed.
Below the code used.
#define ZERO_CROSS 2
#define DIMMER_CONTROL 4
int dimming=64;
float programedTemp = 27.0;
int halfDimming=128;
int maxDimming=64;
void power(void *parameters){
tempSensor.requestTemperaturesByIndex(0);
temp=tempSensor.getTempCByIndex(0);
if(temp<programedTemp){
dimming=maxDimming;
if(temp<(programedTemp-1.0)){
dimming=maxDimming;
} else if(temp<programedTemp){
dimming++;
}
} else if(temp>programedTemp+0.9){
dimming=128;
}else{
dimming=halfDimming;
}
delay(4000);
}
void zero_crosss_int() {
if(dimming>=128){
delayMicroseconds(8.333);
digitalWrite(DIMMER_CONTROL, LOW); // triac Off
Serial.println((String) "=====>>> TRIAC OFF <<<=====");
}else{
int dimtime = (65*dimming);
delayMicroseconds(dimtime); // Off cycle
digitalWrite(DIMMER_CONTROL, HIGH); // triac firing
delayMicroseconds(8.333);
digitalWrite(DIMMER_CONTROL, LOW); // triac Off
}
}
void setup() {
pinMode(DIMMER_CONTROL, OUTPUT);
pinMode(ZERO_CROSS, INPUT);
attachInterrupt(ZERO_CROSS, zero_crosss_int, RISING);
Serial.begin(115200);
xTaskCreatePinnedToCore(power,"controlDimm",10000,NULL,0,&mainsPower,0);
}
void loop() {
}
You're doing way too much in your interrupt handler. It's amazing that it isn't just crashing constantly... the only reason that's not happening is likely because you don't do anything in loop().
It's not safe to call most of the functions you're calling in the interrupt handler. Interrupt handlers need to run for as little time as possible - other interrupts may be locked out and they may interfere with the network stack and other housekeeping functions. You absolutely should not be calling delayMicroseconds() or any Serial methods in an interrupt handler, or spending any more time there than is absolutely necessary. Almost all ESP-IDF or Arduino Core functions are not safe to call from inside an interrupt handler - the interrupt handler may interrupt another call to them while they're in an inconsistent state, or may change hardware settings while they're in the middle of using that hardware.
Your code would be much better if you structured it like this:
volatile bool zero_crossing_flag = false;
void IRAM_ATTR zero_cross_int() {
zero_crossing_flag = true;
}
void loop() {
if(zero_crossing_flag) {
zero_crossing_flag = false;
if(dimming>=128){
delayMicroseconds(8.333);
digitalWrite(DIMMER_CONTROL, LOW); // triac Off
Serial.println((String) "=====>>> TRIAC OFF <<<=====");
}else{
int dimtime = (65*dimming);
delayMicroseconds(dimtime); // Off cycle
digitalWrite(DIMMER_CONTROL, HIGH); // triac firing
delayMicroseconds(8.333);
digitalWrite(DIMMER_CONTROL, LOW); // triac Off
}
}
}
The IRAM_ATTR attribute on zero_cross_int() tells the compiler that this code must always be available. If you write an interrupt handler without IRAM_ATTR it's basically luck if it executes properly.
Restructuring your code this way will probably not solve the problem you're asking about, but it will allow it to run in a stable, reproducible way, which it's unlikely to the way it's written now.
Use your ZCD input to generate an interrupt. In the ISR start a Timer which generates another interrupt within 0ms(Full power) to 10ms(completely off) of the ZCD interrupt.
I don't understand to Switch off the lamp why are you using TRIAC instead of a Relay.
I am using Arduino for the first time, my project consists of RF transmitter connected with arduino UNO and a RF receiver connected to Arduino Mega.
I'm try to send data from transmitter and print it on receiver serial using VirtualWire library and every thing is okey for this receiver code:
#include <VirtualWire.h>
int x=9;
int y=8;
int z=10;
int r=7;
void setup()
{
Serial.begin(9600);
pinMode(x,OUTPUT);
pinMode(y,OUTPUT);
pinMode(z,OUTPUT);
pinMode(r,OUTPUT);
vw_setup(2000);
vw_rx_start();
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // Non-blocking
{
int i;
// Message with a good checksum received, print it.
Serial.print("Got: ");
for (i = 0; i < buflen; i++)
{
Serial.print(buf[i], HEX);
Serial.print(' ');
}
Serial.println();
}
}
Then i add some if statments to run 2 motors (connected to x,y,z,r pins) based on recrived values :
#include <VirtualWire.h>
int x=9;
int y=8;
int z=10;
int r=7;
void setup()
{
Serial.begin(9600);
pinMode(x,OUTPUT);
pinMode(y,OUTPUT);
pinMode(z,OUTPUT);
pinMode(r,OUTPUT);
vw_setup(2000);
vw_rx_start();
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // Non-blocking
{
int i;
// Message with a good checksum received, print it.
Serial.print("Got: ");
for (i = 0; i < buflen; i++)
{
if (buf[i]==0x77)//Stop motors
{
digitalWrite(x,LOW);
digitalWrite(y,LOW);
digitalWrite(z,LOW);
digitalWrite(r,LOW);
}
else
{
if(buf[i]==0x80)//2 motors clockwise
{
digitalWrite(x,LOW);
digitalWrite(y,HIGH);
digitalWrite(z,HIGH);
digitalWrite(r,LOW);
}
if (buf[i]==0x90)//counter clockwise
{
digitalWrite(x,HIGH);
digitalWrite(y,LOW);
digitalWrite(z,LOW);
digitalWrite(r,HIGH);
}
}
}
Now the problem is that when motors is stop working and I am sending the values that will run it either with or counterclockwise the motor works in the right direction but then does not respond to any data sent.
In short, when the motor stops working and I send data, the receiver receives the values and runs the motor violin is required, but then for example if the motor was working clockwise and sent the order which is running counterclockwise or even stop work, it does not respond and continues to move It was.
I noticed that this bacause when motors runs this function returns false
vw_get_message(buf, &buflen)
But i don't no why!
In VirtualWire library every time you send a new character or a set of characters your buffer will be overwritten. So the problem in this program is with your for loop checking. It will work fine if you just use the following
For example if you are sending characters like 'A', 'B' etc then
if (vw_get_message(buf, &buflen))
{
if(buf[0]=='A')
{
//move forward
}
if(buf[0]=='B')
{
//move backward
}
.... and so on
Hope this helps
I'm trying to attach interrupts to the rising edge of a signal (PWM). However, the signal is somewhat noisy when it's HIGH which causes the code to register another interrupt when it should not. I obviously tried to fix this in my circuit but that's not quite working so I moved to the software part.
The question is how I can filter out interrupts within a given frequency range? I need to apply a lowpass filter so that the interrupts do not get triggered when the signal is HIGH. My idea was detach the interrupt for a given amount of time or simply ignore the interrupt if it happens within a certain time range.
I'm just not sure how to achieve this.
This is my code:
unsigned long tsend = 0;
unsigned long techo = 0;
const int SEND = 2;
const int ECHO = 3;
unsigned long telapsed = 0;
unsigned long treal = 0;
void setup() {
Serial.begin(115200);
Serial.println("Start");
pinMode(SEND, INPUT);
pinMode(ECHO, INPUT);
attachInterrupt(digitalPinToInterrupt(SEND), time_send, RISING);
attachInterrupt(digitalPinToInterrupt(ECHO), time_echo, RISING);
}
void loop() {
telapsed = techo - tsend;
if (telapsed > 100 && telapsed < 10000000) {
treal = telapsed;
Serial.println(treal);
}
}
void time_send() {
tsend = micros();
}
void time_echo() {
techo = micros();
}
Below is the signal (yellow) which has a lot of noise. I need to ignore the interrupts when the signal is HIGH. Here is an image of the PWM Signal
I would try the following:
#define DEBOUNCE_TIME 100
void time_send() {
static long last = micros() ;
if (last-tsend > DEBOUNCE_TIME)
tsend = last;
}
void time_echo() {
static long last = micros() ;
if (last-techo > DEBOUNCE_TIME)
techo = last;
}
And adjust DEBOUNCE_TIME until I get a satisfactory result.
const byte intrpt_pin = 18;
volatile unsigned int count = 0;
#define DEBOUNCE_TIME 5000
void isr()
{
cli();
delayMicroseconds(DEBOUNCE_TIME);
sei();
count++;
}
void setup()
{
pinMode(intrpt_pin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(intrpt_pin), isr, FALLING);
}
void loop()
{
}
cli() : Disables all interrupts by clearing the global interrupt mask.
sei() : Enables interrupts by setting the global interrupt mask.
So, basically this program will ignore all the interrupt that occurs between these two lines, that is for DEBOUNCE_TIME.
Check your your interrupt bouncing time and adjust DEBOUNCE_TIME accordingly for the best result.
I'm having trouble communicating between Arduino's over I2C. For some reason, the onReceive handler is only called once.
Master Code (sender):
#include <Wire.h>
#include "i2csettings.h" // defines address
void setup()
{
Wire.begin(I2C_MASTER_ADDRESS);
}
void loop()
{
Wire.beginTransmission(I2C_SLAVE_ADDRESS);
Wire.write(0x11);
Wire.endTransmission();
delay(1000);
}
Slave Code (receiver):
#include <Wire.h>
#include "i2csettings.h"
void takeAction(int);
void setup()
{
Serial.begin(9600);
Wire.begin(I2C_SLAVE_ADDRESS);
Wire.onReceive(takeAction);
}
void loop()
{}
void takeAction(int nBytes)
{
Serial.println("Action!");
}
The idea in this test-setup is to have the sender send a byte every second, and let the receiver act on this by printing a message. However, the message is only printed once. When I reset the Slave, it's printed again, but just once.
Any ideas where this may come from?
You have to make sure you read all the bytes from the stream.
Other wise it seems to block.
Make your event handler look like this. So you can call it multiple times.
void takeAction(int nBytes)
{
Serial.println("Action!");
while(Wire.available())
{
Wire.read();
}
return;
}
Using the alarm library, I don't get the alarm to work:
#include <Time.h>
#include <TimeAlarms.h>
void setup()
{
Serial.begin(9600);
while (!Serial)
{
;
}
setTime(8,29,0,1,1,10); // set time to 8:29:00am Jan 1 2010
Alarm.timerRepeat(15, Repeats);
}
void Repeats()
{
Serial.print("alarmed timer!");
digitalWrite(10,1);
}
void loop()
{
}
I suppose you are using this library.
If you read in the help, you can see this:
Normal Running Usage
Alarm.delay(milliseconds); Alarms and Timers are only checks and their
functions called when you use this delay function. You can pass 0 for
minimal delay. This delay should be used instead of the normal Arduino
delay(), for timely processing of alarms and timers.
so in order for the alarms to be called, you have to add this:
void loop(){
Alarm.delay(1000); // wait one second between clock display
}
If you prefer to check the alarm faster, you can use a lower delay (e.g. 100). You can also use 0, so the function doesn't block, but it is not mandatory for your application.
By the way, I THINK (so I can be wrong) that the call to setTime is used just by the other functions, not by the timer. So you can omit it. Moreover you missed the pinmode statement..
In the end.. Try this code and let us know.
#include <Time.h>
#include <TimeAlarms.h>
void setup()
{
Serial.begin(9600);
while (!Serial) ;
pinMode(10, OUTPUT);
Alarm.timerRepeat(15, Repeats);
}
void Repeats()
{
Serial.print("alarmed timer!");
digitalWrite(10,1);
}
void loop()
{
Alarm.delay(500);
}
Add Alarm.delay(0); this way your program won't freeze and your alarm will work...