Anonymous avatar Anonymous committed 6bed469

arduino_article: codes for the Arduino article

Comments (0)

Files changed (10)

arduino_article/README

+The source code in this directory are meant to be used as resources for the article 
+"Learning  to Program the Arduino" to be published in Linux Journal tentatively in November, 2011.
+(http://echorand.me/writings/)

arduino_article/README~

+The source code in this directory are meant to be used as resources for the article 
+"Getting started with Inotify" published in "Linux For You",  April 2011. 
+(http://echorand.me/writings/)

arduino_article/listing1.pde

+/*
+ Blink
+ Turns on an LED on for one second, then off for one second, repeatedly.
+
+ This example code is in the public domain.
+ */
+
+void setup() {
+ // initialize the digital pin as an output.
+ // Pin 13 has an LED connected on most Arduino boards:
+ pinMode(13, OUTPUT);
+}
+
+void loop() {
+ digitalWrite(13, HIGH);   // set the LED on
+ delay(1000);              // wait for a second
+ digitalWrite(13, LOW);    // set the LED off
+ delay(1000);              // wait for a second
+}

arduino_article/listing2.pde

+/* Multiple LED Blinking program
+  Amit Saha
+
+*/  
+
+// constants won't change. Used here to
+// set pin numbers:
+const int numPins = 3;
+const int ledPin [] =  {11,12,13};      // the number of the LED pins
+
+int interval = 100;           // interval at which to blink (milliseconds)
+
+void setup() {
+  // Iterate over each of the pins and set them as output
+  for(int i=0;i<numPins;i++)
+    pinMode(ledPin[i], OUTPUT);
+}
+
+/* Loop until death */
+void loop()
+{
+  for(int i=0;i<numPins;i++)
+  {
+    digitalWrite(ledPin[i],HIGH);
+    delay(interval);
+    digitalWrite(ledPin[i],LOW);
+    delay(interval);
+  }
+}
+

arduino_article/listing3.pde

+/* Using potentiometer to fade on/off a LED
+*  Original code notice below:
+* ------------------------------
+* Demonstrates analog input by reading an analog sensor on analog
+* pin 0 and turning on and off a light emitting diode(LED) connected to
+*  digital pin 13.
+* The amount of time the LED will be on and off depends on the value obtained by
+* analogRead().
+* Created by David Cuartielles
+* Modified 16 Jun 2009
+* By Tom Igoe
+* http://arduino.cc/en/Tutorial/AnalogInput
+*/
+
+int sensorPin = 0;
+int ledPin = 13;
+int sensorValue = 0;
+
+void setup() {
+     //declare the ledPin as an OUTPUT:
+     pinMode(ledPin, OUTPUT);
+     Serial.begin(9600);
+}
+
+void loop() {
+	sensorValue = analogRead(sensorPin);//
+
+	digitalWrite(ledPin, HIGH);
+
+	delay(sensorValue);
+
+	digitalWrite(ledPin, LOW);
+
+	delay(sensorValue);
+}

arduino_article/listing3.pde~

+/* Using potentiometer to fade on/off a LED
+*  Original code notice below:
+* ------------------------------
+* Demonstrates analog input by reading an analog sensor on analog
+* pin 0 and turning on and off a light emitting diode(LED) connected to
+*  digital pin 13.
+* The amount of time the LED will be on and off depends on the value obtained by
+* analogRead().
+* Created by David Cuartielles
+* Modified 16 Jun 2009
+* By Tom Igoe
+* http://arduino.cc/en/Tutorial/AnalogInput
+*/
+
+int sensorPin = 0;
+int ledPin = 13;
+int sensorValue = 0;
+
+void setup() {
+     //declare the ledPin as an OUTPUT:
+     pinMode(ledPin, OUTPUT);
+     Serial.begin(9600);
+}
+
+void loop() {
+	sensorValue = analogRead(sensorPin);//
+
+	digitalWrite(ledPin, HIGH);
+
+	delay(sensorValue);
+
+	digitalWrite(ledPin, LOW);
+
+	delay(sensorValue);
+}

arduino_article/listing4.pde

+/* Flex sensor + LED 
+/* Analog Input
+* Demonstrates analog input by reading an analog sensor on analog
+* pin 0 and turning on and off a light emitting diode(LED) connected to
+  digital pin 13.
+* The amount of time the LED will be on and off depends on the value obtained by
+* analogRead().
+* Created by David Cuartielles
+* Modified 16 Jun 2009
+* By Tom Igoe
+* http://arduino.cc/en/Tutorial/AnalogInput
+* Modified 16 July, 2011
+* By Amit Saha
+* Current code was tested with a Flex sensor
+*/
+int sensorPin = 0; /*Flex sensor pin */
+int ledPin = 13; /* LED pin*/
+
+void setup() {
+  pinMode(ledPin, OUTPUT);
+}
+
+void loop() {
+  
+  int loop=1;
+  float sensorValues=0.0,delaytime;
+  for(loop=1;loop <=10 ;loop++)
+  {
+     sensorValues = sensorValues + analogRead(sensorPin);
+  }
+      
+  //Use the average as a delay value
+  delaytime = sensorValues/10;
+  
+  digitalWrite(ledPin, HIGH);
+  delay(delaytime);
+  digitalWrite(ledPin, LOW);
+  
+delay(delaytime);
+}
+
+

arduino_article/listing5.pde

+/* Serial communication demo: +1*/
+
+/* Server program*/
+
+int number;
+
+void setup() 
+{ 
+  // Open the serial connection, 9600 baud
+  Serial.begin(9600);
+ } 
+
+void loop() 
+{ 
+  // Get the data "packet" 
+  // Wait for some data to arrive  
+  if (Serial.available()>1) {
+      //operation=Serial.read();
+      number=Serial.read();
+     
+      Serial.println(number + 1);      
+    }  
+    
+}

arduino_article/listing6.pde

+/*Client for Serial communication*/
+
+import processing.serial.*;
+
+Serial myPort; // The serial port
+
+// initial variables:
+int i = 1; // counter
+char inData;
+
+void setup () {
+
+size(400, 350); // window size
+
+// List all the available serial ports
+println(Serial.list());
+
+// Pick up the first port, since I usually have just Arduino connected
+// Please make sure the correct port is selected here
+myPort = new Serial(this, Serial.list()[0], 9600);
+myPort.clear();
+
+// set inital background:
+
+background(255); }
+
+void draw () {
+
+  myPort.write(4);
+  //myPort.write(5);
+
+  if (myPort.available() > 0) {
+    inData = (char)myPort.read(); //Typecast it to the corresponding chracter for the
+      // ASCII value
+    serialEvent();
+   }
+
+}
+
+void serialEvent () {
+  System.out.println(inData);
+}
+

arduino_article/listing7.pde

+/*
+ * servo1: servo1.pde
+ 
+ * Servo control from the Serial port
+ *
+ * Slower, faster, Center and Stop a Servo with a LED Blinky
+ * Created: 1 June, 2011, Amit Saha (http://echorand.me)
+ * Adapted from http://principialabs.com/arduino-serial-servo-control/
+ */
+
+/** Adjust these values for your servo and setup, if necessary **/
+int servoPin     =  2;    // control pin for servo motor
+int minPulse     =  500;  // minimum servo position
+int maxPulse     =  3000; // maximum servo position
+int turnRate     =  10;  // servo turn rate increment (larger value, faster rate)
+int refreshTime  =  20;   // time (ms) between pulses (50Hz)
+
+int OFF=0; // This variable will be used to get/set the status of the servo
+
+/** The Arduino will calculate these values for you **/
+int centerServo;         // center servo position
+int pulseWidth;          // servo pulse width
+int moveServo;           // raw user input
+long lastPulse   = 0;    // recorded time (ms) of the last pulse
+
+/* LED setup*/
+int ledPin=13;
+
+
+void setup() {
+  pinMode(ledPin, OUTPUT); // LED Blink
+  pinMode(servoPin, OUTPUT);  // Set servo pin as an output pin
+  centerServo = maxPulse - ((maxPulse - minPulse)/2);
+  pulseWidth = 0;   
+  Serial.begin(9600);
+  Serial.println("      Arduino Serial Servo Control");
+  Serial.println("Keys:'(s)lower' or '(f)aster', spacebar to center and o to stop");
+  Serial.println();
+  
+  moveServo = 60;
+}
+
+void loop() {
+  // wait for serial input
+  if (Serial.available() > 0) {
+    // read the incoming byte:
+    moveServo = Serial.read();
+    
+    // ASCII 's' is 115, ASCII 'f' is 102, 'o' is 111, 'spacebar' is 32
+    if (moveServo == 115) { pulseWidth = pulseWidth - turnRate; OFF=0;} //slower
+    if (moveServo == 102) { pulseWidth = pulseWidth + turnRate;OFF=0; } //faster
+    if (moveServo == 32) { pulseWidth = centerServo; OFF=0;} //center
+    if (moveServo == 111) { OFF= 1;} //STOP
+
+    // limit the servo pulse at min and max
+    if (pulseWidth > maxPulse) { pulseWidth = maxPulse; }  
+    if (pulseWidth < minPulse) { pulseWidth = minPulse; } 
+
+  }
+
+  // pulse the servo every 20 ms (refreshTime) with current pulseWidth
+  // this will hold the servo's position if unchanged, or move it if changed
+  if (OFF == 0)
+  {
+    /* Turn ON the LED*/
+    digitalWrite(ledPin,HIGH);
+    
+    if (millis() - lastPulse >= refreshTime) {
+      digitalWrite(servoPin, HIGH);   // start the pulse
+      delayMicroseconds(pulseWidth);  // pulse width
+      digitalWrite(servoPin, LOW);    // stop the pulse
+      lastPulse = millis();           // save the time of the last pulse    
+    } 
+  }
+  else
+  {  
+     /* Turn OFF the LED*/
+    digitalWrite(ledPin,LOW);
+
+    //Stop the servo    
+    digitalWrite(servoPin, LOW);
+  }
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.