XBee and watchdog timer transmission not working - arduino

I've created a sketch that uses a watchdog timer that runs every eight seconds. I've used a counter to wait 64 seconds (about a minute).
In addition, I've used the pin hibernation mode on the XBee. The XBee communication together with the pin hibernation work properly. If I add the watchdog timer it stops working. Is the program restarted from the last row executed before the watchdog interrupt?
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>
#include <XBee.h>
XBeeAddress64 coordAddr = XBeeAddress64(0x0013a200, 0x4090c5a6);
uint8_t xbeePayload[] = "Hello";
XBee xbee = XBee();
ZBTxRequest zbTx = ZBTxRequest();
ZBTxStatusResponse txStatus = ZBTxStatusResponse();
int XBee_pin = 9; // This pin wakes up the XBee and puts it to sleep.
int counter = 0;
void sendData() {
zbTx.setAddress64(coordAddr);
zbTx.setAddress16(0xFFFE);
zbTx.setPayload(xbeePayload);
zbTx.setPayloadLength(sizeof(xbeePayload));
xbee.send(zbTx);
}
ISR(WDT_vect)
{
counter++;
}
void enterSleep(void)
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
/* Now enter sleep mode. */
sleep_mode();
/* The program will continue from here after the WDT timeout */
sleep_disable(); /* First thing to do is disable sleep. */
/* Re-enable the peripherals. */
power_all_enable();
}
void setup()
{
// To reduce power, setup all pins as inputs with without any pullups
/*for(int x = 1 ; x < 18 ; x++){
pinMode(x, INPUT);
digitalWrite(x, LOW);
}*/
pinMode(XBee_pin, OUTPUT);
digitalWrite(XBee_pin, HIGH);
xbee.begin(9600);
delay(5000);
/*** Setup the WDT ***/
/* Clear the reset flag. */
MCUSR &= ~(1 << WDRF);
/* In order to change WDE or the prescaler, we need to
* set WDCE (this will allow updates for 4 clock cycles).
*/
WDTCSR |= (1 << WDCE) | (1 << WDE);
/* Set new watchdog timeout prescaler value */
WDTCSR = 1 << WDP0 | 1 << WDP3; /* 8.0 seconds */
/* Enable the WD interrupt (note no reset). */
WDTCSR |= _BV(WDIE);
//Serial.println("Initialisation complete.");
//delay(100); //Allow for serial print to complete.
ADCSRA &= ~(1 << ADEN); //Disable ADC
ACSR = (1 << ACD); //Disable Analog Comparator
DIDR0 = 0x3E; //Disable digital input buffers on ADC1-ADC5 pins
DIDR1 = (1 << AIN1D)|(1 << AIN0D); //Disable digital input buffer on AIN1/0
power_twi_disable();
power_spi_disable();
power_usart0_disable();
//power_timer0_disable(); //Needed for delay_ms
power_timer1_disable();
power_timer2_disable();
}
void loop()
{
if(counter == 8)
{
counter = 0;
pinMode(XBee_pin, OUTPUT);
digitalWrite(XBee_pin, LOW);
delay(5000);
sendData();
delay(2000);
pinMode(XBee_pin, INPUT);
digitalWrite(XBee_pin, HIGH);
/* Re-enter sleep mode. */
enterSleep();
}
}

enterSleep() needs to be outside the if statement. Your current implementation spins in the loop(), waits for 8 watchdog timeouts, executes the code once, sleeps, wakes up and does the same spin. The code should look like this I believe.
void loop()
{
if(counter == 8)
{
counter = 0;
pinMode(XBee_pin, OUTPUT);
digitalWrite(XBee_pin, LOW);
delay(5000);
sendData();
delay(2000);
pinMode(XBee_pin, INPUT);
digitalWrite(XBee_pin, HIGH);
}
/* Re-enter sleep mode. */
enterSleep();
}
Also, be aware that the watchdog is still running while your delays occur. It may trigger again during your if statement, you should issue a wdt_reset() in a few places like:
void loop()
{
if(counter == 8)
{
counter = 0;
pinMode(XBee_pin, OUTPUT);
digitalWrite(XBee_pin, LOW);
delay(5000);
wdt_reset();
sendData();
wdt_reset();
delay(2000);
pinMode(XBee_pin, INPUT);
digitalWrite(XBee_pin, HIGH);
}
/* Re-enter sleep mode. */
enterSleep();
}

