[Solved] Variable are automatically updated

Hi All,

The follow code is similar to Arduino blinkWithoutDelay.iso code.
This code there are a bug on variable “start_chronometer” line 17.
On end of the loop function (line 95) I update the variable “start_chronometer” with millis() return value.
But when I check the value of the same variable on beginning of next loop (line 82) the value was updated automatically.

  1. The code just doesn’t work if I select some Arduino analog port as digital (line 15). In other words if I use pins A0, A1, A2 or A3 on line 15 doesn’t work, but I uses other port like 10, works fine.

  2. This code doesn’t work on arduino pro mini 328 5V (https://www.sparkfun.com/products/11113). I tested it on two of this boards. But If I use the same code on Teensy 3.2 board, the code works fine.

//Control Valves Test
//
// This draft of program control solenoids valves 
//
// 
//
//version 0.01
//
//02-08-2016

#include <Arduino.h>
#define ARDUINOPORTS 12
#define VALVES_TIME_LAPSE 5000

int PinValve1 = 14; //I try A0 too

unsigned long start_chronometer = 0;


////FLAGS///////

boolean F_valve[ARDUINOPORTS];
boolean f_loopValves = false;



byte inByte = 0;
byte previousInByte = 0;
byte repeat_inByte = 0;


void switchValve(int port){
  F_valve[port] = !F_valve[port];
  digitalWrite(port, F_valve[port]);
  Serial.println("The Valve wired on arduino port " + String(port) + " was suitched"); 
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(PinValve1, OUTPUT);
  for(int i = 0; i < ARDUINOPORTS; i++){
    F_valve[i] = LOW;
  }
}

void loop() {
  // put your main code here, to run repeatedly:
 
  if(Serial.available() > 0){
    Serial.println("Hear the serial");
  
    inByte = Serial.read(); 
    delay(500);
    Serial.println(inByte); 
  }
  
  if(inByte == '1'){
    switchValve(PinValve1);
  }
 
  
  if(inByte == 'l'){ //it is L not one
    Serial.println("Loop");
    f_loopValves = !f_loopValves;
    repeat_inByte = previousInByte;
    start_chronometer = millis();
    Serial.print("First start_chronometer: ");
    Serial.println(start_chronometer);
  }
  
  if(inByte != '0' && inByte != 'l'){ //it is L not one
    previousInByte = inByte;
    
  }
  inByte = '0';
  unsigned long currentMillis = millis();
  
  Serial.println("DEBUG:==================Loop====================");
  Serial.print("DEBUG: currentMillis " + String(currentMillis));
  Serial.print(" delta:" + String(currentMillis - start_chronometer));
  Serial.println(" Second start_chronometer: " + String(start_chronometer));
  
  if((f_loopValves == true) && (VALVES_TIME_LAPSE < (currentMillis - start_chronometer))){
      if(currentMillis < start_chronometer){
        Serial.println("DEBUG: Millis() is Minor than start_chronometer");
        Serial.print("DEBUG: currentMillis " + String(currentMillis));
        Serial.print("   delta: " + String(currentMillis - start_chronometer));
        Serial.println("   3rd start_chronometer: " + String(start_chronometer));
      }
      Serial.print("DEBUG: currentMillis " + String(currentMillis));
      Serial.print("   delta: " + String(currentMillis - start_chronometer));
      Serial.println("   4rd start_chronometer: " + String(start_chronometer));
      inByte = repeat_inByte;
      start_chronometer = currentMillis;
      Serial.print("DEBUG: currentMillis " + String(currentMillis));
      Serial.print("   delta: " + String(currentMillis - start_chronometer));
      Serial.println("   5rd start_chronometer: " + String(start_chronometer));
      
  }
  delay(100);
}

a cut of the output of this code is:

DEBUG:==================Loop====================
DEBUG: currentMillis 15370 delta:4294902264 Second start_chronometer: 80402
DEBUG: Millis() is Minor than start_chronometer
DEBUG: currentMillis 15370   delta: 4294902264   3rd start_chronometer: 80402
DEBUG: currentMillis 15370   delta: 4294902264   4rd start_chronometer: 80402
DEBUG: currentMillis 15370   delta: 0   5rd start_chronometer: 15370
The Valve wired on arduino port 14 was suitched
DEBUG:==================Loop====================
DEBUG: currentMillis 15874 delta:4294902264 Second start_chronometer: 80906
DEBUG: Millis() is Minor than start_chronometer
DEBUG: currentMillis 15874   delta: 4294902264   3rd start_chronometer: 80906
DEBUG: currentMillis 15874   delta: 4294902264   4rd start_chronometer: 80906
DEBUG: currentMillis 15874   delta: 0   5rd start_chronometer: 15874
The Valve wired on arduino port 14 was suitched

You can see on line 6 on arduino output that the start_chronometer variable has 15370 and on line 9 it was updated to 80906

Please, let me know if I made a basic mistake. If not, this post is just a info. I’m working using a digital port.
Thank you for your help

On attached file you can find:
the code (_2016-08-04-bugged-forum.ino)
the output of sketch (2016-08-04-console-OUPTUT-bugged-forum.txt)
the compilation and upload output (2016-8-04-arduino-compilation-and-upload-output.txt)
Some info of my system and boards(System-info.txt)
the temporary folder used by arduino compile and update the code (build9726d399befb320a98e70bfe56a47e63.tmp)

bug.zip (381 KB)

You define an array of 12 elements, then you try to write to the 14th element. This happens to be the space where start_chronometer is stored, so it overwrites that value.

Oh no!!

Thanks MorganS!!!