Skip to content
Browse files

Added examples

  • Loading branch information...
1 parent 30ab8a3 commit 45c539ad9cc1c02dcd81fbbb21c597d1fedb036e @pictux pictux committed Oct 17, 2012
View
32 examples/AnalogInOutSerial/AnalogInOutSerial.ino
@@ -0,0 +1,32 @@
+/*
+ Analog input, serial output
+ */
+
+// Analog input pin that the potentiometer is attached to
+const int analogInPin = A0;
+
+int sensorValue = 0; // value read from the pot
+
+void setup() {
+ // initialize serial communications at 9600 bps:
+ Serial.begin(9600);
+}
+
+void loop() {
+ // read the analog in value:
+ sensorValue = analogRead(analogInPin);
+
+ // print the results to the serial monitor:
+ Serial.print("sensor = " );
+ Serial.println(sensorValue);
+
+ // wait 2 milliseconds before the next loop
+ // for the analog-to-digital converter to settle
+ // after the last reading:
+ delay(2);
+}
+
+
+
+
+
View
29 examples/Fade01/Fade01.ino
@@ -0,0 +1,29 @@
+/*
+ Fade
+ */
+
+int led = 9;
+int brightness = 0; // how bright the LED is
+int fadeAmount = 5; // how many points to fade the LED by
+
+void setup() {
+ pinMode(led, OUTPUT);
+}
+
+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);
+}
+
View
29 examples/Fade02/Fade02.ino
@@ -0,0 +1,29 @@
+/*
+ Fade #02
+*/
+
+int ledPin = 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);
+ }
+}
+
+
View
27 examples/StreetLampFade01/StreetLampFade01.ino
@@ -0,0 +1,27 @@
+
+/*
+ Streetlamp - fade
+ */
+
+const int analogInPin = A0;
+const int analogOutPin = 9;
+
+int sensorValue = 0;
+int outputValue = 0;
+
+void setup() {
+ pinMode(analogOutPin, OUTPUT);
+}
+
+void loop() {
+ sensorValue = analogRead(analogInPin);
+
+ outputValue = sensorValue / 4;
+
+ analogWrite(analogOutPin, outputValue);
+
+ delay(2);
+}
+
+
+
View
33 examples/StreetLampFade02/StreetLampFade02.ino
@@ -0,0 +1,33 @@
+/*
+ Streetlamp - fade #02
+ */
+
+const int analogInPin = A0;
+const int analogOutPin = 9;
+
+int sensorValue = 0;
+int outputValue = 0;
+
+void setup() {
+ Serial.begin(9600);
+}
+
+void loop() {
+ 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);
+
+ delay(2);
+}
+
+
View
35 examples/buttonFlag/buttonFlag.ino
@@ -0,0 +1,35 @@
+/*
+ State change
+*/
+
+const int buttonPin = 2;
+const int ledPin = 9;
+
+int buttonState = 0;
+int buttonFlag = 0;
+
+void setup() {
+ pinMode(ledPin, OUTPUT);
+ pinMode(buttonPin, INPUT);
+}
+
+void loop(){
+ buttonState = digitalRead(buttonPin);
+
+ if (buttonState == HIGH) {
+ //if the pushbutton, check buttonFlag value
+ if (buttonFlag == 1) {
+ buttonFlag = 0;
+ digitalWrite(ledPin, LOW);
+ delay(500); //to avoid bouncing
+ } else {
+ buttonFlag = 1;
+ digitalWrite(ledPin, HIGH);
+ delay(500); //to avoid bouncing
+ }
+ }
+}
+
+
+
+
View
57 examples/buttonModulo/buttonModulo.ino
@@ -0,0 +1,57 @@
+/*
+ State change (modulo)
+ */
+
+const int buttonPin = 2;
+const int ledPin = 9;
+
+// counter for the number of button presses
+int buttonPushCounter = 0;
+// current state of the button
+int buttonState = 0;
+// previous state of the button
+int lastButtonState = 0;
+
+void setup() {
+ pinMode(buttonPin, INPUT);
+ pinMode(ledPin, OUTPUT);
+}
+
+
+void loop() {
+ buttonState = digitalRead(buttonPin);
+
+ // compare the buttonState to its previous state
+ if (buttonState != lastButtonState) {
+ // if the state has changed, increment the counter
+ if (buttonState == HIGH) {
+ // if the current state is HIGH then the button
+ // wend from off to on:
+ buttonPushCounter++;
+ }
+ }
+ // save the current state as the last state,
+ //for next time through the loop
+ lastButtonState = buttonState;
+
+ // turns on the LED every 2 button pushes by
+ // checking the modulo of the button push counter.
+ // the modulo function gives you the remainder of
+ // the division of two numbers:
+ if (buttonPushCounter % 2 == 0) {
+ digitalWrite(ledPin, HIGH);
+ } else {
+ digitalWrite(ledPin, LOW);
+ }
+}
+
+
+
+
+
+
+
+
+
+
+
View
48 examples/buttonStateChange/buttonStateChange.ino
@@ -0,0 +1,48 @@
+/*
+ State change detection (edge detection)
+ */
+
+const int buttonPin = 2;
+const int ledPin = 9;
+
+// current state of the button
+int buttonState = 0;
+// previous state of the button
+int lastButtonState = 0;
+
+void setup() {
+ pinMode(buttonPin, INPUT);
+ pinMode(ledPin, OUTPUT);
+}
+
+
+void loop() {
+ buttonState = digitalRead(buttonPin);
+
+ // compare the buttonState to its previous state
+ if (buttonState != lastButtonState) {
+ // if the state has changed, do something!
+ if (buttonState == HIGH) {
+ digitalWrite(ledPin, HIGH);
+ }
+ else {
+ digitalWrite(ledPin, LOW);
+ }
+ }
+ // save the current state as the last state,
+ //for next time through the loop
+ lastButtonState = buttonState;
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
View
0 Rome/dev/postTweet/postTweet.ino → examples/postTweet/postTweet.ino
File renamed without changes.
View
17 examples/pwmBasic01/pwmBasic01.ino
@@ -0,0 +1,17 @@
+int led = 9;
+
+void setup() {
+ pinMode(led, OUTPUT);
+}
+
+void loop() {
+ digitalWrite(led, HIGH);
+ delay(10); //then try 1 here
+ digitalWrite(led, LOW);
+ delay(1); //..and 10 here
+}
+
+
+
+
+
View
16 examples/pwmBasic02/pwmBasic02.ino
@@ -0,0 +1,16 @@
+int led = 9;
+
+void setup() {
+ pinMode(led, OUTPUT);
+}
+
+void loop() {
+ for (int i=0; i < 10; i++) {
+ for (int y=0; y < 100; y++) {
+ digitalWrite(led, HIGH);
+ delay(i); // 0, 1, 2, 3 ...
+ digitalWrite(led, LOW);
+ delay(10 -i ); // 10, 9, 8, 7 ...
+ }
+ }
+}
View
0 Rome/dev/readTweet/readTweet.ino → examples/readTweet/readTweet.ino
File renamed without changes.
View
36 examples/simpleStreetlamp/simpleStreetlamp.ino
@@ -0,0 +1,36 @@
+/*
+ Simple Streetlamp
+ */
+
+const int analogInPin = A0;
+const int ledPin = 9;
+
+int sensorValue = 0;
+
+void setup() {
+ Serial.begin(9600);
+ pinMode(ledPin, OUTPUT);
+}
+
+void loop() {
+ sensorValue = analogRead(analogInPin);
+
+ Serial.print("sensor = " );
+ Serial.print(sensorValue);
+ Serial.print("; led = " );
+
+ if (sensorValue < 250) {
+ Serial.println("ON");
+ digitalWrite(ledPin, HIGH);
+ } else {
+ Serial.println("OFF");
+ digitalWrite(ledPin, LOW);
+ }
+
+ delay(2);
+}
+
+
+
+
+
View
62 examples/stupidBot/stupidBot.ino
@@ -0,0 +1,62 @@
+/*
+ stupidBot
+ created 22 sept 2012
+ by pitusso
+
+ This example code is in the public domain.
+
+ */
+
+// These constants won't change. They're used to give names
+// to the pins used:
+const int sharp01Pin = A0; // DX Analog input pin that the 1st sharp is attached to
+const int sharp02Pin = A1; // RX Analog input pin that the 2nd sharp is attached to
+
+const int motor1Pin = 9; // Analog output pin that the MOSFET is attached to
+const int motor2Pin = 10; // Analog outpu pin that the MOSFET is attached to
+
+int sharp01Value = 0; // value read from the 1st sharp
+int sharp02Value = 0; // value read from the 2st sharp
+
+int speedMotor01 = 255;
+int speedMotor02 = 255;
+
+void setup() {
+ // initialize serial communications at 9600 bps:
+ Serial.begin(9600);
+ pinMode(motor1Pin, OUTPUT);
+ pinMode(motor2Pin, OUTPUT);
+}
+
+void loop() {
+ //run motors
+ analogWrite(motor1Pin, speedMotor01);
+ analogWrite(motor2Pin, speedMotor02);
+
+ // read the analog in value:
+ sharp01Value = analogRead(sharp01Pin);
+ sharp02Value = analogRead(sharp02Pin);
+
+ // print the results to the serial monitor:
+ Serial.print("sharp01 = " );
+ Serial.print(sharp01Value);
+ Serial.print("\t sharp02 = ");
+ Serial.println(sharp02Value);
+
+
+ if (sharp01Value > 130) {
+ //turn left
+ speedMotor02 = 0;
+ } else {
+ speedMotor02 = 255;
+ }
+
+ if (sharp02Value > 130) {
+ //turn left
+ speedMotor01 = 0;
+ } else {
+ speedMotor01 = 255;
+ }
+
+ delay(2);
+}
View
38 examples/trafficLights/trafficLights.ino
@@ -0,0 +1,38 @@
+/*
+ Basic Traffic Lights
+ */
+
+void setup() {
+ // initialize the digital pin as an output.
+
+ //green led
+ pinMode(7, OUTPUT);
+ //yellow led
+ pinMode(8, OUTPUT);
+ //red led
+ pinMode(9, OUTPUT);
+}
+
+void loop() {
+ // turn the green LED on
+ digitalWrite(7, HIGH);
+ //...and wait for 5 seconds
+ delay(5000);
+
+ // turn the green LED off
+ digitalWrite(7, LOW);
+ // ...the yellow LED on
+ digitalWrite(8, HIGH);
+ //...and wait for 1 second
+ delay(1000);
+
+ // turn the yellow LED off
+ digitalWrite(8, LOW);
+ // ...the red LED on
+ digitalWrite(9, HIGH);
+ //...and wait for 5 seconds
+ delay(5000);
+
+ // turn the red LED off
+ digitalWrite(9, LOW);
+}

0 comments on commit 45c539a

Please sign in to comment.
Something went wrong with that request. Please try again.