Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th...

31
Arduino Course Technology Will Save Us - Tim Brooke 10th August 2013 Friday, 9 August 13

Transcript of Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th...

Page 1: Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th August 2013 Friday, 9 August 13

Arduino CourseTechnology Will Save Us - Tim Brooke

10th August 2013

Friday, 9 August 13

Page 2: Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th August 2013 Friday, 9 August 13

Arduino Projects

http://www.instructables.com/id/20-Unbelievable-Arduino-Projects/Friday, 9 August 13

Page 3: Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th August 2013 Friday, 9 August 13

Blink

/*  Blink  Turns on an LED on for one second, then off for one second, repeatedly.   */ // Pin 13 has an LED connected on most Arduino boards.// give it a name:int led = 13;

// the setup routine runs once when you press reset:void setup() {                  // initialize the digital pin as an output.  pinMode(led, OUTPUT);     }

// the loop routine runs over and over again forever:void loop() {  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)  delay(1000);               // wait for a second  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW  delay(1000);               // wait for a second}

Friday, 9 August 13

Page 4: Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th August 2013 Friday, 9 August 13

Fade

// the loop routine runs over and over again forever:void loop()  {   // set the brightness of pin 9:  analogWrite(led, brightness);    

  // change the brightness for next time through the loop:  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:   if (brightness == 0 || brightness == 255) {    fadeAmount = -fadeAmount ;   }       // wait for 30 milliseconds to see the dimming effect      delay(30);                            }

/* Fade  This example shows how to fade an LED on pin 9 using the analogWrite() function.  This example code is in the public domain. */

int led = 9;           // the pin that the LED is attached toint brightness = 0;    // how bright the LED isint fadeAmount = 5;    // how many points to fade the LED by

// the setup routine runs once when you press reset:void setup()  {   // declare pin 9 to be an output:  pinMode(led, OUTPUT);}

Friday, 9 August 13

Page 5: Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th August 2013 Friday, 9 August 13

Digital Read Serial

Friday, 9 August 13

Page 6: Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th August 2013 Friday, 9 August 13

Digital Read Code/*  DigitalReadSerial Reads a digital input on pin 2, prints the result to the serial monitor   This example code is in the public domain. */

// digital pin 2 has a pushbutton attached to it. Give it a name:int pushButton = 2;

// the setup routine runs once when you press reset:void setup() {  // initialize serial communication at 9600 bits per second:  Serial.begin(9600);  // make the pushbutton's pin an input:  pinMode(pushButton, INPUT);}

// the loop routine runs over and over again forever:void loop() {  // read the input pin:  int buttonState = digitalRead(pushButton);  // print out the state of the button:  Serial.println(buttonState);  delay(1);        // delay in between reads for stability}

Friday, 9 August 13

Page 7: Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th August 2013 Friday, 9 August 13

Debounce Button

Friday, 9 August 13

Page 8: Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th August 2013 Friday, 9 August 13

Debounce Button

const int buttonPin = 2;    // the number of the pushbutton pinconst int ledPin = 13;      // the number of the LED pin

// Variables will change:int ledState = HIGH;         // the current state of the output pinint buttonState;             // the current reading from the input pinint lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are long's because the time, measured in miliseconds,// will quickly become a bigger number than can be stored in an int.long lastDebounceTime = 0;  // the last time the output pin was toggledlong debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {  pinMode(buttonPin, INPUT);  pinMode(ledPin, OUTPUT);

  // set initial LED state  digitalWrite(ledPin, ledState);}

