Go Down

Topic: arduino counting LED light (Read 777 times) previous topic - next topic

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);
}

PaulS

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().

marco_c

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.
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

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

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

marco_c

Code: [Select]

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);
}
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

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!!!!!

Go Up