Blatant failure of ' If (state=1)...' statement.

Here’s the code. A day has gone by as I tried to figure this out.

It’s suppose to be a …
’ Go to sleep.
Wake up using watchdog timer and transmit a packet.
Wake up when the D3 button is pressed and transmit a different packet. ’

The transmit part works fine.
The serial monitor link baud rate is a little jittery right after wakeup, but that’s not the issue.
It’s the… ‘state=0’ but executes the ‘If (state=1)… statement anyway’ part that is the issue.

Any ideas?

#include <VirtualWire.h>
#include <avr/sleep.h>
#include <avr/wdt.h>

/* Pin definitions */
int wakePin = 3;

/* Variables */
volatile int state = 0;

/* Arrays */
char msg[30];

ISR(WDT_vect) { // Watchdog Interrupt Service
} //________ END OF ISR _____________

void wakeUpNow() { // ISR for button push
} //________ END OF wakeUpNow_____________

void setup() {
vw_setup(2400); // Bits per sec

setup_watchdog(9); //6=1sec, 9=8 sec

pinMode(wakePin, OUTPUT);
digitalWrite (wakePin,HIGH); //turns on the internal pullup?
pinMode(wakePin, INPUT); // push-button to gnd.
attachInterrupt(1, wakeUpNow, LOW); // use interrupt 1 (D3) and run function
} //_______ END OF SETUP _____________________________

void loop() {

XMIT_WDT(); Serial.println(“Finished XMIT WDT”); // every 8 sec?

delay(1000); //because my board has trouble waking up-to-speed - resonator issues?

Serial.print("\nState="); Serial.println(state);
//that’s funny - it prints “State=0”…
if (state=1) { //and then does this anyway!
XMIT_D3LOW(); Serial.println(“Finished XMIT D3LOW”);
goto back2sleep; }

} //_______ END OF LOOP _____________________________

void XMIT_WDT() { //wakeup due to watchdog timer
*msg = 0;
strcat(msg, “:XMIT WDT”);
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx(); //wait until it’s done

} //________ END OF XMIT_WDT ______________

void XMIT_D3LOW() { //wakeup due to pin D3 going low
*msg = 0;
strcat(msg, “:XMIT D3LOW”);
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx(); //wait until it’s done
} //________ END OF XMIT_D3LOW ______________

void system_sleep() {
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_mode(); // System sleeps here
// Z - Z - Z - Z - Z - Z - Z
sleep_disable(); // System continues execution here when watchdog times out
} //________ END OF SYSTEM SLEEP _____________

void setup_watchdog(int ii) {
// 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms
// 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec
byte bb;
int ww;
if (ii > 9 ) ii=9;
bb=ii & 7;
if (ii > 7) bb|= (1<<5);
bb|= (1<<WDCE);
MCUSR &= ~(1<<WDRF);
// start timed sequence
WDTCSR |= (1<<WDCE) | (1<<WDE);
// set new watchdog timeout value
WDTCSR = bb;
} //
_______ END OF SETUP_WATCHDOG _____________


Should be if(state==1) = is assignment, not compare for equality

You are assigning state the value 1 which is successful and therefore passes the if condition. What you probably want is

if(state == 1)

Which would be the proper way to compare the value of state ;)

AGHH!!! It must be the lack of sleep. I can't believe I stared at it for that long and didn't think of it! Must be the years of VB programming. It just didn't 'look' wrong. Perhaps this little slap-of-shame will keep it from happening again for a few months. ;) Thanks!

Everyone does that once in a while, the amount of shame is irrelevant. Say I make 10 such mistakes in a particular month and comparing with my other month, 1 such mistake for the entire month. My single mistake is 10 time more shameful as the mistakes from 10 mistakes :)

Also if you have a buddy that codes, check each others code. Much better than staring at your own code.

My frequent mistakes also include declaring a string not long enough to hold a string from sprintf, and other silly stuff.

Your code looks well-organized so I will share with you one trick I use when my code gets big. I use ASCII art fonts to separate my sections of code like:

.___  ___.  _______ .__   __.  __    __  
|   \/   | |   ____||  \ |  | |  |  |  | 
|  \  /  | |  |__   |   \|  | |  |  |  | 
|  |\/|  | |   __|  |  . `  | |  |  |  | 
|  |  |  | |  |____ |  |\   | |  `--'  | 
|__|  |__| |_______||__| \__|  \______/  

Just google ASCII font starwar or other places that offer free online conversion. you type in a phrase they convert into large font. I wish I can change the color of comments to green, as other IDEs do, then this will be more visible.

I use ASCII art fonts to separate my sections of code

Difficult to search for, though, aren’t they?

Stuff like this:

if (ii > 7) bb|= (1<<5);

is better indented. This is C, not BASIC.

Hey! That's pretty neat! Now if there was a way to organize code sections into pages... and auto-scroll didn't happen three lines short of the top and bottom...

Oh, BTW, that little mistake entered up after I hacked down the code to the essentials - about 1/2 hour before the 'I give up' point - not as shameful as originally thought. :)

However, the original issue that prompted the pruning is still there - and is ... I push the button - it wakes up because of that (not the WDT) - but 'state' was NOT being set to zero. It still isn't. There isn't THAT much else going on - just a bunch of analog port reading, float conversions and the like. Any ideas on why 'state' might not be getting set to '1'? The interrupt is clearly causing it to wake up. Either wakeUpNow() isn't actually getting called or 'state' isn't hanging on to it's '1'. It's hard to tell without being able to put debug code in the ISR. PS. The 'jittery' serial monitor issue was the old 'going to sleep before the last character is fully sent' thing.

Your code looks like a cat has vomited on it, and what's with the "goto"?

:D Yeah, that's nicer than what I thought - it's the font - looks better in the IDE. I had to grit my teeth when I typed 'goto' - but it was late and I rationalized it as being one of those exceptions that make the rule. It's not going to stay there long. Anyhoo... never mind - it's all good again. I pasted in routines from the working pruned-down code back into my original program and it's working now - probably something like a missing () - I'll never know - and I'll not be caring. ;) Moving on... nice chatting with y'all! What I learned... There's no 'serial print buffer empty' flag. You can't be watching Craig Ferguson AND get up at 5:30am ALL WEEK and not suffer some consequences.

it's the font - looks better in the IDE

Maybe if you'd put it in a "Code" box, using the posting editor's # (code) toolbar icon.

I wish I can change the color of comments to green, as other IDEs do

If the not-other IDE is the arduino IDE, you can edit that in the “theme.txt” file. On linux it’s located in arduino-00xx/lib/theme/theme.txt. Search for “comment1”, or just look at the end of the file:

# TEXT - COMMENTS = #00FF00,plain = #777755,plain

Change the to something, then start the arduino IDE. Format is the usual #RRGGBB in hex. What the is I have no idea, it don’t seem to work on anything.

What a hell, does anyone thinks that that code is easy to read?! That thing put my eyes bleeding


I use ASCII art fonts to separate my sections of code

Difficult to search for, though, aren’t they?

I didn’t say you can’t use // Menu together with the ascii font, did I?,58597.0.html