Category: Assignment-10B

Assignment-10B : Potentiometers


potentiometer

 

 

/*
  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(). 
 
 The circuit:
 * Potentiometer attached to analog input 0
 * center pin of the potentiometer to the analog pin
 * one side pin (either one) to ground
 * the other side pin to +5V
 * LED anode (long leg) attached to digital output 13
 * LED cathode (short leg) attached to ground
 
 * Note: because most Arduinos have a built-in LED attached 
 to pin 13 on the board, the LED is optional.
 
 
 Created by David Cuartielles
 Modified 16 Jun 2009
 By Tom Igoe
 
 http://arduino.cc/en/Tutorial/AnalogInput
 
 */

int sensorPin = 0;    // select the input pin for the potentiometer
int ledPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);  
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);    
  // turn the ledPin on
  digitalWrite(ledPin, HIGH);  
  // stop the program for  milliseconds:
  delay(sensorValue);          
  // turn the ledPin off:        
  digitalWrite(ledPin, LOW);   
  // stop the program for for  milliseconds:
  delay(sensorValue);                  
}

Circuit 8 & 13

Circuit 8:

Circuit 13:

Fritzing:

circut8

circut13

Code:

/*
  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(). 
 
 The circuit:
 * Potentiometer attached to analog input 0
 * center pin of the potentiometer to the analog pin
 * one side pin (either one) to ground
 * the other side pin to +5V
 * LED anode (long leg) attached to digital output 13
 * LED cathode (short leg) attached to ground
 
 * Note: because most Arduinos have a built-in LED attached 
 to pin 13 on the board, the LED is optional.
 
 
 Created by David Cuartielles
 Modified 16 Jun 2009
 By Tom Igoe
 
 http://arduino.cc/en/Tutorial/AnalogInput
 
 */

int sensorPin = 0;    // select the input pin for the potentiometer
int ledPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);  
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);    
  // turn the ledPin on
  digitalWrite(ledPin, HIGH);  
  // stop the program for  milliseconds:
  delay(sensorValue);          
  // turn the ledPin off:        
  digitalWrite(ledPin, LOW);   
  // stop the program for for  milliseconds:
  delay(sensorValue);                  
}
////comment that code ^ and uncomment this code for circuit 13
//int sensePin = 2; // the pin the FSR is attached to
//int ledPin = 9; // the pin the LED is attached to (use one capable of PWM)
//void setup() {
// Serial.begin(9600);
// pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
//}
//void loop() {
// int value = analogRead(sensePin) / 4; //the voltage on the pin divded by 4 (to 
// //scale from 10 bits (0-1024) to 8 (0-255)
// analogWrite(ledPin, value); //sets the LEDs intensity proportional to 
// //the pressure on the sensor
// Serial.println(value); //print the value to the debug window
//}


Circuit 13: Squeezing

Circuit 13CIRC13 Fritzing Diagram_bb

/*
 * Force Sensitive Resistor Test Code
 *
 * The intensity of the LED will vary with the amount of pressure on the sensor
 */

int sensePin = 2;    // the pin the FSR is attached to
int ledPin = 9;      // the pin the LED is attached to (use one capable of PWM)

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);  // declare the ledPin as an OUTPUT
}

void loop() {
  int value = analogRead(sensePin) / 4; //the voltage on the pin divded by 4 (to scale from 10 bits (0-1024) to 8 (0-255)
  analogWrite(ledPin, value);        //sets the LEDs intensity proportional to the pressure on the sensor
  Serial.println(value);              //print the value to the debug window
}

Circuit 8: Potentiometer

Circuit 8CIRC8 Fritzing Diagram_bb

/*
  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(). 
 
 The circuit:
 * Potentiometer attached to analog input 0
 * center pin of the potentiometer to the analog pin
 * one side pin (either one) to ground
 * the other side pin to +5V
 * LED anode (long leg) attached to digital output 13
 * LED cathode (short leg) attached to ground
 
 * Note: because most Arduinos have a built-in LED attached 
 to pin 13 on the board, the LED is optional.
 
 
 Created by David Cuartielles
 modified 30 Aug 2011
 By Tom Igoe
 
 This example code is in the public domain.
 
 http://arduino.cc/en/Tutorial/AnalogInput
 
 */

