HI,
Can some one help me with my script. I am using 4 ultra sonic and 4 relays. And i also add the count function of Relay on. But this is not working. Can some one help me. Cont of relay on is not shown .
int RelayState = 0; // counter for the number of button presses
int CurrentRelayState = 0; // current state of the button
int LastRelayState = 0; // previous state of the button
int Relay1 = 2;
int Relay2 = 3;
int Relay3 = 4;
int Relay4 = 5;
int trigPin1 = 6;
int echoPin1 = 7;
int trigPin2 = 8;
int echoPin2 = 9;
int trigPin3 = 10;
int echoPin3 = 11;
int trigPin4 = 12;
int echoPin4 = 13;
void setup() {
Serial.begin (9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
pinMode(trigPin4, OUTPUT);
pinMode(echoPin4, INPUT);
pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
pinMode(Relay3, OUTPUT);
pinMode(Relay4, OUTPUT);
}
void firstsensor(){ // This function is for first sensor.
int duration1, distance1;
digitalWrite (trigPin1, HIGH);
delayMicroseconds (100);
digitalWrite (trigPin1, LOW);
duration1 = pulseIn (echoPin1, HIGH);
distance1 = (duration1/2) / 29.1;
Serial.print("1st Sensor: ");
Serial.print(distance1);
Serial.print("cm ");
if (distance1 < 30) { // Change the number for long or short distances.
digitalWrite (Relay1, HIGH);
} else {
digitalWrite (Relay1, LOW);
// read the pushbutton input pin:
CurrentRelayState = digitalRead(Relay1);
// compare the CurrentRelayState to its previous state
if (CurrentRelayState != LastRelayState) {
// if the state has changed, increment the counter
if (CurrentRelayState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
RelayState++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(RelayState);
} else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state,
//for next time through the loop
LastRelayState = CurrentRelayState;
}
}
void secondsensor(){ // This function is for second sensor.
int duration2, distance2;
digitalWrite (trigPin2, HIGH);
delayMicroseconds (100);
digitalWrite (trigPin2, LOW);
duration2 = pulseIn (echoPin2, HIGH);
distance2 = (duration2/2) / 29.1;
Serial.print("2nd Sensor: ");
Serial.print(distance2);
Serial.print("cm ");
if (distance2 < 20) { // Change the number for long or short distances.
digitalWrite (Relay2, HIGH);
}
else {
digitalWrite (Relay2, LOW);
// read the pushbutton input pin:
CurrentRelayState = digitalRead(Relay2);
// compare the CurrentRelayState to its previous state
if (CurrentRelayState != LastRelayState) {
// if the state has changed, increment the counter
if (CurrentRelayState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
RelayState++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(RelayState);
} else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state,
//for next time through the loop
LastRelayState = CurrentRelayState;
}
}
void thirdsensor(){ // This function is for third sensor.
int duration3, distance3;
digitalWrite (trigPin3, HIGH);
delayMicroseconds (100);
digitalWrite (trigPin3, LOW);
duration3 = pulseIn (echoPin3, HIGH);
distance3 = (duration3/2) / 29.1;
Serial.print("3rd Sensor: ");
Serial.print(distance3);
Serial.print("cm");
if (distance3 < 10) { // Change the number for long or short distances.
digitalWrite (Relay3, HIGH);
}
else {
digitalWrite (Relay3, LOW);
// read the pushbutton input pin:
CurrentRelayState = digitalRead(Relay3);
// compare the CurrentRelayState to its previous state
if (CurrentRelayState != LastRelayState) {
// if the state has changed, increment the counter
if (CurrentRelayState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
RelayState++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(RelayState);
} else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state,
//for next time through the loop
LastRelayState = CurrentRelayState;
}
}
void forthsensor(){ // This function is for forth sensor.
int duration4, distance4;
digitalWrite (trigPin4, HIGH);
delayMicroseconds (100);
digitalWrite (trigPin4, LOW);
duration4 = pulseIn (echoPin4, HIGH);
distance4 = (duration4/2) / 29.1;
Serial.print(" 4th Sensor: ");
Serial.print(distance4);
Serial.print("cm ");
if (distance4 < 40) { // Change the number for long or short distances.
digitalWrite (Relay4, HIGH);
} else {
digitalWrite (Relay4, LOW);
// read the pushbutton input pin:
CurrentRelayState = digitalRead(Relay4);
// compare the CurrentRelayState to its previous state
if (CurrentRelayState != LastRelayState) {
// if the state has changed, increment the counter
if (CurrentRelayState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
RelayState++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(RelayState);
} else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state,
//for next time through the loop
LastRelayState = CurrentRelayState;
}
}
void loop() {
Serial.println("\n");
firstsensor();
secondsensor();
thirdsensor();
forthsensor();
delay(10);
}