2008 3.05: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 30: Line 30:




=== Example ===
=== Code Examples ===


<pre lang="python">
"Call and respond" Arduino Code
import time, random
<source lang="c">
import gst, pygst
int potPin = 2;    // select the input pin for the potentiometer
int ledPin = 13;  // select the pin for the LED
int val = 0;      // variable to store the value coming from the sensor


# pipe2_desc = 'videotestsrc ! ffmpegcolorspace ! ximagesink'
void setup() {
oggpath = '/home/murtaugh/Videos/personalchef.ogg'
  pinMode(ledPin, OUTPUT);  // declare the ledPin as an OUTPUT
# pipe2_desc = 'filesrc location=%s ! oggdemux ! theoradec ! ffmpegcolorspace ! ximagesink' % oggpath
  Serial.begin(9600);
player_desc = 'filesrc location=%s ! oggdemux ! theoradec ! videobalance name=bal contrast=2 ! ffmpegcolorspace ! ximagesink'
}
player = gst.parse_launch(player_desc % oggpath)
videobalance = player.get_by_name("bal")


player.set_state(gst.STATE_PLAYING)
void loop() {
  digitalWrite(ledPin, HIGH);  // turn the ledPin on
  if (Serial.available() > 0) {
    Serial.read();              // read (and ignore) the call byte
    val = analogRead(potPin);    // read the value from the sensor
    digitalWrite(ledPin, LOW);  // turn the ledPin off
    Serial.print(val, DEC);
    Serial.println();
  }
}
</source>


for i in range(50):
"Call and respond" Python Code
time.sleep(1)
 
rval = random.random()
<source lang="python">
videobalance.set_property("contrast", rval)
import serial
ser = serial.Serial('/dev/ttyUSB0', 9600) # your serial port may be different
player.set_state(gst.STATE_NULL)
while 1:
</pre>
ser.write('x') # call
line = ser.readline() # and read the response
print line
</source>

Latest revision as of 00:30, 16 May 2008

Tech Day 3.05

In-class Problem Set

Analog Sensor / Python Class

Wiring the analog sensor

We use a voltage divider, as in this example from the Making Things Talk text:

File:Makingthings voltagedivider.png

Write the Python program

Create a "call and response" arduino application. program the arduino to read one or more inputs (*at least one* analog), send the data only in response to reading a byte from it's serial port (the call)

Write a simple "proof of concept" receiver (in Python)

Relativizing Analog Input

Use two variables, maxsensor and minsensor, to remember the highest and lowest sensor value your program as seen. Use these values to translate the sensor reading at any moment into a "scaler" that goes from 0 (when the sensor value is at the minimum) to 1 (sensor is at maximum).

Use your scaler to control a visual PyGame property (size, color, position of a drawn shape, image).

c.

CREATE a Class called Sensor to encapsulate your min / max sensor reading code.


Code Examples

"Call and respond" Arduino Code

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

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

void loop() {
  digitalWrite(ledPin, HIGH);  // turn the ledPin on
  if (Serial.available() > 0) {
     Serial.read();               // read (and ignore) the call byte
     val = analogRead(potPin);    // read the value from the sensor
     digitalWrite(ledPin, LOW);   // turn the ledPin off
     Serial.print(val, DEC);
     Serial.println();
  }
}

"Call and respond" Python Code

import serial
ser = serial.Serial('/dev/ttyUSB0', 9600) # your serial port may be different
while 1:
	ser.write('x')		# call
	line = ser.readline()	# and read the response
	print line