int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);  
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);    
  // turn the ledPin on
  digitalWrite(ledPin, HIGH);  
  // stop the program for  milliseconds:
  delay(sensorValue);          
  // turn the ledPin off:        
  digitalWrite(ledPin, LOW);   
  // stop the program for for  milliseconds:
  delay(sensorValue);                  
}

Assignment 10

These assignments are due next Wednesday, October 29th. 

We begin with three or four straightforward tutorials on sensors, and then make a creative assignment. (You can probably complete the sensor tutorials during class.)

Assignment-10A: PushButtons (Circuit 07)

  • Connect CIRC07 (Pushbuttons), on page 20 of the ARDX booklet.
  • Document the project in a blog post with a video, Fritzing diagram, and source code.
  • (Optional) Thoughts, observations or questions? Blog them too.

Assignment-10B: Variable Resistors (Circuits 08 & 13)

  • Connect CIRC08 (Twisting), on page 22 of the ARDX booklet. Take a photo or video, and then,

After this, 

  • Connect CIRC13 (Squeezing), on page 32 of the ARDX booklet. It’s almost identical to CIRC08, which you just did.
  • Document the project(s) in a blog post with a video, Fritzing diagram, and source code.
  • (Optional) Thoughts, observations or questions? Blog them too.

Assignment-10C: Temperature (Circuit 10) + Serial

  • Connect CIRC07 (Pushbuttons), on page 20 of the ARDX booklet.
  • Note that the temperature sensor looks alot like the transistor! The temperature sensor is marked TMP36.
  • Also note that the temperature sensor is polarized, so make sure it’s inserted correctly. Otherwise you could get burned!
  • Try adding serial communication so that the temperature value is reported back to your laptop or desktop computer.
  • Document the project in a blog post with a video, Fritzing diagram, and source code.
  • (Optional) Thoughts, observations or questions? Blog them too.

Assignment 10-SensorDisplay

Create an interactive screen display in Processing that responds to one or more sensors, whose data is obtained from the Arduino over serial communications. For example, you might start from one of the following concepts (others are possible, too):
Screen Shot 2014-10-22 at 10.30.00 PM

  • An interactive game which is played with a button, slider, force sensor, etc. (Consider how even a single button can be used to create a game like Flappy Bird.)
  • A dynamic abstraction or  “interactive painting” whose properties change when the user turns some knobs — or which changes autonomously, according (for example) to the current temperature and ambient lighting conditions.
  • An information visualization which displays the diagnostic status of some sensor values (like an accelerometer), indicating movements in the world.

    cosmosmorph2-640x371

In creating your assignment, consider the following:

  • Nothing too fancy, but it might be nice to build a little cardboard box to hold your components.
  • You might need to extend your wires using solder.
  • Feel free to design a prototype which serves as a maquette or scale model for a much larger scenario. For example, you could pretend that the FSR in your kit actually represents an enormous pressure pad for detecting foot traffic .
  • Optionally, it can be Halloween related.

wilfred-clavilux (Thomas Wilfred’s Clavilux (~1920) controlled a dynamic light abstraction from a set of potentiometers.)

Things to WATCH OUT FOR (CAUTION!): 

Here’s a helpful generalization: If your sensor only has two leads, you’ll need to create a voltage divider. This is done by adding a resistor in a certain pattern. Without this, you can CAUSE A SHORT CIRCUIT and fry your Arduino! This here is a very helpful article which explains voltage dividers for resistive sensors.

This also explains some helpful stuff:

Requirements: 

This won’t surprise you, but please include:

  • A blog post, categorized Assignment-10-SensorDisplay
  • A short statement of 100-150 words describing your concept, process, and evaluation of your work
  • An embedded YouTube video demonstrating your project working
  • A scan of any paper sketches you made
  • A photograph of your project, perhaps in use
  • Some screenshots from your Processing display
  • A Fritzing diagram of your Arduino circuit
  • Your Arduino and Processing code, embedded using WP-Syntax

Here’s some Arduino and Processing code to get you started. The Arduino communicates serial data for two sensors to the Processing project, which parses the data and then displays the results.

Arduino code (v. 1.0.6):

// This Arduino program reads two analog signals, 
// such as from two potentiometers, and transmits 
// the digitized values over serial communication. 

int sensorValue0 = 0;  // variable to store the value coming from the sensor
int sensorValue1 = 0;  // variable to store the value coming from the other sensor

void setup() {
  Serial.begin(9600);  // initialize serial communications    
}

