mail@pastecode.io avatar
a month ago
1.7 kB
int pin1 = 13; //the LED is on pin number 13

void setup() {

  pinMode(pin1, OUTPUT); //to say if the pin 13 is going to be input or output, output because its going to produce an effect

  Serial.begin(9600); //the quantity of data sent between the board and the computer
} // end setup()

float time_clock; // the actual time of the microcontroller in seconds

float time_p = 0.0;//the initial time 

int pin_state = LOW; //if the pin has power in it or not; 0V(doesnt power the LED) or 5V(power the LED)

void loop() 

 while(1){  //question 1 : why do we need a while loop when the loop() function is a loop itself ?

    float dt = time_clock - time_p; // the difference in time between the actual time and the initial time.
    time_clock = micros()*1.0e-6;


  if(dt > 0.5) // question 2 : why the change doesnt happen at exactly 0.5 seconds so at dt == 0.5 ?

    if(pin_state == LOW) // if the pin isnt powered (5v), power it

        pin_state = HIGH;  
        digitalWrite(pin1, HIGH); // set to 5V TO to turn the LED on
    } //end if #2
   else // if the pin is powered, stop current (0v)

    pin_state = LOW;
    digitalWrite(pin1, LOW); // set to 5V TO to turn the LED on

   } //end else

     Serial.print("\ntime =");

    time_p = time_clock; // question 3 : time_p is the initial time and time_clock is the actual time, if they're equal  then dt = 0, so dt isnt superior than 0.5, i don't understand.

  }//end if #1

 } //end while

 //digitalWrite(pin1, HIGH); // set to 5V TO to turn the LED on

} //end void loop()


Leave a Comment