int trig2 = 11; //Trig and echo pin setup for the Ultrasonic sensors
int echo2 = 10;
int trig1 = 13;
int echo1 = 12;
int trig3 = 7;
int echo3 = 6;
int trig4 = 4;
int echo4 = 3;
int button1 = 8; //Button variable setup
int button2 = 9;
int button3 = 2;
int button0 = 5;
int x; //x integer for the x coordinate
int y; //y integer for the y coordinate
int px = x; //sets previous x as x
int py = y; // sets previous y as y
int inByte = 0; //For arduino to processing serial com
int s1 = 4; //sesnor region 1
int s12 = 6; //sesnor region 1 and 2
int s2 = 8; //sesnor region 2
int s23 = 10; //sesnor region 2 and 3
int s3 = 12; //sesnor region 3
int s34 = 14; //sesnor region 3 and 4
int s4 = 16; //sesnor region 4
int potpin = 2; //potentiometer pin
int pot; //potentiometer value
int colorval; //color value (from potentiometer)
void setup() {
Serial.begin (9600);
pinMode(trig2, OUTPUT);
pinMode(echo2, INPUT);
pinMode(trig4, OUTPUT);
pinMode(echo4, INPUT);
pinMode(trig3, OUTPUT);
pinMode(echo3, INPUT);
pinMode(trig1, OUTPUT);
pinMode(echo1, INPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button0, INPUT);
establishContact(); //establish contact with processing
}
void loop() {
int button1val = digitalRead(button1); //setting up buttons
int button2val = digitalRead(button2);
int button3val = digitalRead(button3);
int button0val = digitalRead(button0);
pot= analogRead(potpin); //reads potentiometer value
colorval = pot; //translates pot val to color
if (Serial.available() > 0)
{
inByte = Serial.read();
long duration2, distance2; //duration and distance for the sensors
long duration4, distance4;
long duration3, distance3;
long duration1, distance1;
digitalWrite(trig2, LOW); //trigger the ultrasonic pins
delay(.002);
digitalWrite(trig2, HIGH);
delay(.01);
digitalWrite(trig2, LOW);
duration2 = pulseIn(echo2, HIGH); //record the time it takes for the sound to return
distance2 = (duration2 / 2) / 29.1; //convert the time to distance
digitalWrite(trig4, LOW);
delay(.002);
digitalWrite(trig4, HIGH);
delay(.01);
digitalWrite(trig4, LOW);
duration4 = pulseIn(echo4, HIGH);
distance4 = (duration4 / 2) / 29.1;
digitalWrite(trig3, LOW);
delay(.002);
digitalWrite(trig3, HIGH);
delay(.01);
digitalWrite(trig3, LOW);
duration3 = pulseIn(echo3, HIGH);
distance3 = (duration3 / 2) / 29.1;
digitalWrite(trig1, LOW);
delay(.002);
digitalWrite(trig1, HIGH);
delay(.01);
digitalWrite(trig1, LOW);
duration1 = pulseIn(echo1, HIGH);
distance1 = (duration1 / 2) / 29.1;
if (distance1 <30 && distance2 >30) //If sensor detects in a certain area
{
x = distance1; //set x and y to the recorded distances
y = s1;
}
else if (distance1 <30 && distance2 <30)
{
x = ((distance1+distance2)/2);
y = s12;
}
else if (distance2 <30 && distance3 >30) //different statements for different sensors
{
x = distance2;
y = s2;
}
else if (distance2 <30 && distance3 <30)
{
x = ((distance2+distance3)/2);
y = s23;
}
else if (distance2 >30 && distance3 <30)
{
x = distance3;
y = s3;
}
else if (distance3 <30 && distance4 <30)
{
x = ((distance3+distance4)/2);
y = s34;
}
else if (distance3 >30 && distance4 <30)
{
x = distance4;
y = s4;
}
Serial.write(x); //Serial write everything (sends it to processing via serial)
Serial.write(y);
Serial.write(px);
Serial.write(py);
Serial.write(button1val);
Serial.write(button2val);
Serial.write(button3val);
Serial.write(button0val);
Serial.write(colorval);
px = x; //Set px and py to x and y (used if lines are reimplemented)
py= y;
// }
delay(50);
}
}
void establishContact() { //prints A until contact is established.
while (Serial.available() <= 0) {
Serial.print('A'); // send a capital A
delay(300);
}
}
No comments:
Post a Comment