IoT Programming the Particle Photon
Cloud Service sensor Browser actuator Photon
Pins Input Pin - reads voltage at that point in the circuit Output Pin - allows voltage to flow out from that point in the circuit Can be digital or analog
build.particle.io/ Build: Web IDE Flash Verify Save
Build: Web IDE Code Library Docs Devices Settings
New App: void setup() { } void loop() {
C stuff Define stuff before you use it. Arrays: int[] nums = {1,2,3,4}; Constants: Use “const” (not “final”)
C(++) Strings Photons let you use Strings and some String functions like you’re used to: String message = “message”; You should know that underneath it is an array of characters with a null character at the end.
C Characters char letter = ‘c’; But chars are really ints. int index = ‘c’ - ‘a’; // index is now 2. if (letter >= ‘a’ && letter <= ‘z’) // if lowercase letter
Pointers Every variable has a name, a value, and a memory address. Consider: int x = 3; &x refers to the address of variable x.
Pointers To store the address of a variable, you need a pointer type. Declare variables with a star: int x = 3; int* ptr = &x;
Pointers If you have a pointer variable, you can retrieve the value of the variable it points to. int x = 3; int* ptr = &x; int y = *ptr; // What’s the value of y?
int x = 3; int* ptr = &x; int y = *ptr; ADD NAME TYPE VALUE 1000 x int 2000 ptr int* 3000 y
int x = 3; int* ptr = &x; int y = *ptr; ADD NAME TYPE VALUE 1000 x int 2000 ptr int* 3000 y
int x = 3; int* ptr = &x; int y = *ptr; ADD NAME TYPE VALUE 1000 x int 2000 ptr int* 3000 y
Photon stuff There are constants (ints) named after the pins and also for INPUT, OUTPUT, HIGH, LOW, … Spark.function Spark.variable Be careful with the terminology
Hello World void setup(){ RGB.control(true); } void loop(){ RGB.color(255,0,0); delay(2000); RGB.color(0,255,0); delay(2000); RGB.color(0,0,255); delay(2000); }
Let’s add a light
Controlling the light int ledPin = D7; void setup(){ pinMode(ledPin, OUTPUT); } ... digitalWrite(ledPin, HIGH); // light on delay(1000); // wait one second digitalWrite(ledPin, LOW); // light off
Input: Switches Use a pin as digital input. Can be either a pull-up or pull-down (or neither, technically)
Pull-down Switch sits between the input pin and the voltage source. Switch off = Low voltage to input pin
Pull-down Issue with pull-down: What if circuit is built with pin still in output mode? Can cause a short circuit before the code is flashed.
Pull-up Switch sits between pin and ground. Switch off = High voltage reading We generally use this as it is safer.
I/O!! D7 -> 220R -> LED-> GND D3 -> SWITCH-> GND
Code int ledPin = D7; int switchPin = D3; void setup(){ pinMode(ledPin, OUTPUT); pinMode(switchPin, INPUT_PULLUP); } void loop(){ if(digitalRead(switchPin) == LOW){ // Button is pressed digitalWrite(ledPin,HIGH); else{ digitalWrite(ledPin, LOW); } }
Code Design Write a Morse code flasher. When you press the button the flashing begins. What are the variables? Functions? How will we write it?
Before you go for it... String letters[] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", // A-I ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", // J-R "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." // S-Z }; Timing: Dot: 200 Dash: 600 Between dots and dashes: 200 Between letters of a word: 400 Between words: 1400 Between messages: 5000
Analog Input The A0-A5 pins can be used for analog input. They give a value from 0 - 4095, telling how much voltage is at that point.
Analog Output Pin DAC (Digital-to-Analog converter) gives true analog output Other A pins (A4 & A5) give “fake” analog output using Pulse Width Modulation Analog outputs are between 0 and 255
Code setup is same as before - tell what is input and what is output. digitalRead becomes analogRead digitalWrite becomes analogWrite
Try a program Set up A5 as an analog output. Connect A5 -> 220 Resistor -> LED -> Ground Set up A0 as an analog input. Connect 3V3 -> Photoresistor -> A0 -> 1K Resistor -> Ground Program: The darker the sensor, the brighter the LED. (Note: Is very sensitive. Try different things. I divided by 500?????)