Ultrasonic program giving me an odd error - arduino

I created a program that is intended to turn a servo motor 180 degrees then back if an object is within 20cm of the ultrasonic sensor. This works fine except sometimes when the if condition is not met. In this case, the motor doesn't move at all and the distance is put into the serial monitor. This works fine sometimes, but in most cases the serial monitor just prints 0 repeatedly and I have to restart the program for it to work again. Even when I put an object within 20cm of the sensor it still prints 0. I am quite new to Arduino so this really just stumped me haha. Any tips or help would be greatly appreciated.
#include <Servo.h>
Servo servo1; // create servo object to control a servo
#define trigPin 13
#define echoPin 12
void setup()
{
Serial.begin(9600);
servo1.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
long duration = 0, distance = 0;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
if(distance > 2 && distance < 20){
servo1.write(180);
Serial.println(distance);
delay(1000);
servo1.write(-180);
}
else{
servo1.write(0);
Serial.println(distance);
}
delay(2000);
}

See what happens when you try this code, as I cleaned it up a little bit:
#include <Servo.h>
#define trigPin 13
#define echoPin 12
Servo servo1; // create servo object to control a servo
void setup() {
Serial.begin (9600);
servo1.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
float duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) * 29.1;
if (duration) {
if (distance > 2 && distance < 20) {
servo1.write(180);
Serial.println(distance);
delay(1000);
servo1.write(-180);
}
else {
servo1.write(0);
Serial.println(distance);
}
}
delay(2000);
}
I think if you use a float it might work. However, if it doesn't work, try switching up the pin assignments to different pins on the Arduino and see if that works. If the distance measurment is off the best way to adjust it is with the multiplied value in the distance variable in the void loop ().This one works for me.

Related

I am facing some issues while using arduino working with memory card and speaker, please help me

This is the code I wrote:
I set the pins given accordingly I checked my ultrasonic sensors, transistor, and resistors are in working condition. CS (in code denoted as chipset) is pinned in pin 10, resistor to 9, and that resistor is connected to the transistor, and then it is connected to the speaker.
#define trigPin1 3
#define echoPin1 2
#define trigPin2 4
#define echoPin2 5
#define trigPin3 7
#define echoPin3 8
#define cs 10
#include "SD.h"
#include "TMRpcm.h"
#include "SPI.h"
TMRpcm tmrpcm;
long duration, distance, RightSensor, BackSensor, FrontSensor, LeftSensor;
void setup()
{
tmrpcm.speakerPin = 9;
Serial.begin(9600);
if (!SD.begin(cs))
{
Serial.println("SD fail");
}
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
}
void loop() {
SonarSensor(trigPin1, echoPin1);
RightSensor = distance;
tmrpcm.setVolume(6);
if (RightSensor > 10)
{
tmrpcm.play("RightFormatted.wav");
}
SonarSensor(trigPin2, echoPin2);
LeftSensor = distance;
if (LeftSensor > 10)
{
tmrpcm.play("LeftFormatted.wav");
}
SonarSensor(trigPin3, echoPin3);
FrontSensor = distance;
if (FrontSensor > 10)
{
tmrpcm.play("FrontFormatted.wav");
}
Serial.println(LeftSensor);
Serial.println(" - ");
Serial.print(FrontSensor);
Serial.print(" - ");
Serial.println(RightSensor);
}
void SonarSensor(int trigPin, int echoPin)
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
}
The error I am getting is:
Error of this code
Instead of typing the name of the .wav file directly inside the .play method, create a const char variable to assign the name(s):
const char* audio1= "RightFormatted.wav";
then you can use it as:#
tmrpcm.play(audio1);
However, based on the examples from the library, you do not need the file extension, but I might be wrong in this regard.
Try first my proposal and please report your results

How to combine two Adruino Sketches into one

I am doing a project using sensors (LDR & Ultrasonic) and LEDs using the Arduino Software. I have managed to get the light bulb to work, however I would like to know on how to combine two different Arduino programs into one. Attached below are the two different programs
Program 1:
int ldr=A0;//Set A0(Analog Input) for LDR.
int value=0;
void setup() {
Serial.begin(9600);
pinMode(3,OUTPUT);
}
void loop() {
value=analogRead(ldr);//Reads the Value of LDR(light).
Serial.println("LDR value is :");//Prints the value of LDR to Serial Monitor.
Serial.println(value);
if(value<300)
{
digitalWrite(3,HIGH);//Makes the LED glow in Dark.
}
else
{
digitalWrite(3,LOW);//Turns the LED OFF in Light.
}
}
Program 2:
#define trigPin 13
#define echoPin 12
#define led 11
void setup()
{ Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
}
void loop()
{ long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance < 10)
{ digitalWrite(led,HIGH);
}
else {
digitalWrite(led,LOW);
}
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
Thank you!
You can use a timer and each 500 milliseconds run loop2
The first loop run normaly but the second loop run each 500 milliseconds
#define trigPin 13
#define echoPin 12
#define led 11
#define DELAY 500
int ldr = A0; //Set A0(Analog Input) for LDR.
int value = 0;
long timer = millis(); // a timer for timing 500 milliseconds
void setup() {
Serial.begin(9600);
pinMode(3, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
}
void loop() {
value = analogRead(ldr); //Reads the Value of LDR(light).
Serial.println("LDR value is :");//Prints the value of LDR to Serial Monitor.
Serial.println(value);
if (value < 300)
{
digitalWrite(3, HIGH); //Makes the LED glow in Dark.
}
else
{
digitalWrite(3, LOW); //Turns the LED OFF in Light.
}
if (millis() - timer >= DELAY ) {
timer = millis();//reset timer
loop2();
}
}
void loop2() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
if (distance < 10)
{ digitalWrite(led, HIGH);
}
else {
digitalWrite(led, LOW);
}
Serial.print(distance);
Serial.println(" cm");
}

How do i make an ultrasonic sensor trigger an LED to stay on until reset?

So, I know it's probably SUPER simple, but I'm new to arduino and I'm just drawing a blank. I'm making a motion detector with an HC-SRO4 ultrasonic sensor. Right now I have it set so whenever it senses an object within 60 cm, it turns on an LED light, but when the object disappears, the LED turns off. What I would like to happen is that it would stay on until I press a button to reset it. Any help is really appreciated and i thank you in advance.
void setup() {
#define LED 8
#define trigPin 12
#define echoPin 13
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LED, OUTPUT);
}
void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1
;if (distance >= 60 || distance <= 0){
Serial.println("no object detected");
digitalWrite(LED, LOW);}
else {
Serial.println("object detected");
digitalWrite(LED, HIGH);
}}
for this you have make low pushbutton pin while pressing.once the oobject came near the flag will set once the set it will move to while loop and run until the button pressed
#define LED 8
#define trigPin 12
#define echoPin 13
#define push_button 5
int flag=0;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LED, OUTPUT);
pinMode(push_button,INPUT);
}
void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if(digitalRead()==0)
{
flag=0;
}
if (distance >= 60 || distance <= 0){
Serial.println("no object detected");
digitalWrite(LED, LOW);}
else {
Serial.println("object detected");
flag=1;
while(flag=1)
{
digitalWrite(LED, HIGH);}
}}

How can I do that my Rover goes to the Left after doing a photo

at the moment I'm trying that my Rovers drives until he found an Object. Then he should go to the right, stops and making a photo. After this first tour, the Rover should drive around the object and should always make a photo.
The problem is that he goes first to the right but drives always forward without driving around.
I already ask in this group for help that he only moves and drives around but I would try for myself that he drives around but without success.
Here you can see my Arduino Code that I am using.
// Define SensorS pins
#define trigPin 15
#define echoPin 2
//Define SensorXL pins
#define trigPinXL 14
#define echoPinXL 13
//Define Raspberry Pin
#define RaspiPin 26
//Define Motor pins
#define motorIn3 16 //Input 3
#define motorIn1 17 //Input 1
#define motorIn4 18 //Input 4
#define motorIn2 19 //Input 2
// Defines variables
long duration;
int distance;
// Define ActivateDistance
const int activateDistance = 40;
const int activateDistance2 =40;
void setup()
{
// Sets the trigPin as an Output
pinMode(trigPin, OUTPUT);
pinMode(trigPinXL, OUTPUT);
// Sets the echoPin as an Input
pinMode(echoPin, INPUT);
pinMode(echoPinXL, INPUT);
// sets the Motorpins as outputs:
pinMode(motorIn1, OUTPUT);
pinMode(motorIn2, OUTPUT);
pinMode(motorIn3, OUTPUT);
pinMode(motorIn4, OUTPUT);
//Sets Raspberry Pin as output
pinMode(RaspiPin, OUTPUT);
// Starts the serial communication
Serial.begin(9600);
}
void stop()
{
// stop motor without duration
Serial.println("STOP");
digitalWrite(motorIn1, LOW);
digitalWrite(motorIn2, LOW);
digitalWrite(motorIn3, LOW);
digitalWrite(motorIn4, LOW);
}
void stopp(int duration)
{
// stop motor without duration
Serial.println("STOP");
digitalWrite(motorIn1, LOW);
digitalWrite(motorIn2, LOW);
digitalWrite(motorIn3, LOW);
digitalWrite(motorIn4, LOW);
delay(duration);
stop();
}
void left(int duration)
{
//Motor goes to left
Serial.println("LEFT");
digitalWrite(motorIn1, LOW);
digitalWrite(motorIn2, HIGH);
digitalWrite(motorIn3, HIGH);
digitalWrite(motorIn4, LOW);
delay(duration);
stop();
}
void right(int duration)
{
//Motor goes to left
Serial.println("RIGHT");
digitalWrite(motorIn1, HIGH);
digitalWrite(motorIn2, LOW);
digitalWrite(motorIn3, LOW);
digitalWrite(motorIn4, HIGH);
delay(duration);
stop();
}
void forward(int duration)
{
//Motor goes forward
Serial.println("FORWARD");
digitalWrite(motorIn2, HIGH);
digitalWrite(motorIn4, HIGH);
digitalWrite(motorIn3, LOW);
digitalWrite(motorIn1, LOW);
delay(duration);
stop();
}
long get_distance(void)
{
//get distance from sensor
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
return distance;
}
long get_distanceXL(void)
{
//get distance from sensor
// Clears the trigPin
digitalWrite(trigPinXL, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPinXL, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinXL, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPinXL, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
return distance;
}
int turn = 0;
void loop()
{
//Define Raspi Pin as Low
digitalWrite(RaspiPin, LOW);
// check sensor
if (get_distance() <= activateDistance)
{
// go right for 1 second
right(1000);
//stop for 2 seconds
stopp(2000);
//make a Photo
digitalWrite(RaspiPin, HIGH);
delay(100);
digitalWrite(RaspiPin, LOW);
while(turn<4)
{
if(get_distanceXL()>activateDistance2)
{
//go left for 1 second
left(1000);
forward(1000);
stopp(2000);
digitalWrite(RaspiPin, HIGH);
delay(100);
digitalWrite(RaspiPin, LOW);
forward(500);
turn = turn + 1;
}
else
//go forward for 1 second
forward(500);
stopp(2000);
digitalWrite(RaspiPin, HIGH);
delay(100);
digitalWrite(RaspiPin, LOW);
forward(500);
}
}
else
// go forward for 1 second
forward(1000);
}

Arduino - Ultrasonic sensor (SR04T) measure only 0

I have a ultrasonic sensor (SR04T) that I have connected to my Arduino. I'm using the TX and RX port at the Arduino UNO. The problem is that it only reads value 0 cm. Could anyone help me find the error?
The code I'm using looks like this:
const int trigPin = 1;
const int echoPin = 0;
void setup() {
Serial.begin(9600);
}
void loop()
{
long duration, inches, cm;
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// convert time into cm
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
You are doing two things incorrectly here.
You are setting pinMode in the Loop. No need for that. Put them in the Setup. You don't need to set pinmode continuously.
You are using pin 0 and 1 for your input and output while using Serial. Serial uses pin 0 and 1 for serial communication. Use other pins for your input and output. Have a look at http://marcusjenkins.com/wp-content/uploads/2014/06/ARDUINO_V2.png

Resources