Related

Master - Slave using SPI communication (Tinkercad)

Need your help again: I'm doing this time Master - Slave Using SPI communication, there is no error in the code when I simulate the code but the LED won't turn on.
The supposed outcome that should happen is that when I push the push button on master board the LED on the slave board will turn on.
Master code:
// Master Board
#include <SPI.h>
#define button1 4
#define SS 10
int buttonvalue;
int x;
void setup(void) {
Serial.begin(115200); //set baud rate to 115200 for usart
digitalWrite(SS, HIGH); // disable Slave Select
SPI.begin ();
SPI.setClockDivider(SPI_CLOCK_DIV8); //divide the clock by 8
}
void loop(void) {
digitalWrite(SS, LOW);
buttonvalue = digitalRead(button1);
if (buttonvalue == HIGH) {
x = 1;
} else {
x = 0;
}
digitalWrite(SS, HIGH);
delay(1000);
}
Slave code:
// Slave Board
#include <SPI.h>
#define led1 2
volatile byte Slavereceived;
volatile boolean received;
int x;
void setup(void) {
Serial.begin(115200);
pinMode(2, OUTPUT);
pinMode(MISO,OUTPUT);
SPCR |= _BV(SPE);
received = false;
SPI.attachInterrupt();
}
ISR (SPI_STC_vect) {
Slavereceived = SPDR;
received = true;
}
void loop() {
if (received) {
if (Slavereceived == 1) {
digitalWrite(led1, HIGH);
} else {
digitalWrite(led1, LOW);
}
delay(1000);
}
}
I too was stuck in the same situation, there is no support for the SPI library in tinkercad, you can include it without errors, and even use it, but any useful command will let the code stuck at that command
Sorry, but there no much you can do
this link if for a tinkercad forum, where one of the people said SPI library amoung two others are not supported
Add SPI.transfer(x); below the if else to your master code.
The master code will look somewhat like this:
// Master Board
#include <SPI.h>
#define button1 4
#define SS 10
int buttonvalue;
int x;
void setup(void) {
Serial.begin(115200); //set baud rate to 115200 for usart
digitalWrite(SS, HIGH); // disable Slave Select
SPI.begin ();
SPI.setClockDivider(SPI_CLOCK_DIV8); //divide the clock by 8
}
void loop(void) {
digitalWrite(SS, LOW);
buttonvalue = digitalRead(button1);
if (buttonvalue == HIGH) {
x = 1;
} else {
x = 0;
}
SPI.transfer(x);
digitalWrite(SS, HIGH);
delay(1000);
}

Arduino LED control sleep

