Dear All,
I’m new to arduinno and c – there’s this problem that's driving me nuts, I hope someone can shed some light on it.
I was building a sky remote/tv/projector that I could control on my phone using Bluetooth and a program called microcontrol(android app). It seems to work great – I then wanted to leave the prototype world and make the real thing. I bought the same atmel chip as my arduino and made a permanent circuit. Again it seem to work great except for the fact it would eat batteries.
So I wanted to use power down mode.
I felt my system would feel smoother and not laggy if I had a delay so it would remain in loop for say 15sec before going on to sleep.
My method to keep it in loop before going to sleepNow() was to
count=count-1;
if (count==0){sleepNow(); count=150;}
but for some reason it does not delay unless I leave the line Serial.print(" LLLL start of loop LLLL "); - which is weird as it really has nothing to do with anything, it was just added so I could see where it was in the program when it was running.
As soon as I remove this line the delay does not work and it goes quickly to sleep. If I include the line it delays as it should – I can even increase count and it will delay for longer as expected.
What’s going on? (thinking about it seems when Serial.print(" LLLL start of loop LLLL "); is not running causes it to jump out. what's causing the jump? the interrupt? why is running something on serial.print stopping it?)
Thanks.
I’m new to arduinno and c – there’s this problem that's driving me nuts, I hope someone can shed some light on it.
I was building a sky remote/tv/projector that I could control on my phone using Bluetooth and a program called microcontrol(android app). It seems to work great – I then wanted to leave the prototype world and make the real thing. I bought the same atmel chip as my arduino and made a permanent circuit. Again it seem to work great except for the fact it would eat batteries.
So I wanted to use power down mode.
I felt my system would feel smoother and not laggy if I had a delay so it would remain in loop for say 15sec before going on to sleep.
My method to keep it in loop before going to sleepNow() was to
count=count-1;
if (count==0){sleepNow(); count=150;}
but for some reason it does not delay unless I leave the line Serial.print(" LLLL start of loop LLLL "); - which is weird as it really has nothing to do with anything, it was just added so I could see where it was in the program when it was running.
As soon as I remove this line the delay does not work and it goes quickly to sleep. If I include the line it delays as it should – I can even increase count and it will delay for longer as expected.
What’s going on? (thinking about it seems when Serial.print(" LLLL start of loop LLLL "); is not running causes it to jump out. what's causing the jump? the interrupt? why is running something on serial.print stopping it?)
Thanks.
Code:
/*
* IRremote: IRsendDemo - demonstrates sending IR codes with IRsend
* An IR LED must be connected to Arduino PWM pin 3.
* Version 0.1 July, 2009
* Copyright 2009 Ken Shirriff
* http://arcfn.com
*/
#include <IRremote.h>
#include <SoftwareSerial.h> //software serial as hardware serial not working!
#include <avr/interrupt.h>
#include <avr/power.h>
#include <avr/sleep.h>
SoftwareSerial mySerial(2, 10); // RX, TX change to pin 2 from 9 for interrupt
IRsend irsend;
const int ledPin = 13;
unsigned long count = 150;
////////////////////////////////////////////////////////////////////
//////////////////////////start setup/////////////////////////////////
void setup()
{
Serial.begin(9600);
mySerial.begin(9600);
Serial.print(" QQQQ void setup QQQQ ");
pinMode(2,INPUT_PULLUP);
pinMode(13,OUTPUT);
}
////////////////////////////////////////////////////////////////////
///////////////////////////end setup ////////////////////////////////
////////////////////////////////////////////////////////////////////
/////////////////////////start loop//////////////////////////////////
void loop()
{
digitalWrite(13,HIGH);
Serial.print(" LLLL start of loop LLLL ");// WHEN I REMOVE THIS LINE MY DELAY DOES NOT WORK
if (mySerial.available())
{
char serr = mySerial.read();
if(serr == 'P') {goto jump6;}
else if(serr == 'Q') { goto jump7;}
}
goto start;
jump6:
irsend.sendNEC(0xAF548B7, 32); // vol up
Serial.print(" vol up");
delay (50);
goto start;
jump7:
irsend.sendNEC(0xAF5A857, 32); // vol down
Serial.print(" vol down");
delay (50);
goto start;
start:
count=count-1;
if (count==0){sleepNow(); count=150;} //loops 150times then jumps to sleepnow for delay
}
////////////////////////////////////end loop////////////
///////////////////start sleep///////////////////////////
void sleepNow(void)
{
Serial.print("xxxx in sleep xxxx ");
delay(100);
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Choose our preferred sleep mode:
sleep_enable(); // Set sleep enable (SE) bit:
attachInterrupt(0, pinInterrupt, LOW); //interrupt 0 is on pin 2
digitalWrite(13,LOW);
sleep_mode(); // Put the device to sleep:
sleep_disable(); // Upon waking up, sketch continues from this point.
}
//////////////////////end sleep///////////////////////////////////
/////////////////////stop interrupt////////////////////////////////
void pinInterrupt(void)
{
detachInterrupt(0);
Serial.print("--- TURN OFF INTERR ---");
}
Last edited: