the button code

Just for the record: This code will drive our arduino boards (two of them and yes! they are connected AND talking!).

It took Larissa and me quite some time and a lot more nerves. And Tomek, too! He spent hours with us on ICQ altruisticly, even got his two Arduinos out of the box and gave us the right hints finally – by remote!
So we learnt a lot. Most important: With two communicating Boards, never forget to connect the grounds. Always remember.

The code for the geeks:

//##################
//variables that might be changed for interface adjustments

//1 - distance sensor
int distancePin = 3; //analog

//rgbLED:
//red = pin 8; green = 9; blue = pin 10;
//choose color for idle status ("nothing in sight...")
int dist_idleLED = 10; //digital
//choose color for active status ("A FRIEND!")
int dist_activeLED = 8; //digital

//if the distance sensor switches on, start a counter
//every interruption in the sensor's sight will start the counter again
//so the switchback to "idle" will only occur
//if there is "nothing in sight" for a given time (switchbackdelay)
//to prevent the light from flickering when the sensor loses "contact"
int switchbackdelay = 6000;

int smoothfactor = 10; //loop x times and than take an average value
int smoothdelay = 50; //ms to wait between measurements (of distance_raw)

//sensor characteristics for Sharp GP2D12
//distance = 100: nothing in sight, can be as low as 40 (distance > 70cm)
//distance = 600; //largest value the sensor put out (around 10cm)
int distance_switch = 130; // most probably sth is in the way (around 60cm)
//(this threshold represents sensitivity as well)

//2 - arduino communication
int arduinoOutput = 2; //pin to "send" data
int arduinoInput = 3; // pin to "receive" data
int schalterInput = 4; // pin to read, if switch is pressed
int led_both = 5; // pin for led that lights up if both arduinos have a button pressed

//##################
//variables that are needed for internal calculations

//1 - distance sensor
//a time variable for counters
long time;
long time_switchon; //starttime of the counter

int smoothloops = 0; //number of loops that were already run through for smoothing
int smoothcollector = 0; //adds all distance_raw values (will be devided by smoothfactor)

int distance_raw = 0; //the value measure directly
int distance = 0; //calibrated distance
int distance_avg = 0; //the distance smoothened

//2 - arduino communication
//variable to keep the code blocks separate
int both_pressed = 0; // =1 if the button at each arduino is pressed

void setup() {
//1 - distance sensor
pinMode (dist_idleLED, OUTPUT);
pinMode (dist_activeLED, OUTPUT);

//2 - arduino communication
pinMode(led_both, OUTPUT);
pinMode(arduinoOutput, OUTPUT);
pinMode(arduinoInput, INPUT);
pinMode(schalterInput, INPUT);

beginSerial(9600); //start serial communciation
}

void loop(){
//first, check if button is pressed
//if not, use distance sensor
//if it is pressed, continue to use distance sensor
//untill second button (of the other arduino) is pressed as well

//########## 1 - read and process distance-sensor data########

//run distance-sensing only, if buttons are not pressed
if (both_pressed == 0) {
// get raw data
distance_raw = analogRead(distancePin);
/* output to check single values
Serial.print (distance_raw);
Serial.print (" ");
*/
//collect data for smoothing
smoothcollector += distance_raw;
smoothloops++;

if (smoothloops >= smoothfactor) {
//compute average value
distance = smoothcollector / smoothfactor;
/* debugging
Serial.print (" //smooth ");
Serial.print (distance);
Serial.print (" // ");
*/
//reset smooth-variables for next run
smoothcollector = 0;
smoothloops = 0;

//check if sth is in the way:
//start counter
time = millis();

//"distance" as variable contains sensor-value
//which is higher when the object is closer, so:
if (distance > distance_switch) {
Serial.print ("A FRIEND!");
//store time of switching for counter
time_switchon = time;
//Serial.println (time_switchon); //debugging

digitalWrite (dist_idleLED, LOW);
digitalWrite (dist_activeLED, HIGH);
}
else {
//only switch back after waiting for some time
if (time > time_switchon + switchbackdelay) {
Serial.print ("nobody in sight...");

digitalWrite (dist_activeLED, LOW);
digitalWrite (dist_idleLED, HIGH);
}
}
Serial.print (10, BYTE); //this is a LF linefeed character in ASCII
} //end of if-smoothloop...
delay(smoothdelay);
} // end of if-both_pressed
else {
//switch the lights of the distance sensor off if both
digitalWrite(dist_idleLED, LOW);
digitalWrite(dist_activeLED, LOW);
}

//####### 2 - build a two-switch-AND-connection####
//if the switch is pressed, the Board will set an Output-Pin to HIGH
//that Pin is connected to an Input-Pin of another board where it is read as Input
//if the switch of that board is pressed, too (>both buttons pressed)
//an LED will light up
//in place of Serial communication a simpler way via two digitalPins is used

/* debugging
if (digitalRead(arduinoInput) == HIGH) {
Serial.print("das andere Arduino sagt: schalter ist an - ");
}
else {
Serial.print("das andere Arduino sagt: nischt - ");
}
*/
//the switch will connect the Pin to gnd, so it will be low when the switch gets pressed
if (digitalRead(schalterInput) == LOW)
{
Serial.print("schalter ist an - ");
digitalWrite(arduinoOutput, HIGH);

if (digitalRead(arduinoInput) == HIGH) {
//Serial.print("das andere Arduino sagt: schalter ist an - ");
digitalWrite(led_both, HIGH);
both_pressed = 1;
}
else {
digitalWrite(led_both, LOW);
both_pressed = 0;
}
}
else{
//Serial.print("nischt is an - ");
digitalWrite(led_both, LOW);
both_pressed = 0;
digitalWrite(arduinoOutput, LOW);
}

//Serial.print(10, BYTE); //a linefeed-character in ASCII
//Serial.print(13, BYTE);
}

no Responses so far to the button code