David Maiden Mueller
Colin Foe Parker
Yashar Ganjeh
Tim Stutz
Lab three is all about the glorious world of sensors, microprocessor integration, and even delving a little into the world of labView. We were given a Button, Pot, Photoresistor, Force Sensing Resistor, Photo interruptor, Hall Effect Sensor, LED's, a speaker, and a Hobby Servo and told to go to town. I mean, not literally town... because that is quite a walk.... but rather go play/build/tinker/learn. The end goal? Build a electronic musical instrument of some form that uses an Analog sensor for pitch and some other sensor for either articulation or volume. There were a few waypoints along the way.... 1. Read all the sensors as Digital Inputs in the Arduino. 2. Drive LED's off an Arduino GPIO. 3. Read Analog signals using the Arduino. 4. Run a Servo. 5. Play with LabView and try to do the same things again. So we weren't COMPLETELY unsupervised....
So, we met on Thursday all gungho and ready to start and build. Instead, we spent an hour and a half trying to rebuild what we had done the week before. As with all things... all four of us had built the previous weeks lab circuit differently. And everyone wanted to know WHY people chose which resistors or placed what parts where. So, we talked it out and eventually reached an amalgamation of different designs. Now it was time to actually start working on the lab. We went through the data sheets and laid out circuit diagrams for all the individual components. (Refer to Figures 1 through 3)
Figure 1: Circuit Layout
Figure 2: Circuit Layout
Figure 3: Circuit Layout
With this data, we were prepped to tackle sensor integration with the Arduino. We wrote a simple program that would take in sensor values. The system would the light up a LED if on. The code also saved the gpio value into the EEPROM and when all 512 memory locations were filled would dump the values onto the serial port. The code follows.
#include
int SensorPin = 52;
int LEDPIN = 13;
int Value;
int addr;
void setup(){
Serial.begin(9600);
pinMode(SensorPin,INPUT);
pinMode(LEDPIN,OUTPUT);
}
void loop(){
Value = digitalRead(SensorPin);
digitalWrite(LEDPIN,Value);
EEPROM.write(addr,Value);
if (addr == 512){
addr = 0;
while (addr <>
Value =EEPROM.read(addr);
Serial.println(Value, DEC);
addr = addr + 1;
}
addr = 0;
}
addr++;
delay(20);
}
This code worked for all digital pins. We then did the same test with an analog input. We used the same code with an exception of switching the "int SensorPin = 52;" to "int SensorPin = 0;" and the "Value= digitalRead(SensorPin)" to "Value = analogRead(SensorPin)'. As expected, the led did not light up unless the analog signal was truthfully 0. But all the rest of the components worked like a charm. :) The following graph shows the analog output of the arduino after 500 time steps.
Our next task was to get the servo up and running. The great thing about the arduino is that all the hard stuff is already done for us. We don't have to go through all the ports searching for the pwm output timers, or set up clocks to output at the right frequencies, or figure out how to scale the pwm duty cycle to get the servo running. Instead, all we have to do is "#include " set up a class and then tell the servo what to do. We got it up and running in <>
#include
#include
int SensorPin = 0;
int LEDPIN = 13;
int Value;
int addr;
Servo Servo1;
int i;
void setup(){
Serial.begin(9600);
Servo1.attach(12);
// pinMode(SensorPin,INPUT);
pinMode(LEDPIN,OUTPUT);
}
void loop(){
Value = analogRead(SensorPin);
digitalWrite(LEDPIN,Value);
EEPROM.write(addr,Value);
if (addr == 512){
addr = 0;
while (addr <>
Value =EEPROM.read(addr);
Serial.println(Value, DEC);
addr = addr + 1;
}
addr = 0;
}
addr++;
if (i > 5) {
Servo1.write(255);
}
else {
Servo1.write(0);
}
if (i==10){
i=0;
}
i++;
delay(100);
}
Our final task was to make a musical instrument of some form. We immediately decided make something that resembled a thermin. (I guess we aren't all that inventive...) However, we added a little twist. We would use the Hall Effect Sensor to control the pitch of the sound. We attached the Hall Effect sensor to an analog input of the Arduino. We would measure the value and using a gpio bitbang an output signal resembling a pwm. David devised a cool way to augment the signal to give it the traditional 8 bit Mario Kart video game sound that we all grew up with. The arduino output signal was then fed into a Nand gate in addition to an input from the photo interrupter. The result was fed into the NPN BJT and controlled a speaker output. The result? We developed a really cool electric instrument that augmented frequency by moving a magnet and controlled articulation with a blocker in the photo interrupter. Tim became pretty adept at making some hip tunes. Watch the embedded video below!!!
The code is following.....
const int sensorPin = 2; // pin that the sensor is attached to
const int ledPin = 22; // pin that the LED is attached to
const int outputOne = 24;
const int sensorGate = 52;
int outVal = 0; // value of the output
// variables:
int sensorValue = 0; // the sensor value
int sensorMin = 1023; // minimum sensor value
int sensorMax = 0; // maximum sensor value
int time = 1;
double frequency = 1;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(outputOne, OUTPUT);
pinMode(sensorGate, INPUT);
// turn on LED to signal the start of the calibration period:
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
// calibrate during the first five seconds
while (millis() <>
sensorValue = analogRead(sensorPin);
// record the maximum sensor value
if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}
// record the minimum sensor value
if (sensorValue <>
sensorMin = sensorValue;
}
}
// signal the end of the calibration period
digitalWrite(13, LOW);
Serial.begin(9600);
}
void loop() {
// read the sensor:
sensorValue = analogRead(sensorPin);
// apply the calibration to the sensor reading
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
// in case the sensor value is outside the range seen during calibration
sensorValue = constrain(sensorValue, 0, 255);
// fade the LED using the calibrated value:
//analogWrite(ledPin, sensorValue);
time = sensorValue*15;
//Serial.print("Sensor Value: ");
//Serial.print(sensorValue);
//Serial.print(" Time: ");
//Serial.print(time);
//Serial.print(" Frequency: ");
//Serial.println(frequency);
digitalWrite(outputOne, digitalRead(sensorGate));
for(int i=0; i<25;>
digitalWrite(ledPin, HIGH);
//outVal = digitalRead(ledPin);
//Serial.println(outVal);
delayMicroseconds(time);
digitalWrite(ledPin, LOW);
//outVal = digitalRead(ledPin);
//Serial.println(outVal);
delayMicroseconds(time);
}
}
The final aspect of our lab was to play with LabView.... After trying to figure out which computer actually had working DAQ's, what the difference was between a waveform graph and waveform chart, and what sampling frequency we wanted we were able to develop a while loop that sampled data the voltage out of a power supply and displayed it onto the computer screen. We added some buttons to light up software LED's and display funky stuff. All in all, the LabView part was just to get acquainted with the DAQ and the graphical programming layout.
So I guess thats all folks! Lab 3 was fun. And brought back good memories of nintendo games!