Pages: [1]   Go Down
Author Topic: arduino counting LED light  (Read 706 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I need some help if any can help me please do. I want to use the serial monitor to count how many time's the LEd light come's on can any one help me here is my sketch so far...i'm not sure what to do next please point me in the right direction..thanks
nt ledpin = 12;

void setup()
{
  //set outpups
  pinMode(ledpin, OUTPUT);
 
  Serial.begin(9600);
}

void loop()
{
 Serial.print(digitalRead(ledpin));
 
 delay(100);
 
  digitalWrite(ledpin, HIGH);
 
  delay(1000);
 
  digitalWrite(ledpin, LOW);
 
  delay(1000);
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 644
Posts: 50524
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I want to use the serial monitor to count how many time's the LEd light come's on
Looking at that code, it should be obvious that the light comes on once per iteration of loop(). You need to create a global variable, or a static variable in loop() initialized to 0, and increment that variable in loop().
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1285
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Set up a counter and increment it every time you set the LED high. Then print the count just like you are printing the LED status in your sketch.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How would i do this this is only my second sketch ever? thanks
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How would I increment it every time the LED is high?? Is it a simple ++?
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1285
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int ledpin = 12;
int counter = 0;

void setup()
{
  //set outpups
  pinMode(ledpin, OUTPUT);
 
  Serial.begin(9600);
}

void loop()
{
 Serial.print(digitalRead(ledpin));
 
 delay(100);
 
  digitalWrite(ledpin, HIGH);
  counter = counter + 1;  // or counter++ does the same thing
  Serial.print(counter);

  delay(1000);
 
  digitalWrite(ledpin, LOW);
 
  delay(1000);
}
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ohh thanks soo much I thank both of you for your help now i can finally stop stressing over this I was so close and thanks!!!!!
Logged

Pages: [1]   Go Up
Jump to: