I have some difficulty with my Arudino project.
I have a lcd display and i am trying to display a specific value that updates every time it goes through the loop. But it keeps repeating the value, here is an example: 71 71 71 71 71, but what i want is only 71. Here is the code for the loop:
for (pos = 0; pos <= 175; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
servol.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
Serial.println(pos);
// Send out a sound wave and calculate how far away the nearest object is to it.
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
//Serial.print("Distance:");
//Serial.println(distance);
lcd.setCursor(0, 0);
lcd.print(distance);
//lcd.setCursor(0, 1);
//lcd.print(distance);
while (distance < 10)
{
// If the distance is less than 98 cm, then sound the alarm and make the led flash
digitalWrite(buzzer, HIGH);
digitalWrite(ledPin, HIGH);
delay(delayTime1);//wait for 1ms
digitalWrite(buzzer, LOW);
digitalWrite(ledPin, LOW);
delay(delayTime1);//wait for 1ms
digitalWrite(buzzer, HIGH);
digitalWrite(ledPin, HIGH);
delay(delayTime2);//wait for 2ms
digitalWrite(buzzer, LOW);
digitalWrite(ledPin, LOW);
delay(delayTime2);//wait for 2ms
// If the B button is pushed, shut the alarm off and disarm it
if (digitalRead(buttonBpin) == LOW)
{
break;
}
}
delay(100);
// If b button is pressed, disarm the alarm
if (digitalRead(buttonBpin) == LOW)
{
break;
}
} //FOR1
Here is the entire code:
#include <Servo.h>
#include <LiquidCrystal.h>
Servo servol;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int pos = 0;
const int trigPin = 8;//Pin to send out sound wave
const int echoPin = 9; //Pin to listen for sound wave coming back
int ledPin = A3;//Pin to turn Led on
int buttonApin = 10;//Pin to arm the alarm
int buttonBpin = A5;//Pin to disarm the alarm
int value_A;//Value of pin A
int value_B;//Value of pin B
int buzzer = A4;//the pin of the active buzzer
int delayTime1 = 250;
int delayTime2 = 250;
long duration; // Travel time obtained from sensor
int distance; // calculated distance of how far away the object is
void setup() {
servol.attach(6);
lcd.begin(16, 2);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(buttonApin, INPUT_PULLUP);
pinMode(buttonBpin, INPUT_PULLUP);
pinMode(buzzer, OUTPUT);
pinMode(12, INPUT);
Serial.begin(9600);
}
void loop() {
// Check to see the state of button A and B
value_A = digitalRead(buttonApin);
value_B = digitalRead(buttonBpin);
delay(500);
if (value_A == LOW)
{
//If the alarm is armed, turn the LED on
digitalWrite(ledPin, HIGH);
while (ledPin, HIGH)
{
for (pos = 0; pos <= 175; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
servol.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
Serial.println(pos);
// Send out a sound wave and calculate how far away the nearest object is to it.
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
//Serial.print("Distance:");
//Serial.println(distance);
lcd.setCursor(0, 0);
lcd.print(distance);
//lcd.setCursor(0, 1);
//lcd.print(distance);
while (distance < 10)
{
// If the distance is less than 98 cm, then sound the alarm and make the led flash
digitalWrite(buzzer, HIGH);
digitalWrite(ledPin, HIGH);
delay(delayTime1);//wait for 1ms
digitalWrite(buzzer, LOW);
digitalWrite(ledPin, LOW);
delay(delayTime1);//wait for 1ms
digitalWrite(buzzer, HIGH);
digitalWrite(ledPin, HIGH);
delay(delayTime2);//wait for 2ms
digitalWrite(buzzer, LOW);
digitalWrite(ledPin, LOW);
delay(delayTime2);//wait for 2ms
// If the B button is pushed, shut the alarm off and disarm it
if (digitalRead(buttonBpin) == LOW)
{
break;
}
}
delay(100);
// If b button is pressed, disarm the alarm
if (digitalRead(buttonBpin) == LOW)
{
break;
}
} //FOR1
for (pos = 175; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
servol.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
Serial.println(pos);
// Send out a sound wave and calculate how far away the nearest object is to it.
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
Serial.print("Distance:");
Serial.println(distance);
while (distance < 10)
{
// If the distance is less than 98 cm, then sound the alarm and make the led flash
digitalWrite(buzzer, HIGH);
digitalWrite(ledPin, HIGH);
delay(delayTime1);//wait for 1ms
digitalWrite(buzzer, LOW);
digitalWrite(ledPin, LOW);
delay(delayTime1);//wait for 1ms
digitalWrite(buzzer, HIGH);
digitalWrite(ledPin, HIGH);
delay(delayTime2);//wait for 2ms
digitalWrite(buzzer, LOW);
digitalWrite(ledPin, LOW);
delay(delayTime2);//wait for 2ms
// If the B button is pushed, shut the alarm off and disarm it
if (digitalRead(buttonBpin) == LOW)
{
break;
}
}
delay(100);
// If b button is pressed, disarm the alarm
if (digitalRead(buttonBpin) == LOW)
{
break;
}
} //FOR2
} //WHILE
}
// If the alarm is disarmed, turn the LED off
if (value_B == LOW)
{
digitalWrite(ledPin, LOW);
}
}