Tuesday, October 26, 2010

ARDUINO codes, fixes, ideas

01/6 /2010
Today we have achieved a lot within our ardiuno project. we found a section of code that allows us to play music via a piazo. the notes represent different voltage values that are sent to the piazo to make different tones. so it initally came with twinkle twinkle little star. Nic G wrote out mary had a little lamb and we got that to run on the piazo. then we put on the motor shield which allowes us to run two servo motors and a bunch of other thingns we have yet to have the chance to experiment with. with the servo it runs by degree's and greater/lesser values to determine when it should run. while we were doing this i noticed that it stopped when placed over the metal siding of the table. nic related this discovery to our earlier problems that we had where the arduino simply stopped working because it was shorting on the metal strip. We resolved the problem by placing a slice of paper under the Arduino board and now it works smoothly. The piezo program will be fun to work with because we can make a variety of simple songs.
Tags: arduino

Arduino Potentiometer Program

ard1.eml
ard2.eml
ard3.eml

These pictures are of the Arduino setup with the potentiometer circuit. The program measures the resistance of the potentiometer and translates it to a specific angle on the servo. So as you turn the potentiometer dial, the servo motor turns at nearly the same rate.

Arduino Piezo music

http://webmail.aol.com/30272-111/aim-2/en-us/mail/get-attachment.as...

This is the arduino playing a song with a piezo.

Arduino Piezo Melody

AduinoMelody1.pde


int speakerPin = 9;

int length = 15; // the number of notes
char notes[] = "ccggaagffeeddc "; // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;

void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}

void playNote(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}

void setup() {
pinMode(speakerPin, OUTPUT);
}

void loop() {
for (int i = 0; i < length; i++) {
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
} else {
playNote(notes[i], beats[i] * tempo);
}

// pause between notes
delay(tempo / 2);
}
}
//plays a melody using a piezo speaker



http://arduino.cc/en/Tutorial/Melody

Arduino Servo Sweep

ArduinoServoSweep1.pde



// Sweep
// by BARRAGAN a href="http://barraganstudio.com">http://barraganstudio.com>

#include

Servo myservo; // create servo object to control a servo

int pos = 0; // variable to store the servo position

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}



http://arduino.cc/en/Tutorial/Sweep








here is another piece of code i found that makes an attached led blink, but at irregular rates.


/* Heartbeat
* Paul Badger 2009
* Demonstates how to use an array with millis to achieve irregular rhythms
* This function still uses one delay but it never leaves the LEDpin / LED in
* the ON state.
*/


long heartBeatArray[] = {
50, 100, 15, 1200};
int hbeatIndex = 1; // this initialization is important or it starts on the "wrong foot"
long prevMillis;

int LEDpin = 13;

void setup()
{
Serial.begin(9600);
pinMode(LEDpin, OUTPUT);
}

void loop()
{
heartBeat(1.0); // try changing the parameter
}

void heartBeat(float tempo){
if ((millis() - prevMillis) > (long)(heartBeatArray[hbeatIndex] * tempo)){
hbeatIndex++;
if (hbeatIndex > 3) hbeatIndex = 0;

if ((hbeatIndex % 2) == 0){
digitalWrite(LEDpin, HIGH);
delay((int)heartBeatArray[hbeatIndex]) ;
digitalWrite(LEDpin, LOW);
}
hbeatIndex++;
// Serial.println(hbeatIndex);
prevMillis = millis();

}
}

Arduino Servo Program 1

ArduinoServoPotentiometer1.pde




// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott a href="http://people.interaction-ivrea.it/m.rinott">http://people.interaction-ivrea.it/m.rinott>;

#include

Servo myservo; // create servo object to control a servo

int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}





http://arduino.cc/en/Tutorial/Knob

Arduino Code #3

int redPin = 12; // Red LED connected to digital pin 12
int greenPin = 13; // Green LED connected to digital pin 11

void setup() // run once, when the sketch starts
{
pinMode(redPin, OUTPUT); // sets the digital pin as output
pinMode(greenPin, OUTPUT); // sets the digital pin as output
}

void loop() // run over and over again
{
digitalWrite(redPin, HIGH); // sets the Red LED on
digitalWrite(greenPin, HIGH); // sets the Green LED on
delay(500); // waits for half a second
digitalWrite(redPin, HIGH); // sets the Red LED on
digitalWrite(greenPin, LOW); // sets the Green LED off
delay(500); // waits for half a second
digitalWrite(redPin, LOW); // sets the Red LED off
digitalWrite(greenPin, LOW); // sets the Green LED off
delay(500); // waits for half a second
digitalWrite(redPin, LOW); // sets the Red LED off
digitalWrite(greenPin, HIGH); // sets the Green LED on
delay(500); // waits for half a second
}

This is a more refined version of the LED blinking program that we made. It blinks 2 LEDs in an alternating on/off sequence.