Workshop 3 plan

  • Recap on digital and analogue i/o
  • Creating your own functions

/*
 * This code shows you how to create your own function in 
 * another 'void' section and then call it in the main loop.
 * It also shows how you can pass a variable to it, in this case
 * a name. Upload and then open the serial monitor to see what
 * happens
 * 
 * Created by Luke Woodbury 16th March 2016
 */

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);  //start serial communication
}

void loop() {
  // put your main code here, to run repeatedly:
sayhello("Luke");  //call the function we have created below and pass it a variable
                   
}

void sayhello(char yourName[ ]){  //our function we have created
  Serial.print("Hello ");  //print "Hello " over serial
  Serial.print(yourName);  //print the name variable passed in from main loop
  Serial.println("!");  //print an exclamation mark and then a line break (println)
  delay(1000);  //wait for a second
}

  • Check out this Instructable and follow it
    • Try adjusting the delays and the LED values to create different colours and animations
    • Refer to last weeks lesson and add a potentiometer to your circuit
    • Use the pot to fade a colour on your RGB LED (hint: look at last weeks lesson!) 
    • Use another pot to fade 2 colours at once (hint: you will need to duplicate your variables and give them new names, then duplicate the code and insert the new variables)
  • Learning how to control your programme with the 'if' operator
    • Look at 'potPrint' sketch and adapt as described

/* This code simply prints out the value of a potentiometer
 * on an analog pin. The potentiometer should have one side
 * to 5V, one side to GND, and the middle to an analogue in
 * pin. Once the sketch is uploaded, open the serial monitor
 * (top right button) to view the reading.
 * 
 * Look at the Arduino site reference page and the description of
 * thw 'if...else' operator under the 'Structure' section. Use
 * this operator to also print 'left' or 'right' if the pot is
 * turned either side of the halfway mark:
 * https://www.arduino.cc/en/Reference/HomePage
 *  
 *  Luke Woodbury 17th March 2016
 */

const int potPin = 0;  //define our pin for our pot, it won't change so is a const(ant) integer
int potVal;  //define a variable integer (whole number) to hold the value from the pot reading

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);  //start serial communication with the computer
}

void loop() {
  // put your main code here, to run repeatedly:
  potVal = analogRead(potPin);  //read the pot pin and store the value in potVal

Serial.println(potVal);  //print out the value of potVal and add a line break (println)
}

  • Again, look at the Arduino Reference page, this time under the 'Math' section at the 'map' operator. This page gives you an example of how to scale one value to another, in this case an analogue input value to to a PWM output value. We used this in the fade example last week, you will need it for the next step!
  • Use your new 'if' skills and your knowledge of fading and mapping values to fade one of your RGB LED values with one side of the pot and another with the other side.
  • For a grand finale, create a programme that enables you to use a single pot to fade through all 3 colours of your LED, for bonus points do it in a way that enables colour blending so you get a rainbow effect as you turn the pot. 
  • Investigate this Instructable as inspiration, see how you could adapt it for your circuit and create your own lamp.