Arduino Won't Change EEPROM Setting.

What I’m trying to do is to make a program that’ll toggle an output pin (like 13) to come on only
every other time it’s powered up. That is, you plug it in the first time, it comes on, unplug it
then plug it in again, and the LED won’t like a divide by two counter but count the cycling of the
power. This is for an alarm clock with AM/PM function for a blackout-resistant alarm clock.

Here’s the code:

—begin code—
#include <EEPROM.h>

int led = 13;
int x;

void setup()
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
x = EEPROM.reas(0);

int y = x;
if( x =1)
y = 0;
y = 1;
EEPROM.write(0, y);

void loop()
// blinky code here, not needed for discussion.
—end code—

When I upload it, it goes fine but when I check the terminal, I get a zero every time I reset it.

The project is to use this “divide by 2-duino” (I actually want to use an ATTiny85 chip" and
with small relays hook it to a battery-driven alarm clock I already hacked and a radio. I don’t
want to use the sleep function as that still drains battery 24/7. A nice future function would
be a divide by 14 to add in days of the week to the blackout-proof alarm clock.But that’ll be later.

In IF statements, to compare anything you use == not a single =. Tell me from that code, can you read from the EEPROM?


if (x = 1)

should be

if (x == 1)

This is a typo in the post, yes?
x = EEPROM.reas(0);


To the dirst guy:
Yep, once I added the second equal sign to the if() worked perfect. I added it,
flashed it in, and tested it with plugging and unplugging the Arduino with the
sketch. I wonder why the script compiled with the missing second equal sign.


To the second guy:
Yep, that reas(0) was in fact a typo. I couldn't cut and paste so I switched
windows and retyped it, adding the typo in the post. The actual script has no
typo. It wouldn't compile with it.

Thanks to both of you!

I couldn't cut and paste

Ctrl+a will select all the code, Ctrl+c will copy whatever is selected to the clipboard. How you paste it into a browser window will be determined by the browser, but most browsers recognise Ctrl+v as a paste command.

I wonder why the script compiled with the missing second equal sign.

Because the compiler doesn't see it as being wrong, it sees the one equal sign and "thinks" you want to assign 1 to the variable x, when what you really want to do is compare them.

I have seen that also, can't copy from the ide window.