Pages: [1]   Go Down
Author Topic: I don't understand this simple code  (Read 239 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i found this piece of code in a book and its meant to change the brightness of an led depending on how long you hold the button.  and i understand what the code does and what it is meant to do. i understand the logic flip to detect if the switch was pressed or not, but where i get confused is when it says starttime = millis(); because millis is how long since the program has started and it writes that millis number to starttime, but then later on in the code it says mills() - starttime>500, and wouldn't that always be zero because starttime was set to equal millis() in the begining? why does the starttime go up as you hold the button?

If someone could SIMPLY explain this to me that would be very helpful!!! i am very much confused by this program.


down below is the code.




Code:
const int led = 9;
const int button1 = 3;

int val = 0;
int oldval= 0;
int state = 0;
int brightness= 128;
unsigned long starttime= 0;

void setup()
{
  pinMode(led,OUTPUT);
  pinMode(button1, INPUT);
  Serial.begin(9600);
}

void loop()
{
  val= digitalRead(button1);
  if ((val == HIGH)&& (oldval== LOW))
  {
    state = 1 - state;
    starttime= millis ();
    delay (10);
  }
  if ((val == HIGH) && (oldval == HIGH))
  {
    if (state == 1&& (millis()-starttime)>500)
    {
      brightness++;
      delay (10);
      if (brightness >255)
      {
        brightness = 0;
      }
    }
  }
  oldval= val;
  if (state == 1)
  {
    analogWrite(led, brightness);
  } else{
    analogWrite (led, 0);
  }
}


Moderator edit: [code] [/code] tags added.

* hold_buttondown.ino (0.69 KB - downloaded 1 times.)
« Last Edit: November 20, 2012, 11:17:30 pm by Coding Badly » Logged

SE USA
Offline Offline
Faraday Member
**
Karma: 40
Posts: 3783
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

millis() returns the amount of time since start

so you call it once to establish a timestamp, call it again later (like 2 seconds) and see the difference

its a constantly running timer from when the chip starts up, using the millis() function just recalls the current number at that point
« Last Edit: November 21, 2012, 12:35:24 am by Osgeld » Logged


Texas
Offline Offline
Newbie
*
Karma: 1
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Not sure but think it takes the last stored "time" and monitors the current "time' and subtracts the last stored "time" from the current "time" and if it is more than 500 milliseconds then it stores the new current "time" as the new last "time" and adds one count to the brightness. By using millis (milliseconds) it allows the program to continue to keep looping so other codes can keep being processed. if delay is used it stops every thing else from being processed until the delay has reached the end time.
Logged

Pages: [1]   Go Up
Jump to: