2010年6月6日日曜日

Today's exercise(homework)2


http://www.lulu.com/items/volume_63/1108000/1108699/4/print/ARDUINO_NOTEBOOKv6.pdf

The below program is based on the things that I learned in the PDF document page 16 to 26.

Learned: complex condition cords with "if" and "while".

This original program tells the value from analog input, which is connected to the Potentiometer, with Green LED and Red LED and sound.

If the analog input got more electrical flow, Arduino lights Green LED. If the flow is middle, Arduino lights Red LED. If the flow is low or none, Arduino tells us through the speaker.

I use commends that learned through the PDF exercise, in order to check my understanding.

Requirements

Arduino basic kit(two LEDs, possibly red and green; speaker; potentiometer and wires)

Resistor (more than 2. The number depend on how much Ω  is it)


 

Program

/*

This is a middle part of the exercise of PDF p16-26
http://www.lulu.com/items/volume_63/1108000/1108699/4/print/ARDUINO_NOTEBOOKv6.pdf
This exercise contains mainly
if else
while
random(min,max)
This program is made to check my understanding of the contents of the PDF.
This program also requires my original circuit setup. I will post it on my blog.
*/

int digiPin=13;
int digiPin2=2;
int digiPin3=7;
int anaPin=0;
int val;
int duration;

void setup()
{
Serial.begin(9600); //setup serial seep
pinMode(digiPin, OUTPUT); //setup digital pins
pinMode(digiPin2,OUTPUT);
pinMode(digiPin3,OUTPUT);
}
void loop()
{
val=analogRead(anaPin); //get a value from analog pin 0
if (val > 500) //if analog pin 0 got more than 500, do followings
{
digitalWrite(digiPin, HIGH); //light up LED at digital pin 13
delay(val); //dulation is decided by the value of analog value
Serial.println("more than 500"); //send back from Arduino
digitalWrite(digiPin, LOW);
delay(1000);
}
else // if analog pin 0 got less than 500, do followings
{

while(val<=300)// if analog pin 0 got less than 300, do play a sound

{

tone(digiPin3, 494, 500); // command for sound

delay(500);  

val=analogRead(anaPin);  // This step is important. If you forget it, you can't go out of while loop

}

// if value is 300

Serial.println("midium and random duration");

digitalWrite(digiPin2,HIGH); //turn on LED connected to digital pin 2.
duration = random(1000,3000); // LED lights from 1 to 3 second
delay(duration);
digitalWrite(digiPin2,LOW);
duration = random(1000,3000);
delay(duration);
}

}



val=analogRead(anaPin); is  important in the while loop. I did not put this commend at first so that the program did not work what I wanted. It was a good study.



Hardware setup






My work movie.






0 件のコメント:

コメントを投稿