Arduino woe / Arduino wow! - Page 1
Forum section: electronics / beginners

author: PerranOak
author_rank: Frequent Contributor
timestamp: August 04, 2020, 01:45:23 pm
content: I have an Arduino Uno connected to COM6 on my laptop (Win 8.1 pro) running this simple prog from the IDE's built in examples:Code:const int switchPin = 8;unsigned long previousTime = 0; // store the last time an LED was updatedint switchState = 0; // the current switch stateint prevSwitchState = 0; // the previous switch stateint led = 2; // a variable to refer to the LEDslong interval = 200; // interval at which to light the next LEDvoid setup() {Serial.begin(9600);for (int x = 2; x < 8; x++) { // set the LED pins as outputspinMode(x, OUTPUT);}pinMode(switchPin, INPUT); // set the tilt switch pin as input}void loop() {unsigned long currentTime = millis(); // store the time since the Arduino started running in a variableif (currentTime - previousTime > interval) {previousTime = currentTime;digitalWrite(led, HIGH);led++;if (led == 7) { //time is up}}switchState = digitalRead(switchPin);if (switchState != prevSwitchState) {for (int x = 2; x < 8; x++) {digitalWrite(x, LOW);}led = 2;previousTime = currentTime;}prevSwitchState = switchState;}It initially worked fine: the six LEDs (connected to digital pins 2-7) light one after the other until all are lit and then it sits there, all on. I was writing a prog to do something "fun" when I noticed (it was still running) that after a time it seemed to reset and start again without me resetting via pin8 which is connected to a tilt switch.I guessed it was some sort of overflow so I began to investigate. This is where it gets interesting ...honestly(!)When the command:Serial.begin(9600);is put in the LEDs go bonkers. It runs normally once then the LEDs start flashing randomly, the on-board LED goes off (previously on all the time), mad! Then I noticed that if I open the "Serial Monitor" in the IDE it runs normally one time then goes bonkers again. This is without even putting in a command actually to output anything yet!I imagine it's some problem with the laptop's COM port settings? It's never happened on other progs before and when I comment-out the Serial.begin it goes back to working fine.Please help me understand/investigate.Cheers.

author: macboy
author_rank: Super Contributor
timestamp: August 04, 2020, 02:57:01 pm
content: If the switch is never toggled, then when does variableledget reset? It doesn't. It will continue to increment and your digitalWrite(led,LOW) will eventually clobber all the pins.

author: rstofer
author_rank: Super Contributor
timestamp: August 04, 2020, 03:22:45 pm
content: if (led == 7) { //time is upled = 0; // missing code?}

author: PerranOak
author_rank: Frequent Contributor
timestamp: August 04, 2020, 03:26:32 pm
content: But the line:led = 2;should reset it?Also, it works fine until I change only one thing and that is to uncomment the Serial.begin command.

author: Fredderic
author_rank: Regular Contributor
timestamp: August 04, 2020, 03:41:32 pm
content: Not sure what your serial issue is, other than it'll reset the thing every time you open the Serial Monitor. Arduino's are like that… Makes programming easier. If that's not the issue at play, then, definitely curious…But scrapping the if(led==7) and putting an if(led<=7) around the whole timer check would protect against the issue macboy mentioned — and as a bonus, it's both a lighter-weight check, and more likely to be true assuming the device sits there ignored for most of it's life. On that count, I'd probably also flip the two checks around, and chain them with an else — it's not much point updating the countdown, just to immediately reset it again.

author: PerranOak
author_rank: Frequent Contributor
timestamp: August 04, 2020, 03:52:59 pm
content: Cheers.Sorry I didn't explain properly.This is a "beginning" prog that I am meant to improve. It is supposed just to sit there with all LEDs on and I am to put in some "clever" code so as to make it do something (flash LEDs or sound a tone, etc.) when it gets to the sixth LED (on pin7) being lit.However, I discovered this anomaly which is more interesting.The key thing is that it works as it should (albeit a very numpty prog) unless and until I put in the Serial.begin command - no other changes at all. I've rebooted the laptop used it with Serial Monitor engaged and without - same results. Very peculiar.

author: Fredderic
author_rank: Regular Contributor
timestamp: August 04, 2020, 04:05:59 pm
content: Still… Fix what you've got, like three of us have mentioned now. And then see if the problem persists. Clobbering your way through whatever, is generally abadidea, and can have all sorts of unexpected results. Until that's done, you can't be sure whether you actually have a problem with serial or not.

author: DrG
author_rank: Super Contributor
timestamp: August 04, 2020, 04:07:22 pm
content: Quote from: rstofer on August 04, 2020, 03:22:45 pmNot=0 but =2 or a lot of other possibilities.This example(Project 08: Hourglass) has been carried over for years in the IDE. Lots of examples of folks struggling with it...for years. Lots of updates also e.g.,https://philectron.github.io/arduino/2016/07/28/arduino-projects-book-project-08/

author: sleemanj
author_rank: Super Contributor
timestamp: August 04, 2020, 08:36:53 pm
content: Low USB voltage right on the edge of brown out maybe. Measure voltages.Otherwise, simplify your problem, does code that just turns on all leds and does Serial.begin in setup and does nothing in loop work, what about "blink" modified to do all leds.

author: 0-8-15 User
author_rank: Regular Contributor
timestamp: August 05, 2020, 08:56:24 am
content: Could it not just be an issue with the tilt switch? You read the raw output of the sensor in a tight loop with zero debouncing in hardware or software at all. This is asking for trouble.Try this:Code:#define LED_TIMER_INTERVAL 200#define TILT_SWITCH_PIN 8void setup() {Serial.begin(9600);for (int x = 2; x < 8; x++) {pinMode(x, OUTPUT);}pinMode(TILT_SWITCH_PIN, INPUT);}void loop() {static unsigned long prevLedUpdateTime = 0;static bool prevSwitchState = 0;static int led = 2;if (led < 8 && millis() - prevLedUpdateTime > LED_TIMER_INTERVAL) {prevLedUpdateTime = millis();digitalWrite(led, HIGH);led++;}int switchState = digitalRead(TILT_SWITCH_PIN);for (int i = 0; i < 5; i++) { // crude sensor debouncedelay(10);if (digitalRead(TILT_SWITCH_PIN) != switchState) {return;}}if (switchState != prevSwitchState) {for (int x = 2; x < 8; x++) {digitalWrite(x, LOW);}prevLedUpdateTime = millis();led = 2;}prevSwitchState = switchState;}

author: PerranOak
author_rank: Frequent Contributor
timestamp: August 05, 2020, 01:49:10 pm
content: Brilliant! Thank you everyone.I tried all those things and it seemed, in the end, to be a prob with the variable "led" running off.Phew! Cheers all.P.S.I still don't get why it behaved differently when Serial.begin was used or not. Ah well.

author: westfw
author_rank: Super Contributor
timestamp: August 08, 2020, 12:53:19 am
content: QuoteThe problem with running your led number past the number of actual pins is that the digitalWrite() function will end up modifying "random" places in memory, which will have "random" effects on your program. By adding Serial.begin(), you change the layout of variables in memory, sodifferent"random" things will happen.