void loop() {  // read the state of the switch into a local variable:  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button   // (i.e. the input went from LOW to HIGH),  and you've waited   // long enough since the last press to ignore any noise:  

   

  // If the switch changed, due to noise or pressing:  if (reading != lastButtonState) {    // reset the debouncing timer    lastDebounceTime = millis();  }

if ((millis() - lastDebounceTime) > debounceDelay) {    // whatever the reading is at, it's been there for longer    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:    if (reading != buttonState) {      buttonState = reading;

      // only toggle the LED if the new button state is HIGH      if (buttonState == HIGH) {        ledState = !ledState;      }    }  }    // set the LED:  digitalWrite(ledPin, ledState);

  // save the reading.  Next time through the loop,  // it'll be the lastButtonState:  lastButtonState = reading;}

Friday, 9 August 13

Page 9: Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th August 2013 Friday, 9 August 13

Analog Read Serial

Friday, 9 August 13

Page 10: Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th August 2013 Friday, 9 August 13

Analog Read-Serial/*  AnalogReadSerial  Reads an analog input on pin 0, prints the result to the serial monitor.  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.  This example code is in the public domain. */

// the setup routine runs once when you press reset:void setup() {  // initialize serial communication at 9600 bits per second:  Serial.begin(9600);}

// the loop routine runs over and over again forever:void loop() {  // read the input on analog pin 0:  int sensorValue = analogRead(A0);  // print out the value you read:  Serial.println(sensorValue);  delay(1);        // delay in between reads for stability}

Friday, 9 August 13

Page 11: Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th August 2013 Friday, 9 August 13

Smoothingconst int numReadings = 10;

int readings[numReadings];      // the readings from the analog inputint index = 0;                  // the index of the current readingint total = 0;                  // the running totalint average = 0;                // the average

int inputPin = A0;

void setup(){  // initialize serial communication with computer:  Serial.begin(9600);                     // initialize all the readings to 0:   for (int thisReading = 0; thisReading < numReadings; thisReading++)    readings[thisReading] = 0;          }

void loop() {  // subtract the last reading:  total= total - readings[index];           // read from the sensor:    readings[index] = analogRead(inputPin);   // add the reading to the total:  total= total + readings[index];         // advance to the next position in the array:    index = index + 1;                    

  // if we're at the end of the array...  if (index >= numReadings)                  // ...wrap around to the beginning:     index = 0;                          

  // calculate the average:  average = total / numReadings;           // send it to the computer as ASCII digits  Serial.println(average);     delay(1);        // delay in between reads for stability            }

Friday, 9 August 13

Page 12: Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th August 2013 Friday, 9 August 13

Fade Version 2

Friday, 9 August 13

Page 13: Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th August 2013 Friday, 9 August 13

Fade 2 Codeint ledPin = 9;    // LED connected to digital pin 9

void setup()  {   // nothing happens in setup }

void loop()  {   // fade in from min to max in increments of 5 points:  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {     // sets the value (range from 0 to 255):    analogWrite(ledPin, fadeValue);             // wait for 30 milliseconds to see the dimming effect        delay(30);                              }

  // fade out from max to min in increments of 5 points:  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {     // sets the value (range from 0 to 255):    analogWrite(ledPin, fadeValue);             // wait for 30 milliseconds to see the dimming effect        delay(30);                              } }

Friday, 9 August 13

Page 14: Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th August 2013 Friday, 9 August 13

Analog in-out serial

Friday, 9 August 13

Page 15: Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th August 2013 Friday, 9 August 13

Analog in-out Code// These constants won't change.  They're used to give names// to the pins used:const int analogInPin = A0;  // Analog input pin that the potentiometer is attached toconst int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0;        // value read from the potint outputValue = 0;        // value output to the PWM (analog out)

void setup() {  // initialize serial communications at 9600 bps:  Serial.begin(9600); }

void loop() {  // read the analog in value:  sensorValue = analogRead(analogInPin);              // map it to the range of the analog out:  outputValue = map(sensorValue, 0, 1023, 0, 255);    // change the analog out value:  analogWrite(analogOutPin, outputValue);          

  // print the results to the serial monitor:  Serial.print("sensor = " );                         Serial.print(sensorValue);        Serial.print("\t output = ");        Serial.println(outputValue);  

  // wait 2 milliseconds before the next loop  // for the analog-to-digital converter to settle  // after the last reading:  delay(2);                     }

Friday, 9 August 13

Page 16: Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th August 2013 Friday, 9 August 13

Calibration

Friday, 9 August 13

Page 17: Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th August 2013 Friday, 9 August 13

Calibration Code// These constants won't change:const int sensorPin = A0;    // pin that the sensor is attached toconst int ledPin = 9;        // pin that the LED is attached to

// variables:int sensorValue = 0;         // the sensor valueint sensorMin = 1023;        // minimum sensor valueint sensorMax = 0;           // maximum sensor value

void setup() {  // turn on LED to signal the start of the calibration period:  pinMode(13, OUTPUT);  digitalWrite(13, HIGH);

  // calibrate during the first five seconds   while (millis() < 5000) {    sensorValue = analogRead(sensorPin);

    // record the maximum sensor value    if (sensorValue > sensorMax) {      sensorMax = sensorValue;    }

    // record the minimum sensor value    if (sensorValue < sensorMin) {      sensorMin = sensorValue;    }  }

  // signal the end of the calibration period  digitalWrite(13, LOW);}

void loop() {  // read the sensor:  sensorValue = analogRead(sensorPin);

  // apply the calibration to the sensor reading  sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);

  // in case the sensor value is outside the range seen during calibration  sensorValue = constrain(sensorValue, 0, 255);

  // fade the LED using the calibrated value:  analogWrite(ledPin, sensorValue);}

Friday, 9 August 13

Page 18: Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th August 2013 Friday, 9 August 13

Serial In

Friday, 9 August 13

Page 19: Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th August 2013 Friday, 9 August 13

Serial In Code

void setup() {  // initialize serial communication:  Serial.begin(9600);    // initialize the LED pins:      for (int thisPin = 2; thisPin < 7; thisPin++) {        pinMode(thisPin, OUTPUT);      } }

void loop() {  // read the sensor:  if (Serial.available() > 0) {    int inByte = Serial.read();    // do something different depending on the character received.      // The switch statement expects single number values for each case;    // in this exmaple, though, you're using single quotes to tell    // the controller to get the ASCII value for the character.  For     // example 'a' = 97, 'b' = 98, and so forth:

   

switch (inByte) {    case 'a':          digitalWrite(2, HIGH);      break;    case 'b':          digitalWrite(3, HIGH);      break;    case 'c':          digitalWrite(4, HIGH);      break;    case 'd':          digitalWrite(5, HIGH);      break;    case 'e':          digitalWrite(6, HIGH);      break;    default:      // turn all the LEDs off:      for (int thisPin = 2; thisPin < 7; thisPin++) {        digitalWrite(thisPin, LOW);      }    }   }}

Friday, 9 August 13

Page 20: Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th August 2013 Friday, 9 August 13

Arraysint timer = 100;           // The higher the number, the slower the timing.int ledPins[] = {   2, 7, 4, 6, 5, 3 };       // an array of pin numbers to which LEDs are attachedint pinCount = 6;           // the number of pins (i.e. the length of the array)

void setup() {  // the array elements are numbered from 0 to (pinCount - 1).  // use a for loop to initialize each pin as an output:  for (int thisPin = 0; thisPin < pinCount; thisPin++)  {    pinMode(ledPins[thisPin], OUTPUT);        }}

void loop() {  // loop from the lowest pin to the highest:  for (int thisPin = 0; thisPin < pinCount; thisPin++) {     // turn the pin on:    digitalWrite(ledPins[thisPin], HIGH);       delay(timer);                      // turn the pin off:    digitalWrite(ledPins[thisPin], LOW);    

  }

  // loop from the highest pin to the lowest:  for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {     // turn the pin on:    digitalWrite(ledPins[thisPin], HIGH);    delay(timer);    // turn the pin off:    digitalWrite(ledPins[thisPin], LOW);  }}

Friday, 9 August 13

Page 21: Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th August 2013 Friday, 9 August 13

RGB Led

Friday, 9 August 13

Page 22: Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th August 2013 Friday, 9 August 13

RGB Code

/*Adafruit ArduinoRGB LED*/

int redPin = 11;int greenPin = 10;int bluePin = 9;

void setup(){ pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); }

void loop(){ setColor(255, 0, 0); // red delay(1000); setColor(0, 255, 0); // green delay(1000); setColor(0, 0, 255); // blue delay(1000); setColor(255, 255, 0); // yellow delay(1000); setColor(80, 0, 80); // purple delay(1000); setColor(0, 255, 255); // aqua delay(1000);}

void setColor(int red, int green, int blue){ analogWrite(redPin, red); analogWrite(greenPin, green); analogWrite(bluePin, blue); }

Friday, 9 August 13

Page 23: Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th August 2013 Friday, 9 August 13

Sweep

Friday, 9 August 13

Page 24: Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th August 2013 Friday, 9 August 13

Sweep Code// Sweep// by BARRAGAN <http://barraganstudio.com> // This example code is in the public domain.

#include <Servo.h>  Servo myservo;  // create servo object to control a servo                 // a maximum of eight servo objects can be created  int pos = 0;    // variable to store the servo position  void setup() {   myservo.attach(9);  // attaches the servo on pin 9 to the servo object }   void loop() {   for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees   {                                  // in steps of 1 degree     myservo.write(pos);              // tell servo to go to position in variable 'pos'     delay(15);                       // waits 15ms for the servo to reach the position   }   for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees   {                                    myservo.write(pos);              // tell servo to go to position in variable 'pos'     delay(15);                       // waits 15ms for the servo to reach the position   } }

Friday, 9 August 13

Page 25: Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th August 2013 Friday, 9 August 13

Control Stick

Friday, 9 August 13

Page 26: Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th August 2013 Friday, 9 August 13

Control Code// Controlling a servo position using a potentiometer (variable resistor) // by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>

#include <Servo.h>  Servo myservo;  // create servo object to control a servo  int potpin = 0;  // analog pin used to connect the potentiometerint val;    // variable to read the value from the analog pin  void setup() {   myservo.attach(9);  // attaches the servo on pin 9 to the servo object }  void loop() {   val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)   val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)   myservo.write(val);                  // sets the servo position according to the scaled value   delay(15);                           // waits for the servo to get there }

Friday, 9 August 13

Page 27: Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th August 2013 Friday, 9 August 13

Tone Generator

Friday, 9 August 13

Page 28: Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th August 2013 Friday, 9 August 13

Tone Generator Code#include "pitches.h"

// notes in the melody:int melody[] = {  NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};

// note durations: 4 = quarter note, 8 = eighth note, etc.:int noteDurations[] = {  4, 8, 8, 4,4,4,4,4 };

void setup() {  // iterate over the notes of the melody:  for (int thisNote = 0; thisNote < 8; thisNote++) {

    // to calculate the note duration, take one second     // divided by the note type.    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.    int noteDuration = 1000/noteDurations[thisNote];    tone(8, melody[thisNote],noteDuration);

    // to distinguish the notes, set a minimum time between them.    // the note's duration + 30% seems to work well:    int pauseBetweenNotes = noteDuration * 1.30;    delay(pauseBetweenNotes);    // stop the tone playing:    noTone(8);  }}

void loop() {  // no need to repeat the melody.}

/************************************************* * Public Constants *************************************************/

#define NOTE_B0  31#define NOTE_C1  33#define NOTE_CS1 35#define NOTE_D1  37#define NOTE_DS1 39#define NOTE_E1  41#define NOTE_F1  44#define NOTE_FS1 46#define NOTE_G1  49#define NOTE_GS1 52#define NOTE_A1  55#define NOTE_AS1 58#define NOTE_B1  62#define NOTE_C2  65#define NOTE_CS2 69#define NOTE_D2  73#define NOTE_DS2 78#define NOTE_E2  82#define NOTE_F2  87#define NOTE_FS2 93#define NOTE_G2  98#define NOTE_GS2 104#define NOTE_A2  110#define NOTE_AS2 117#define NOTE_B2  123#define NOTE_C3  131#define NOTE_CS3 139#define NOTE_D3  147#define NOTE_DS3 156#define NOTE_E3  165#define NOTE_F3  175#define NOTE_FS3 185#define NOTE_G3  196#define NOTE_GS3 208#define NOTE_A3  220#define NOTE_AS3 233#define NOTE_B3  247#define NOTE_C4  262#define NOTE_CS4 277#define NOTE_D4  294#define NOTE_DS4 311#define NOTE_E4  330#define NOTE_F4  349#define NOTE_FS4 370#define NOTE_G4  392#define NOTE_GS4 415#define NOTE_A4  440#define NOTE_AS4 466#define NOTE_B4  494#define NOTE_C5  523#define NOTE_CS5 554#define NOTE_D5  587#define NOTE_DS5 622#define NOTE_E5  659#define NOTE_F5  698#define NOTE_FS5 740#define NOTE_G5  784#define NOTE_GS5 831#define NOTE_A5  880#define NOTE_AS5 932#define NOTE_B5  988#define NOTE_C6  1047#define NOTE_CS6 1109#define NOTE_D6  1175#define NOTE_DS6 1245#define NOTE_E6  1319#define NOTE_F6  1397#define NOTE_FS6 1480#define NOTE_G6  1568#define NOTE_GS6 1661#define NOTE_A6  1760#define NOTE_AS6 1865#define NOTE_B6  1976#define NOTE_C7  2093#define NOTE_CS7 2217#define NOTE_D7  2349#define NOTE_DS7 2489#define NOTE_E7  2637#define NOTE_F7  2794#define NOTE_FS7 2960#define NOTE_G7  3136#define NOTE_GS7 3322#define NOTE_A7  3520#define NOTE_AS7 3729#define NOTE_B7  3951#define NOTE_C8  4186#define NOTE_CS8 4435#define NOTE_D8  4699#define NOTE_DS8 4978

http://arduino.cc/en/Tutorial/Tone

Friday, 9 August 13

Page 29: Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th August 2013 Friday, 9 August 13

Theremin Circuit ??

Friday, 9 August 13

Page 30: Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th August 2013 Friday, 9 August 13

Therimin Codevoid loop() { sensorVal = analogRead(0); potVal = analogRead(1); buttonVal = digitalRead(BUTTON); Serial.println(sensorVal); note = map(sensorVal, lowLight, highLight, lowNote, highNote); note = constrain(note, lowNote, highNote); tempo = map(potVal, 0, 1023, lowTempo, highTempo); tempo = constrain(tempo, lowTempo, highTempo); if(buttonVal == LOW) { duration = tempo/2; //Figure out the tempo tone(BUZZER, note); //play note delay(duration); //wait noTone(BUZZER); //turn off buzzer delay(tempo-duration);//wait } else { noTone(BUZZER); } }

#define BUZZER 11#define BUTTON 10

int sensorVal;int potVal;int buttonVal;int note;int tempo;

//limits sectionint lowLight = 200; // Calibration of the light meterint highLight = 600;int lowNote = 31; // lowest noteint highNote = 4978; // highest noteint lowTempo = 5;int highTempo = 1000;int duration;

void setup() { Serial.begin(9600); pinMode(BUZZER, OUTPUT); pinMode(BUTTON, INPUT_PULLUP);}

Friday, 9 August 13

Page 31: Arduino Course - Technology Will Save UsArduino Course Technology Will Save Us - Tim Brooke 10th August 2013 Friday, 9 August 13

The End

Friday, 9 August 13