Download presentation
Presentation is loading. Please wait.
1
IoT Programming the Particle Photon
2
Cloud Service sensor Browser actuator Photon
4
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
5
build.particle.io/ Build: Web IDE
Flash Verify Save
6
Build: Web IDE Code Library Docs Devices Settings
7
New App: void setup() { } void loop() {
8
C stuff Define stuff before you use it. Arrays:
int[] nums = {1,2,3,4}; Constants: Use “const” (not “final”)
9
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.
10
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
11
Pointers Every variable has a name, a value, and a memory address.
Consider: int x = 3; &x refers to the address of variable x.
12
Pointers To store the address of a variable, you need a pointer type. Declare variables with a star: int x = 3; int* ptr = &x;
13
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?
14
int x = 3; int* ptr = &x; int y = *ptr; ADD NAME TYPE VALUE 1000 x int
2000 ptr int* 3000 y
15
int x = 3; int* ptr = &x; int y = *ptr; ADD NAME TYPE VALUE 1000 x int
2000 ptr int* 3000 y
16
int x = 3; int* ptr = &x; int y = *ptr; ADD NAME TYPE VALUE 1000 x int
2000 ptr int* 3000 y
17
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
18
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); }
19
Let’s add a light
20
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
21
Input: Switches Use a pin as digital input.
Can be either a pull-up or pull-down (or neither, technically)
22
Pull-down Switch sits between the input pin and the voltage source.
Switch off = Low voltage to input pin
23
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.
24
Pull-up Switch sits between pin and ground.
Switch off = High voltage reading We generally use this as it is safer.
25
I/O!! D7 -> 220R -> LED-> GND D3 -> SWITCH-> GND
26
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); } }
27
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?
28
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
29
Analog Input The A0-A5 pins can be used for analog input.
They give a value from , telling how much voltage is at that point.
30
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
32
Code setup is same as before - tell what is input and what is output.
digitalRead becomes analogRead digitalWrite becomes analogWrite
33
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?????)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.