I want to implement a simple LED controller with an Arduino Uno, that goes to sleep and has different buttons.
Functions of buttons are:
Digital 2: Button for ON OFF
Digital 3: Button for Wake up
Everything works ok, but when it goes to sleep, the LEDs also turn off. I want that after 30 seconds, when Arduino goes to sleep, lights stays on.
Here is my code:
#include <avr/sleep.h>
#define REDPIN 10
#define GREENPIN 11
#define BLUEPIN 9
#define delayTime 20 //za fading cas
unsigned long interval= 30000;
unsigned long previousMillis = 0;
const int ledPin = 12; // the pin that the LED is attached to
const int buttonPin1 = 2; //on off
bool vklop = false;
int bela = 10;
int barva;
int prejsnja_barva = 0;
int buttonPushCounter1 = 0; // counter for the number of button presses
int buttonState1 = 0; // current state of the button
int lastButtonState1 = 0; // previous state of the button
/////////////////////////////////////*SETUP*/////////////////////////////////////////
void setup()
{
pinMode(buttonPin1, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
pinMode(3,INPUT); //because of interrupts PIN digital 3
digitalWrite(3,HIGH);
}
/////////////////////////////////////*LOOP*/////////////////////////////////////////
void loop()
{
unsigned long currentMillis = millis();
if ((currentMillis-previousMillis) > interval) //15s timer
{
previousMillis = currentMillis;
Serial.println("SLEEP!"); // kaj delaj po preteku 5s
delay(50);
sleepSetup(); //sleep mode
}
else
{
buttonState1 = digitalRead(buttonPin1);
/////////////////////////////////////ON/OFF/////////////////////////////////////////
/////////////////////////////////////ON/OFF/////////////////////////////////////////
if (buttonState1 != lastButtonState1) // compare the buttonState to its previous state
{
if (buttonState1 == HIGH) // if the state has changed, increment the counter
{
buttonPushCounter1++; // if the current state is HIGH then the button went from off to on:
Serial.println("on");
Serial.print("number of BUTTON1 pushes: ");
Serial.println(buttonPushCounter1);
digitalWrite(ledPin, HIGH);
if(buttonPushCounter1 % 2 == 0)
{
setColor(bela, bela, bela);
vklop = true;
barva = 13;
}
else
{
setColor(0, 0, 0);
vklop = false;
}
}
else // if the current state is LOW then the button went from on to off:
{
Serial.println("off");
digitalWrite(ledPin, LOW);
}
delay(50); // Delay a little bit to avoid bouncing
}
lastButtonState1 = buttonState1; // save the current state as the last state, for next time through the loop
}
}
/////////////////////////////////functions/////////////////////////////////////////////
/////////////////////////////////functions/////////////////////////////////////////////
/////////////////////////////////functions/////////////////////////////////////////////
void setColor(int red, int green, int blue)
{
analogWrite(REDPIN, red);
analogWrite(GREENPIN, green);
analogWrite(BLUEPIN, blue);
}
void sleepSetup(void)
{
sleep_enable(); // Set sleep enable (SE) bit:
attachInterrupt(1, pinInterrupt, LOW); // Set pin 2 as interrupt and attach handler:
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // define our preferred sleep mode:
digitalWrite(13,LOW);
sleep_cpu();
Serial.println("Just woke up!"); //OD TU SE NADALJUJE PO PRITISKU TIPKE
digitalWrite(13,HIGH);
}
void pinInterrupt() //ISR
{
sleep_disable();
detachInterrupt(0);
}
You're using AVR's Power Down sleep mode. In this mode all timers are turned off to save power.
No timers -> no PWM -> no analogue output -> no PWM driven LEDs
To keep the LED on use another sleep mode.
See
http://www.atmel.com/images/Atmel-8271-8-bit-AVR-Microcontroller-ATmega48A-48PA-88A-88PA-168A-168PA-328-328P_datasheet_Complete.pdf for details.
But to be honest I am not quite sure if this makes any sense. If you're driving an LED through 3 outputs the power you can save by putting the MCU into sleep is maybe a few percent.
And as sleep stops the CPU and hence your program you won't be able to have the LEDs turn off after 30s.
Why not just wait 30s befor going to sleep? The alternative would be some external timing circuitry that would also consume power. So I guess having a few milliamps more for 30 seconds is still a better alternative.

How to disable then re-enable a watchdog interrupt for Arduino?

I'm attempting to use a watchdog interrupt as a timer to sleep for a certain period of time with my Arduino. My problem lies in the fact that, on wake-up, I need to conduct operations that will take longer than 8 seconds.
Currently, my Arduino will sleep for 1 minute, using successive interrupts by the watchdog to wake it up and place it back into sleep. After 1 minute, however, I begin to conduct operations that take longer than 8 seconds and the watchdog interrupt times out.
I want to shut off the watchdog timer, conduct operations, then re-enable it and return to sleeping.
Here is my code:
#include "Adafruit_FONA.h"
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>
#define FONA_RX 10
#define FONA_TX 9
#define FONA_RST 4
#define LED_PIN 8
// this is a large buffer for replies
char replybuffer[255];
char *SMSnumber = "6015962842";
char stack[128] = {'c'};
//Value for watchdog timer interrupt.
volatile int f_wdt = 1;
int seconds = 0;
int minutes = 1;
int hours = 0;
int interval = ((hours*60*60) + (minutes*60) + (seconds))/8;
int timerCounter = 0;
//Setup for pulse sensor.
volatile int Signal; // holds the incoming raw data
int pulsePin = 0; //Pin to read at analog 0 for the pulse.
volatile int IBI = 600; // int that holds the time interval between beats! Must be seeded!
volatile boolean Pulse = false; // "True" when User's live heartbeat is detected. "False" when not a "live beat".
volatile int BPM; // int that holds raw Analog in 0. updated every 2mS
volatile boolean QS = false; // becomes true when Arduoino finds a beat.
unsigned long lastTime; // used to time the Pulse Sensor samples
unsigned long thisTime; // used to time the Pulse Sensor samples
//ISR for watchdog timer.
ISR(WDT_vect)
{
if(f_wdt == 0)
{
f_wdt=1;
timerCounter++;
}
else
{
Serial.println("WDT Overrun!!!");
}
}
// We default to using software serial. If you want to use hardware serial
// (because softserial isnt supported) comment out the following three lines
// and uncomment the HardwareSerial line
#include <SoftwareSerial.h>
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
SoftwareSerial *fonaSerial = &fonaSS;
// Hardware serial is also possible!
//HardwareSerial *fonaSerial = &Serial;
// Use this for FONA 800 and 808s
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
void setup() {
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
delay(3000);
digitalWrite(LED_PIN, LOW);
while (!Serial);
Serial.begin(115200);
setupGsm();
setupWdt();
}
void loop()
{
if(f_wdt == 1)
{
if (timerCounter == interval)
{
WDTCSR |= _BV(WDTON);
Serial.println(F("Tried to stop the watchdog."));
delay(20000);
//Reset timer.
timerCounter = 0;
WDTCSR |= _BV(WDIE);
Serial.println(F("Tried to re-enable watchdog."));
}
/* Don't forget to clear the flag. */
f_wdt = 0;
/* Re-enter sleep mode. */
enterSleep();
}
else
{
/* Do nothing. */
}
}
void setupGsm()
{
fonaSerial->begin(4800);
while (! fona.begin(*fonaSerial)) {
Serial.println(F("Couldn't find FONA"));
delay(1000);
}
digitalWrite(LED_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
delay(500);
digitalWrite(LED_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
delay(500);
digitalWrite(LED_PIN, HIGH);
}
void enterSleep(void)
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
/* Now enter sleep mode. */
sleep_mode();
/* The program will continue from here after the WDT timeout*/
sleep_disable(); /* First thing to do is disable sleep. */
/* Re-enable the peripherals. */
power_all_enable();
}
void setupWdt()
{
/*** Setup the WDT ***/
/* Clear the reset flag. */
MCUSR &= ~(1<<WDRF);
/* In order to change WDE or the prescaler, we need to
* set WDCE (This will allow updates for 4 clock cycles).
*/
WDTCSR |= (1<<WDCE) | (1<<WDE);
/* set new watchdog timeout prescaler value */
WDTCSR = 1<<WDP0 | 1<<WDP3; /* 8.0 seconds */
/* Enable the WD interrupt (note no reset). */
WDTCSR |= _BV(WDIE);
}
In the void loop() function, I'm attempting to turn off the watchdog using WDTCSR |= _BV(WDTON); however my compiler complains that WDTON is not decalared in its scope.
Rather than directly accessing the controller's registers from your code, use wdt_enable() and wdt_disable() from the avr/wdt.h library to start and stop the watchdog timer.
Also, for the sake of system reliability, it might be better to actually keep the watchdog timer running (not disabling it), and add periodic calls to wdt_reset() inside long loops and functions to prevent inappropriate system resets.
For instance, you can replace the delay(20000); line in your code with a loop that repeats 20 times the statements: delay(1000); wdt_reset();

RFid click board with CR95HF microcontroller refuses to respond

I have an Arduino with a RFid click board from Mikro Electronika. The click board has a microcontroller CR95HF microcontroller. According to the datasheet when I send the ECHO command I should poll and then read the returned value 0x55. If I use the command IDN (0x01) and send a datalength of zero, I should get the ID of the controller back.
According to the serial monitor it only returns zero (is there something wrong with the variable?).
According to the logic analyzer it returns the exact same thing as what was send
(when I send 3, it returns 3, when i send 55, it returns 55).
It seems the microcontroller doesn't react anything I send it or it does and it's just broken?
So right now it looks like it is able to send something through the spi.transfer() function, but the retrieve seems to be a problem.
Here's my code:
#include <SPI.h>
// CR95HF: Write, poll, read, reset
#define CMD 0x00
#define POLL 0x03
#define READ 0x02
#define RESET 0x01
#define ECHO 0x55
const int _SSpin = 10; // CS
const int _IRQpin = 6; // INT_out : 2, INT_in : 6, tried both, barely to no difference
//const int _SI0pin = A3;
//const int _SI1pin = A0;
void WakeupCR95HF()
{
//Serial.println("Wake up!");
digitalWrite(_IRQpin, LOW);
delay(100);
digitalWrite(_IRQpin, HIGH);
delay(100);
}
bool EchoResponse()
{
byte len = 0;
// write to CR95HF
digitalWrite(_SSpin, LOW);
delay(100);
SPI.transfer(0x00);
// Serial.print("echo code: ");
// Serial.println(ECHO);
SPI.transfer(0x55);
//SPI.transfer(0x00);
digitalWrite(_SSpin, HIGH);
//delay(1000);
// poll to CR95HF, is it ready?
byte tmp = 0;
digitalWrite(_SSpin, LOW);
while (!tmp)
{
delay(100);
//Note: whatever I send, I get the same thing back. I send 3, then tmp is 3. I send 8, tmp is 8.
SPI.transfer(0x03);
tmp = SPI.transfer(0);
// Serial.print("Polling: ");
Serial.println(tmp, BIN);
tmp = (tmp & 0x08) >> 3;
delay(100);
}
digitalWrite(_SSpin, HIGH);
delay(20);
// ready to write
byte res = 0;
digitalWrite(_SSpin, LOW);
delay(100);
SPI.transfer(0x02);
res = SPI.transfer(0);
// Serial.print("reading: ");
// Serial.println(res);
delay(100);
digitalWrite(_SSpin, HIGH);
delay(100);
if (res == ECHO)
{
return true;
}
return false;
}
void setup() {
Serial.begin(9600);
// I believe this is not possible on Arduino or not nessaccery, tried it anyway, no change
// pinMode(_SI0pin, OUTPUT);
// pinMode(_SI1pin, OUTPUT);
pinMode(_SSpin, OUTPUT);
pinMode(_IRQpin, OUTPUT);
// digitalWrite(_SI0pin, HIGH);
// digitalWrite(_SI1pin, LOW);
digitalWrite(_IRQpin, HIGH); // wakeup
delay(20);
digitalWrite(_SSpin, HIGH); // slave select, low = ready to write/read
delay(20);
SPI.begin();
// 2 MHz max clock speed, Most Significant Bit first, SPI_MODE unknown (probably 0, all were tried)
SPI.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE0));
WakeupCR95HF();
while(!EchoResponse())
{
WakeupCR95HF();
}
// Serial.println("Connection established");
}
void loop() {
}

