User Tools

Site Tools


courses:intro.prototyping.fall.2015.nov15

Intro to Arduino

We went through the basics of Arduino. What it is and what you can do with it. The examples we went through were from the Arduino website and were blinking LEDs, using a button, using a potentiometer etc.

What I told group B but forgot to tell group A is that when building things on breadboard and with Arduino it's not easy to document what you did, in case you want to tear it up and try another time. However, the graphics they have on the Arduino page are made with the free software Fritzing which consists of easy to read images of most common parts and components. Use this to record what you do, so you don't have to remember from time to time.

Group A got me talking about the command Millis() and the way it is better to use for controlling timed things, instead of delay. For example, you can use millis to make two things happen in different rhythms, which delay can't. Delay halts the entire program, where you with millis instead you look at a timer how much time has passed, and meanwhile you can do other things.

The following code is an example of code that will blink two LEDs in different paces.

int ledPin1 =  13;      // the numbers of the LED pins
int ledPin2 =  2;      
 
int ledState1 = LOW;             // ledStates used to set the LEDs
int ledState2 = LOW;            
 
unsigned long previousMillis1 = 0;        // will store last time LEDs were updated
unsigned long previousMillis2 = 0;       
 
long interval1 = 750;           // intervals at which to blink (milliseconds)
long interval2 = 200;         
 
 
void setup() {
  // set the digital pins as output:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
}
 
void loop() {
  //getting the current time
  unsigned long currentMillis = millis();
 
  if (currentMillis - previousMillis1 >= interval1) 
  {
    // save the last time you blinked the LED
    previousMillis1 = currentMillis;
 
    // if the LED is off turn it on and vice-versa:
    if (ledState1 == LOW) 
    {
      ledState1 = HIGH;
    } 
    else 
    {
      ledState1 = LOW;
    }
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin1, ledState1);
  }
 
 
  if (currentMillis - previousMillis2 >= interval2) 
  {
    // save the last time you blinked the LED
    previousMillis2 = currentMillis;
 
    // if the LED is off turn it on and vice-versa:
    if (ledState2 == LOW) 
    {
      ledState2 = HIGH;
    } 
    else 
    {
      ledState2 = LOW;
    }
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin2, ledState2);
  }
 
}
courses/intro.prototyping.fall.2015.nov15.txt · Last modified: 2015/11/17 04:20 by rickard