2010年8月9日月曜日

program for the second Arduino(Isadora controller)

This program will light led when the second Arduino got a pulse from the first Arduino, and 

send data of sensors to Isadora

/*

Program for second Arduino
Ver 1.0 unchecked

Serial in Watcher "1" got data from daisy chain
Serial in Watcher "2" got data from ultrasonic range finder
Serial in Watcher "3" got data from motion sensor
*/

//regular variable
int alarmValue = 1;
int val=0;

//analog pins
int alarmPin = 1;
int daisyChainPin = 0;
//digital pins
int ledPin = 13;
const int pingPin = 7;


void setup () {
Serial.begin (9600);
pinMode(ledPin, OUTPUT);
pinMode(alarmPin, INPUT);
delay (2000); //wait for a motion sensor to be ready to go
digitalWrite(ledPin, LOW);
}

void loop ()
{
val = analogRead(daisyChainPin);
if (val>100){

//turn on LED while the second arduino got pulse from the first arduino
digitalWrite(ledPin, HIGH);


val =1;//to activate Isadora
Serial.print(1,DEC);
Serial.print (val, DEC);
Serial.println();

/*
for ultrasonic range finder. source from
http://www.arduino.cc/en/Tutorial/Ping

created 3 Nov 2008
by David A. Mellis
modified 30 Jun 2009
by Tom Igoe
*/
long duration, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
cm = microsecondsToCentimeters(duration);
//send this value to use for something
Serial.print(2,DEC);
Serial.print (cm, DEC);
Serial.println();

//motion sensor
//based on http://www.eathertons.com/blog/?p=73
//Shotaro fixed the code little bit

alarmValue = analogRead(alarmPin);
if (alarmValue < 100)
{
alarmValue =1;
sendPulse();
}
delay(100);
digitalWrite(ledPin, LOW);
}
}
//END of void loop




//culculation from microsecond to length
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}




//send a data from motion sensor to Isadora
//This value will control "counter" of Isadora
void sendPulse() {
Serial.print(3,DEC);
Serial.print (alarmValue, DEC);
Serial.println();
}
}

0 件のコメント:

コメントを投稿