Thursday, May 6, 2010

Task 46, 47,49

By separating the code in the previous post into the send/recieve roles you can send pulses between two ardunio boards.

Sender Code:
void setup(){
pinMode(13,OUTPUT);
}

void loop(){
digitalWrite(13,HIGH);
delay(500);
digitalWrite(13,LOW);
delay(500);
}

This code will pulse the IR transmitter between on and off with a ~500ms pause at each state;

Receiver Code:
int reading;

void setup(){
pinMode(13,INPUT);
Serial.begin(9600);
}

void loop(){
reading = digitalRead(13);
if (reading == HIGH){
Serial.println("High");
}
else
{
Serial.println("Low");
}
}

The receiver will either print High or Low depending on what the other board sent.

Task 49:

Sender Code:
int count = 0;

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

void loop(){
for (int i = 0; i < 1001; i ++){
digitalWrite(13,HIGH);
delay(1);
digitalWrite(13,LOW);
delay(1);
}
}

Receiver Code:
int count = 0;

void setup(){
pinMode(13,INPUT);
Serial.begin(9600);
}

void loop(){
for (int i = 0; i < 1001; i ++){
reading = digitalRead(13);
if (reading == HIGH){
Serial.println("High");
count = count + 1;
}
else
{
Serial.println("Low");
}
}
Serial.print(count);
Serial.println(" of 1000 recieved");
}

No comments:

Post a Comment