Arduino 101 - Lesson 2

Discussion in 'Welcome to the DARK side' started by paul_l, Jul 27, 2017.

  1. Toto

    Toto I'm best ignored Staff Member Founder Administrator

    Messages:
    15,419
    Likes Received:
    3,842
    Joined:
    Dec 5, 2017
    I was including myself in the above :avatar:
     
  2. paul_l

    paul_l Staff Member Administrator

    Messages:
    9,848
    Likes Received:
    5,902
    Joined:
    Dec 5, 2015
    I think you guy's are taking this far too seriously - chill out the homework doesn't start till next week

    With the delay set at 500 - this is what I got

    https://youtu.be/hQKXEl4jwNQ



    Next up we will need to be able to stop and start the flashing

    Paul
     
  3. paul_l

    paul_l Staff Member Administrator

    Messages:
    9,848
    Likes Received:
    5,902
    Joined:
    Dec 5, 2015
    So to be able to switch the flashing LEDs off / on one additional wire is required

    [​IMG]

    Here is the code for the sketch

    =======================================================

    /*Note:This code is used for Arduino 1.0.1 or later*/
    /*
    Button Controlled LED
    Turns on an LED when push the button.
    */
    void setup() {
    // initialize the digital pin 7 as an input with pull up resistor
    pinMode(7, INPUT_PULLUP);
    // initialize the digital pins as an output.
    pinMode(11, OUTPUT);
    pinMode(12, OUTPUT);
    }
    void loop() {
    int sensorValue = digitalRead(7);
    if(sensorValue)
    {
    digitalWrite(11, HIGH); // turn the LED on (HIGH is the voltage level)
    digitalWrite(12, LOW);
    delay(500); // wait for a second
    digitalWrite(11, LOW); // turn the LED off by making the voltage LOW
    digitalWrite(12, HIGH);
    delay(500); // wait for a second
    }
    else
    digitalWrite(12, LOW); // set the LED off
    digitalWrite(11, LOW); // set the LED off
    }

    =======================================================

    So what happens

    // initialize the digital pin 7 as an input with pull up resistor
    pinMode(7, INPUT_PULLUP);

    The above line in the void setup() section sets Digital I/O pin 7 as an input, with the internal pullup resistor (10K - I think) - this allows you to connect the input to ground without causing any damage. This can be done using a switch, a wire link, or any sensor

    // initialize the digital pins as an output.
    pinMode(11, OUTPUT);
    pinMode(12, OUTPUT);

    as in the previous example this sets up pins 11 & 12 as Digital outputs.

    Now hear is the magic

    void loop() {
    int sensorValue = digitalRead(7);
    if(sensorValue)

    the int sensorValue = digitalRead(7); create an interger (a whole number) called sensorValue, and sets it equal to the value of input pin7.

    if(sensorValue) now the magic this basically asks if the value of sensorValue is TRUE i.e. its value is HIGH (5v) then it will execute the code between the { } brackets

    {
    digitalWrite(11, HIGH); // turn the LED on (HIGH is the voltage level)
    digitalWrite(12, LOW);
    delay(500); // wait for a second
    digitalWrite(11, LOW); // turn the LED off by making the voltage LOW
    digitalWrite(12, HIGH);
    delay(500); // wait for a second
    }

    Which will flash the two LED's on and off

    If however sensorValue is not TRUE i.e. its value is LOW (0v) - pin 7 is connected to GND
    Then the following code is executed

    else
    digitalWrite(12, LOW); // set the LED off
    digitalWrite(11, LOW); // set the LED off
    }

    which will switch off both LEDs.

    So by connecting Pin 7 to Ground or 0v you can switch off the lights, and like wise removing the connection to GND will start the LED's flashing.

    By calling the variable "sensorValue" has been done to give you a hint at what you may wish to use as a trigger.

    I apologize for the blurry video, I was balancing the board on my knee, hand holding the camera while trying plug and unplug the wire to ground.

    https://youtu.be/0PVWj9IgyWo



    Paul
     
  4. Toto

    Toto I'm best ignored Staff Member Founder Administrator

    Messages:
    15,419
    Likes Received:
    3,842
    Joined:
    Dec 5, 2017
    Very good, we now have a level crossing or lights outside a fire station or a whole load of other uses and very doable. :thumbs:
     
  5. paul_l

    paul_l Staff Member Administrator

    Messages:
    9,848
    Likes Received:
    5,902
    Joined:
    Dec 5, 2015
    and if you hadn't realised, you can cut and paste the code from the posts into the arduino software - saves on all that typing :thumbs:

    coming later two aspect signal, and a traffic light .............

    Paul
     
  6. ianvolvo46

    ianvolvo46 Staff Member Moderator

    Messages:
    5,331
    Likes Received:
    1,588
    Joined:
    Dec 8, 2015
    Anyone know where I can get mi pointy hat?

    Ian vt
     
  7. Toto

    Toto I'm best ignored Staff Member Founder Administrator

    Messages:
    15,419
    Likes Received:
    3,842
    Joined:
    Dec 5, 2017
    I'll keep my eye open for one Ian. :avatar:
     
  8. ianvolvo46

    ianvolvo46 Staff Member Moderator

    Messages:
    5,331
    Likes Received:
    1,588
    Joined:
    Dec 8, 2015
    Cheers Toto I've got the time my 'hard dinna you know' kit is being posted to my son in the UK first as they wouldn't send to Spain.

    Ian vt
     
  9. Ron

    Ron Full Member

    Messages:
    3,318
    Likes Received:
    757
    Joined:
    Dec 5, 2015
    Thanks for the info on 'copy and paste' Paul, didn't fancy all that typing!! :tophat:
    PS, the wife's got a pointy hat somewhere I think? :scratchchin:
     
  10. ianvolvo46

    ianvolvo46 Staff Member Moderator

    Messages:
    5,331
    Likes Received:
    1,588
    Joined:
    Dec 8, 2015
    Ok that's it Ron just got scary I'm off to hide!
     
  11. paul_l

    paul_l Staff Member Administrator

    Messages:
    9,848
    Likes Received:
    5,902
    Joined:
    Dec 5, 2015
    The Dark side is getting stronger :avatar:

    Up to now we have used a separate output per LED, this time I'll try using one output

    [​IMG]

    This time we are connecting the LEDs and resistors in series

    5V - 1K resistor - Red Led - Green LED - 1K resistor - GND

    So if we apply the power both LEDs come on.

    Now by adding the wire between the two LEDs and connecting it to Digital output 11, when out put 11 is LOW (GND or 0V) then the Red LED will be on as it has a direct path to GND, the Green will be off as its anode is at 0V.

    When output 11 goes HIGH (5V), the Red LED goes off as its Cathode is at 5V, and the Green comes on as the anode is at 5V and can flow through the LED to GND.

    By adding a push button to Pin 7 we have a means to toggle between the two states.

    On the breadboard it looks like this

    [​IMG]

    The small orange link isnt needed if you put the Red LED cathode into the same column as the Green LED anode - it just made the picture easier to follow.

    And now the code

    =========================================================

    /*
    LED 2 aspect signal - using 1 output

    */

    // the setup function runs once when you press reset or power the board
    void setup() {
    // initialize digital pin 11 as an output and pin 7 as an input.
    pinMode(11, OUTPUT);
    pinMode(7,INPUT_PULLUP);
    }

    // the loop function runs over and over again forever
    void loop()
    {
    int sensorValue = digitalRead(7);
    if(sensorValue)
    {
    digitalWrite(11, LOW); // Red stop LED on until button pressed
    }
    else
    digitalWrite(11, HIGH); // toggle output so Red LED goes off and Green comes on
    delay(5000); // wait for 5 second
    }

    =========================================================

    There is a delay on detecting the push button, so keep the button presed until the LEDs change, when you release the button the Green will stay on for another 5 seconds, this can be adjusted by changing the value of the delay.

    and the video .....

    https://youtu.be/vDG4h6Z6o1w



    Paul
     
  12. paul_l

    paul_l Staff Member Administrator

    Messages:
    9,848
    Likes Received:
    5,902
    Joined:
    Dec 5, 2015
    and the last installment on LEDs - Traffic Lights

    [​IMG]

    To make the code more readable (wise for the future when you may wish to modify the sketch), I have defined 3 Integers Red, Amber and Green, and set them to the value of the I/O pin they represent.

    ========================================================

    /*
    LED Traffic Light

    To make things easier to follow we will define three integers, called Red amber and Green
    and set them to the value of the digital I/O pin they will use.
    */
    int Red=10;
    int Amber=11;
    int Green=12;

    // the setup function runs once when you press reset or power the board
    void setup() {
    // initialize digital output pins
    pinMode(Red, OUTPUT); //Red LED
    pinMode(Amber, OUTPUT); //Amber - Yellow LED
    pinMode(Green, OUTPUT); //Green LED
    digitalWrite(Red,HIGH); // Turn on Red
    delay(8000); // wait for 8 seconds
    }

    // the loop function runs over and over again forever
    void loop()
    {
    digitalWrite(Amber, HIGH); // Red + Amber
    delay(3000); // wait for 3 seconds
    digitalWrite(Red, LOW); // Red OFF
    digitalWrite(Amber, LOW); // Amber OFF
    digitalWrite(Green, HIGH); //Green ON
    delay(6000); // wait for 6 seconds
    digitalWrite(Green, LOW); // Green OFF
    digitalWrite(Amber, HIGH); // Amber ON
    delay(2000); // wait for 2 seconds
    digitalWrite(Red, HIGH); // Red stop LED on until button pressed
    digitalWrite(Amber, LOW); // Amber OFF
    delay(8000); // wait for 8 seconds
    }

    ========================================================

    And the video can be found here

    https://youtu.be/EH-qumrhg9M



    Paul
     
  13. Toto

    Toto I'm best ignored Staff Member Founder Administrator

    Messages:
    15,419
    Likes Received:
    3,842
    Joined:
    Dec 5, 2017
    Good stuff Paul. I can see that set up being popular. I've got a bit of catching up to do before I achieve that but the editorial looks pretty well explained. ....... Just don't wander too far from the phone. :avatar:

    Nice one. :thumbs:

    Toto
     

Share This Page