Connect with us

Programming for beginners

Discussion in 'Microcontrollers, Programming and IoT' started by Maxcady, Jun 25, 2018.

Scroll to continue with content
  1. Maxcady

    Maxcady

    3
    0
    Jun 25, 2018
    Hi.
    I try to learn to program for arduino a little bit. This time it is to create traffic lights: red led, yellow led and green led. We define 2 cycles with the program. The short cycle is for the yellow light. The long cycle is controlled by the rotary encoder for the time lengh: red and green lights.
    I kind of understand 95% of the code but not completely. Maybe you can help me with that.

    The questions is in the commentary next to the lines.

    Thx a lot.

    Code:
    const int redPin= 7; //red led attach to
    const int yellowPin =8 ; //yellow led attach to
    const int greenPin= 9; //green led attach to
    const int clkPin= 2;
    const int dtPin= 3;
    const int swPin= 6; //the number of the button
    
    int encoderVal = 0;
    
    int state = 0;
    int shortPeriod = 1000;
    int longPeriod = 1000;  // why 1000?????
    int targetCount = shortPeriod;
    int count = 0;
    
    void setup()
    {
        pinMode(clkPin, INPUT);
        pinMode(dtPin, INPUT);
        pinMode(swPin, INPUT);
        digitalWrite(swPin, HIGH);
        pinMode(redPin, OUTPUT);  //set the redPin as an output
        pinMode(yellowPin, OUTPUT);  //set the yellowPin as an output
        pinMode(greenPin, OUTPUT);  //set the greenPin as an output
        Serial.begin(9600);  // start serial port at 9600 bps:
    }
    
    void loop()
    {
      count++;
      int change = getEncoderTurn();
      longPeriod = longPeriod + change * 1000;
      if (digitalRead(swPin) == LOW)
      {
          setLights(HIGH, HIGH, HIGH); '' // why are they all HIGH???
      }
      else
      {
        if (count == targetCount) // Not sure about this.
        {
          setState();
          count = 0;
        }
      }
      delay(1);
    }
    
    void setState(void)
    {
        if (state == 0)
        {
          setLights(HIGH, LOW, LOW);
          targetCount = longPeriod;
          state = 1;
        }
        else if (state == 1)
        {
          setLights(HIGH, HIGH, LOW);
          targetCount = shortPeriod;
          state = 2;
        }
        else if (state == 2)
        {
          setLights(LOW, LOW, HIGH);
          targetCount = longPeriod;
          state = 3;
        }
        else if (state == 3)
        {
          setLights(LOW, HIGH, LOW);
          targetCount = shortPeriod;
          state = 0;
        }
    }
    
    int getEncoderTurn(void)
    {
      static int oldA = HIGH;
      static int oldB = HIGH;
      int result = 0;                          // what is result??
      int newA = digitalRead(clkPin);
      int newB = digitalRead(dtPin);
      if (newA != oldA || newB != oldB)
      {
        // something has changed
        if (oldA == HIGH && newA == LOW)
        {
          result = (oldB * 2 - 1); // I'm lost for this line??
        }
      }
      oldA = newA;
      oldB = newB;
      return result;
    }
    
    // the function to set the led with the specified state(on or off),HIGH is on, and LOW is off
    void setLights(int redState, int yellowState, int greenState)
    {
      digitalWrite(redPin, redState);
      digitalWrite(yellowPin, yellowState);
      digitalWrite(greenPin, greenState);
    }
    
     

    Attached Files:

    Last edited by a moderator: Jun 25, 2018
  2. Kabelsalat

    Kabelsalat

    157
    29
    Jul 5, 2011
    Is this something you have created yourselv or got somewhere from the internet (or school?) ?

    The one big issue I can point out is the lack of comments. To not comment various functions is just a very bad habit to start with.
     
  3. Maxcady

    Maxcady

    3
    0
    Jun 25, 2018
    I am sorry. I am a beginner. I just try to learn by myself. It is from a starter kit that I bought with the materials and codes. There is not much comment in this code. This is all they show. So I can only guess what it is.

    Do you know a website that can help me how to program with an arduino and bluetooth? It would be nice. Thank you. :)
     
  4. Hopup

    Hopup

    253
    36
    Jul 5, 2015
    That code is probably not very good, just by the fact that it's commented very poorly. If you don't already know at least basics of C/CPP, you would benefit lot learning it before doing Arduino programming.
     
  5. Maxcady

    Maxcady

    3
    0
    Jun 25, 2018
    I dont want to become an expert for programming. I just want to know most of arduino programming. My project is to build bb8 in long term. bb8 has arduino in it. That is why.
     
  6. jorgen

    jorgen

    9
    1
    Jul 28, 2018
    If you get this stuff a chunk at a time, it'll all come together, so let's try a random chunk to start with.
    int getEncoderTurn(void), let's 'black-box' this rotary encoder function, first:

    What this does is return a 0, 1 or -1 every time it's called in a program.

    If you turn the encoder one way, you get a 1. The other way, you get a -1. If you don't turn it, you get a 0.

    Encoders have an A and B pulse, the pulse trains are 90 degrees out of phase, when pulse A goes from HI to LO, the arduino reads the value of pulse B.

    If the encoder was turned clockwise, pulse B is HI, if the encoder was turned counter clockwise, pulse B is LO.
    So you can use that info to add or subtract to the length of time lights stay on or the length of time between
    changing to different colored lights or pretty much anything you want to control in an up-down way, kinda like a volume control.

    OK, knowing all that, let's dissect the function:

    Code:
    int getEncoderTurn(void)
    {
      static int oldA = HIGH;
      static int oldB = HIGH;
      int result = 0;             // what is result??
      int newA = digitalRead(clkPin);
      int newB = digitalRead(dtPin);
      if (newA != oldA || newB != oldB)
      {
        // something has changed
        if (oldA == HIGH && newA == LOW)
        {
          result = (oldB * 2 - 1); // I'm lost for this line??
        }
      }int getEncoderTurn(void)
      oldA = newA;
      oldB = newB;
      return result;
    }
    
    Now follow along and look at the corresponding code until this explanation makes sense:

    initialize oldA and oldB to HIGH.
    initialize result to 0;

    read-in the current state of newA and newB with digitalRead().

    if newA and oldA are NOT the same OR newB and oldB are NOT the same

    Check the next conditional (if) to see which way the encoder was turned...
    if oldA is HIGH AND newA is LOW
    result=(oldB * 2)-1

    Here's how that works:
    if oldB==HIGH (0x01), then oldB*2 equals 2, from which we subtract 1 leaving 1.
    if oldB==LOW (0x00), then oldB*2 equals 0, from which we subtract 1 leaving -1.

    if nothing changed, result is still 0.

    save the state of newA in oldA
    save the state of newB in oldB
    return the result.

    That's a good start, you should check up on the 'static' keyword to understand how it affects scope and saves state, since this function saves it's values to two statics for the next time the encoder is turned.
     
  7. (*steve*)

    (*steve*) ¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd Moderator

    25,491
    2,833
    Jan 21, 2010
    Considering that the arduino is programmed in C++, a recommendation that you learn C++ seems to be quite appropriate.
     
  8. jorgen

    jorgen

    9
    1
    Jul 28, 2018
    Yeah, I probably did you a disservice by posting that explanation, I reacted to: 'I kind of understand 95% of the code but not completely'.

    Like Hopup and Steve said, you really do need to 'get your feet wet' with C++. For arduino-level stuff, it's not too hard. Arduino is just a controller and it uses a simplified subset of C++ which makes it easy to use.

    Now don't freak, but I'm going to point you to a C language tutorial site rather than C++. Until you start using classes and OOP, this will work fine for Arduino and will keep it simple for you. I'm also going to point you to a simple C compiler that you don't need to configure any IDE or any PATHs for so you can start practicing with minimal effort, the compiler is for you to learn C with, not to write Arduino code with (you can do that with Arduino's own IDE).

    Later, there are compilers and IDEs specifically for Arduino you can use directly without having to go through the Arduino IDE to flash your controller, but you don't need to get into that yet, for now just get a feel for how C code works, look at some Arduino sketches, run them from the Arduino IDE and keep going on the C tutorials.

    Resources:
    http://forefront.io/a/beginners-guide-to-arduino/
    http://begincprogram.blogspot.com/2009/05/introduction-to-c-language-with.html
    http://www.christian-heffner.de/index.php?page=download&lang=en
    https://create.arduino.cc/projecthub
     
    KJ6EAD likes this.
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-