void loop() {
  
  // Read the value from the sensor(s):
  sensorValue0 = analogRead (A0);  // reads value from Analog input 0
  sensorValue1 = analogRead (A1);  // reads value from Analog input 1    

  Serial.print ("A"); 
  Serial.println (sensorValue0); 
  Serial.print ("B"); 
  Serial.println (sensorValue1);  
  
  delay (50);   // wait a fraction of a second, to be polite
}

/*
The resulting serial data looks like this (for example): 

A903
B412
A900
B409
A898
B406
A895
B404
A893
B404
...etcetera.
*/

Processing code (v. 2.2.1):

// This Processing program reads serial data for two sensors,
// presumably digitized by and transmitted from an Arduino. 
// It displays two rectangles whose widths are proportional
// to the values 0-1023 received from the Arduino.

// Import the Serial library and create a Serial port handler
import processing.serial.*;
Serial myPort;   

// Hey you! Use these variables to do something interesting. 
// If you captured them with analog sensors on the arduino, 
// They're probably in the range from 0 ... 1023:
int valueA;  // Sensor Value A
int valueB;  // Sensor Value B

//------------------------------------
void setup() {
  size(1024, 200);

  // List my available serial ports
  int nPorts = Serial.list().length; 
  for (int i=0; i < nPorts; i++) {
    println("Port " + i + ": " + Serial.list()[i]);
  } 

  // Choose which serial port to fetch data from. 
  // IMPORTANT: This depends on your computer!!!
  // Read the list of ports printed by the code above,
  // and try choosing the one like /dev/cu.usbmodem1411
  // On my laptop, I'm using port #4, but yours may differ.
  String portName = Serial.list()[4]; 
  myPort = new Serial(this, portName, 9600);
  serialChars = new ArrayList();
}

//------------------------------------
void draw() {

  // Process the serial data. This acquires freshest values. 
  processSerial();

  background (150);  

  // draw a pink rectangle displaying valueA:
  fill (255, 200, 200);   
  rect (0, 0, valueA, 100);

  // draw a blue rectangle displaying valueB:
  fill (200, 200, 255); 
  rect (0, 100, valueB, 100); 
  fill (0); 

  // draw the letters A and B:
  text ("A", 20, 60); 
  text ("B", 20, 160);
}


//---------------------------------------------------------------
// The processSerial() function acquires serial data byte-by-byte, 
// as it is received, and when it is properly captured, modifies
// the appropriate global variable. 
// You won't have to change anything unless you want to add additional sensors. 

/*
The (expected) received serial data should look something like this:
 
 A903
 B412
 A900
 B409
 A898
 B406
 A895
 B404
 A893
 B404
 ...etcetera.
 */

ArrayList serialChars;      // Temporary storage for received serial data
int whichValueToAccum = 0;  // Which piece of data am I currently collecting? 
boolean bJustBuilt = false; // Did I just finish collecting a datum?

void processSerial() {

  while (myPort.available () > 0) {
    char aChar = (char) myPort.read();

    // You'll need to add a block like one of these 
    // if you want to add a 3rd sensor:
    if (aChar == 'A') {
      bJustBuilt = false;
      whichValueToAccum = 0;
    } else if (aChar == 'B') {
      bJustBuilt = false;
      whichValueToAccum = 1;
    } else if (((aChar == 13) || (aChar == 10)) && (!bJustBuilt)) {
      // If we just received a return or newline character, build the number: 
      int accum = 0; 
      int nChars = serialChars.size(); 
      for (int i=0; i < nChars; i++) { 
        int n = (nChars - i) - 1; 
        int aDigit = ((Integer)(serialChars.get(i))).intValue(); 
        accum += aDigit * (int)(pow(10, n));
      }

      // Set the global variable to the number we captured.
      // You'll need to add another block like one of these 
      // if you want to add a 3rd sensor:
      if (whichValueToAccum == 0) {
        valueA = accum;
        // println ("A = " + valueA);
      } else if (whichValueToAccum == 1) {
        valueB = accum;
        // println ("B = " + valueB);
      }

      // Now clear the accumulator
      serialChars.clear();
      bJustBuilt = true;
      
    } else if ((aChar >= 48) && (aChar < = 57)) {
      // If the char is between '0' and '9', save it.
      int aDigit = (int)(aChar - '0'); 
      serialChars.add(aDigit);
    }
  }
}
Comments Off on Assignment 10 Posted in