Toggle pin on Arduino Micro without disrupting operation

I have a code that once it receives a particular input over the serial it will turn 2 pins high, and wait for an interrupt to occur and run its code. I tried using an infinite loop(while 1) but the interrupt routine can't get executed when the button is pressed.
I want the outputs LED and BEEP to toggle after 'C' is received and until the interrupt occurs.
// Declarations before
void loop() {
while(Serial.available())
{
char data = Serial.read();
if(data == 'C')
{
digitalWrite(BEEP, 1);
digitalWrite(LED, 1);
flag = true;
}
}
// Interrupt routine
When this code receives a 'C' char from the serial the code starts a led flashing for 50 msec on and for 50 msec off, the led flashing stops when the interrupt ITR_PIN is generated (the ISR buttonPressed function will be called!)
#define ITR_PIN 3
volatile bool start = false;
bool ledstatus = false;
int LED= 4;
/**
* This method is called on the interruption raised on the falling front of the PIN2
* The start flag is used to avoid rebound front. Interruptions are disabled inside the
* interrupt vector method.
* start is reset once it has been processed in the main loop()
*/
void buttonPressed()
{
start=false;
}
void setup()
{
cli();
pinMode(ITR_PIN, INPUT);
pinMode(LED, OUTPUT);
attachInterrupt(0, buttonPressed, FALLING); // Pin 3
sei();
Serial.begin(9600);
}
void loop(){
while(Serial.available())
{
char data = Serial.read();
if(data == 'C')
{
start = true;
}
}
if (start)
{
ledstatus=!ledstatus;
digitalWrite(LED, (ledstatus)?HIGH:LOW);
delay(50);
} else {
if (ledstatus) {
ledstatus=false;
digitalWrite(LED,LOW);
}
}
}
I just tried this on a simulator:
#define ITR_PIN 3
volatile boolean start = false;
volatile boolean flag = false;
int LED= 4;
/**
* This method is called on the interruption raised on the falling front of the PIN2
* The start flag is used to avoid rebound front. Interruptions are disabled inside the
* interrupt vector method.
* start is reset once it has been processed in the main loop()
*/
void buttonPressed()
{
if(flag)
{
if (!start)
{
start = true;
}
}
}
void setup()
{
cli();
pinMode(ITR_PIN, INPUT);
pinMode(LED, OUTPUT);
attachInterrupt(0, buttonPressed, FALLING); // Pin 3
sei();
Serial.begin(9600);
}
void loop(){
while(Serial.available())
{
char data = Serial.read();
if(data == 'C')
{
digitalWrite(LED, 1);
flag = true;
}
}
if(flag == true)
if (start)
{
digitalWrite(LED, LOW);
delay(50);
start = false;
flag=false;
}
}
(it's just a reduced version of yours, I just removed the stepper dependancies and marked flag as volatile). It works with this hardware:
However, I'm pretty sure that you haven't added the pullup to your circuit!
The solution is:
add a resistor (e.g. 10 kOhm) between pin 3 and +5V
enable the internal pullup
Solution 2 is the preferred one: you just have to change
pinMode(ITR_PIN, INPUT);
into
pinMode(ITR_PIN, INPUT_PULLUP);
EDIT:
According to your comment I think that the behavior should be different:
Everything is off
When you receive a C on the serial interface the led starts "blinking"
When the interrupt is asserted it stops blinking.
I modified the code to achieve this; I slightly reduced the rest of the code (but you can merge it with the previous one)
#define ITR_PIN 3
volatile boolean flag = false;
const int blink_period_ms = 500; // Blink period in milliseconds
unsigned long initialBlinkTime;
int LED= 4;
void buttonPressed()
{
flag = false;
}
void setup()
{
cli();
pinMode(ITR_PIN, INPUT);
pinMode(LED, OUTPUT);
attachInterrupt(0, buttonPressed, FALLING); // Pin 3
sei();
Serial.begin(9600);
}
void loop(){
while(Serial.available())
{
char data = Serial.read();
if(data == 'C')
{
if (!flag)
{
initialBlinkTime = millis();
flag = true;
}
}
}
if(flag)
{
int currentTime = millis() - initialBlinkTime;
while (currentTime > blink_period_ms) currentTime -= blink_period_ms;
digitalWrite(LED, currentTime < blink_period_ms/2);
// Insert here the other instructions
delay(50);
}
}

Resources