Ok, I understand java and C++, but I can't replicate this very strange error. So here, I ran this code onto my Arduino One. And, the ouput is a random spew of text and code, and I am CONFUSED!!!! May you please tell me what I did wrong?
#define echoPin 3
#define trigPin 4
#define armedPin 0 //RX
#define intrudedPin 1 //TX
int setDistance, distance;
boolean intruded = false;
int updateDistance(){
int time = 0;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
time = pulseIn(echoPin, HIGH);
distance = (time/2) / 29.1;
Serial.println(distance + "cm");
return distance;
}
void setup(){
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(armedPin, OUTPUT);
pinMode(intrudedPin, OUTPUT);
Serial.begin(9600);
Serial.println("You have 30 seconds till the security system engages!");
for (int x = 3000; x > 0; x--){//Countdown till armed
digitalWrite(armedPin, HIGH);
digitalWrite(intrudedPin, HIGH);
delay(10);
digitalWrite(armedPin, LOW);
digitalWrite(intrudedPin, LOW);
Serial.println(x*100 + " seconds left!");
}
setDistance = updateDistance();
}
void loop(){
if (!intruded){//Nothing has happened
updateDistance();
if (setDistance != distance){
Serial.println("oh my, someone has tripped the wire!");
Serial.println("You have 5 seconds till I claim I have been intruded into!");
for (int x = 5000; x > 0; x--){
digitalWrite(armedPin, HIGH);
digitalWrite(intrudedPin, HIGH);
delay(100);
digitalWrite(armedPin, LOW);
digitalWrite(intrudedPin, LOW);
Serial.println(x*100 + " seconds left!");
}
updateDistance();
if (setDistance != distance){
intruded = true;
}else{
digitalWrite(armedPin, HIGH);
}
}else if (intruded){//Someone has intruded
Serial.println("OH NO! SOMEONE HAS INTRUDED!\n INTRUDER ALERT\n INTRUDER ALERT\n INTRUDER ALERT");
digitalWrite(intrudedPin, HIGH);
digitalWrite(armedPin, LOW);
while (true){
delay(500000000);
}
}
}
}
Why? It was perfectly valid, and explains a lot about the issues OP is having. Pins 0 and 1 can be used for IO OR Serial. They can not be used for both in the same sketch.