On Github mhellar / physcompga2
Slides are here: http://mhellar.github.io/iot3/
Grab the code here: http://bit.ly/1Wh6jdM
Button.ino
Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of "on time" is called the pulse width. To get varying analog values, you change, or modulate, that pulse width. If you repeat this on-off pattern fast enough with an LED for example, the result is as if the signal is a steady voltage between 0 and 5v controlling the brightness of the LED.
In the graphic below, the green lines represent a regular time period. This duration or period is the inverse of the PWM frequency. In other words, with Arduino's PWM frequency at about 500Hz, the green lines would measure 2 milliseconds each. A call to analogWrite() is on a scale of 0 - 255, such that analogWrite(255) requests a 100% duty cycle (always on), and analogWrite(127) is a 50% duty cycle (on half the time) for example.
int val = analogRead(analogPin); // read the input pin
int LEDGreen=9; int LEDBlue=10; int LEDRed=11; int sensorPin=0; int val; void setup(){ Serial.begin(9600); } void loop(){ val=analogRead(sensorPin); Serial.print("sensor = " ); Serial.println(val); if (val<340) { analogWrite(LEDRed,255); analogWrite(LEDBlue,0); analogWrite(LEDGreen,0); } else if (val<680) { analogWrite(LEDRed,0); analogWrite(LEDBlue,255); analogWrite(LEDGreen,0); } else if (val<1024) { analogWrite(LEDRed,0); analogWrite(LEDBlue,0); analogWrite(LEDGreen,255); } delay(10); }
int a = 5; int b = 10; int c = 20; void setup() // run once, when the sketch starts { Serial.begin(9600); // set up Serial library at 9600 bps Serial.println("Here is some math: "); Serial.print("a = "); Serial.println(a); Serial.print("b = "); Serial.println(b); Serial.print("c = "); Serial.println(c); Serial.print("a + b = "); // add Serial.println(a + b); Serial.print("a * c = "); // multiply Serial.println(a * c); Serial.print("c / b = "); // divide Serial.println(c / b); Serial.print("b - c = "); // subtract Serial.println(b - c); } void loop() // we need this to be here even though its empty { }
const int redPin = 9; // the pin that the LED is attached to const int bluePin = 10; // the pin that the LED is attached to const int greenPin = 11; // the pin that the LED is attached to int incomingByte; // a variable to read incoming serial data into void setup() { // initialize serial communication: Serial.begin(9600); // initialize the LED pin as an output: pinMode(redPin, OUTPUT); pinMode(bluePin, OUTPUT); pinMode(greenPin, OUTPUT); } void loop() { // see if there's incoming serial data: if (Serial.available() > 0) { // read the oldest byte in the serial buffer: incomingByte = Serial.read(); // if it's a r , turn on the red LED: if (incomingByte == 'r') { digitalWrite(greenPin, LOW); digitalWrite(bluePin, LOW); digitalWrite(redPin, HIGH); } // if it's a g , turn on the green LED: if (incomingByte == 'g') { digitalWrite(greenPin, HIGH); digitalWrite(bluePin, LOW); digitalWrite(redPin, LOW); } / if it's a b , turn on the blue LED: if (incomingByte == 'b') { digitalWrite(greenPin, LOW); digitalWrite(bluePin, HIGH); digitalWrite(redPin, LOW); } } }
#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 } { myservo.write(pos); delay(15); // in steps of 1 degree // tell servo to go to position in variable 'pos' // 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 } </servo.h>