Hey so I would like to measure the time elapsed and print it on screen between a LED turning on and a button being pressed. This is the code:
int led=13;
int boton=12;
int tim=0;
int buttonState=0;
void setup() {
 Serial.begin(9600);
 Serial.println("when the LED turns on, press the button and the time in milliseconds will be printed on screen");
}
void loop() {
 buttonState = digitalRead(boton);
 pinMode(led, OUTPUT);
 analogWrite(13, 255);
 pinMode(boton, OUTPUT);
while (buttonState == LOW) {
 tim = millis(); //start time
 if (buttonState == HIGH);
 Serial.println(tim);
}
 delay(5000);
 Serial.println("milliseconds elapsed");
 delay(5000);
}
// millis() is an unsigned long.
// so use the same variable type to record it
unsigned long start;
unsigned long elapsed;
int led=13;
int boton=12;
void setup() {
 Serial.begin(9600);
 Serial.println("when the LED turns on, press the button and the time in milliseconds will be printed on screen");
Â
 // only set pinmodes once during start
 // your button should be either INPUT or INPUT_PULLUP
 pinMode(boton, INPUT);
 pinMode(led, OUTPUT);
Â
 //wait
 delay(1000);
Â
 //turn the light on and read the time
 analogWrite(led, 255);
 start=millis();
 //wait for button
 while (digitalRead(boton) == LOW) {}
 elapsed = millis()-start;
Â
  Serial.println("milliseconds elapsed: ");
  Serial.print(String(elapsed));
}
void loop() {}
Don't get mad at me. LOL. just saying what i had to do to light up an LED on my board. i wouldn't have mentioned if i didn't try it both ways or if i changed anything else.
taterking:
Don't get mad at me. LOL. just saying what i had to do to light up an LED on my board. i wouldn't have mentioned if i didn't try it both ways or if i changed anything else.
I'm not mad. I'm just saying that if you thought that calling analogWrite with 255 or calling digitalWrite on the same pin with HIGH accomplished anything differently then you are simply wrong. Maybe you had a wire loose and bumped it or something. Who knows why your led lit up. But I can guarantee without the slightest shadow of a doubt it wasn't because you wrote analogWrite with 255 instead of digitalWrite.
I mean if you doubt me just look at the code. Here is the first few lines of the analogWrite function:
From wiring_analog.c:
void analogWrite(uint8_t pin, int val)
{
// We need to make sure the PWM output is enabled for those pins
// that support it, as we turn it off when digitally reading or
// writing with them. Also, make sure the pin is in output mode
// for consistenty with Wiring, which doesn't require a pinMode
// call for the analog output pins.
pinMode(pin, OUTPUT);
if (val == 0)
{
digitalWrite(pin, LOW);
}
else if (val == 255)
{
digitalWrite(pin, HIGH);
}
else
{
That else at the end has all the stuff for the different processors to do PWM. After that is nothing. So if you call analogWrite with 255 the ONLY thing that happens is that it calls digitalWrite with HIGH.
septillion:
That's not completely true And it might point out what went wrong It's exactly the same as
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
So my money is on not calling pinMode() ;)
Bet. If he had forgot his pinMode. But someone giving me the business about how AVR code works surely wouldn't be making a mistake like that. I mean anyone who is going to blame the core and say that something like digitalWrite doesn't work has to be pretty sure that it wasn't their mistake.
I did just open my sketch and look. septillion is correct. There is no pinmode declaied at the top of my scetch. I didnt debate anything about avr or anything like that. just stated the fact that my light lit up when i changed the line in my scetch. I do appreciate the explanation and information though