Coding help
I am a noob with coding and have two different codes that I want to work together as one and hopefully modify a little bit. I am trying to have an Ultrasonic Sensor stop 2 DC motors when it detects an object within a specific distance. I have a code that is great for the two motors and a code that works for an ultrasonic sensor. The problem I'm running into is that when I add the code bit for the ultrasonic sensor the serial monitor picks it up but it somehow interrupts the motors signal and the motors do not respond at all. If anyone could help me I would be grateful. I am using and arduino uno R3 starter kit.
Code for two motors:
char data = 0; //Variable for storing received data
const int lm1=7;
const int lm2=6;
const int rm1=5;
const int rm2=4;
void setup()
{
Serial.begin(9600); //Sets the baud for serial data transmission
pinMode(lm1,OUTPUT);
pinMode(lm2,OUTPUT);
pinMode(rm1,OUTPUT);
pinMode(rm2,OUTPUT);
}
void loop()
{
if(Serial.available() > 0) // Send data only when you receive data:
{
data = Serial.read(); //Read the incoming data & store into data
Serial.print(data); //Print Value inside data in Serial monitor
Serial.print("\n");
if(data=='F'){
front();
Break;
}else if(data=='B'){
back();
Break;
}else if(data=='L'){
left();
Break;
}else if(data=='R'){
right();
Break;
}else if(data=='S'){
Break();
}
}
}
void front(){
Serial.println("Forward Move");
digitalWrite(lm2,HIGH);
digitalWrite(rm2,HIGH);
digitalWrite(lm1,LOW);
digitalWrite(rm1,LOW);
}
void back(){
Serial.println("Back Move");
digitalWrite(lm1,HIGH);
digitalWrite(rm1,HIGH);
digitalWrite(lm2,LOW);
digitalWrite(rm2,LOW);
}
void left(){
Serial.println("Left Move");
digitalWrite(rm2,HIGH);
digitalWrite(rm1,LOW);
digitalWrite(lm1,HIGH);
digitalWrite(lm2,LOW);
}
void right(){
Serial.println("Right Move");
digitalWrite(lm2,HIGH);
digitalWrite(lm1,LOW);
digitalWrite(rm1,HIGH);
digitalWrite(rm2,LOW);
}
void Break(){
Serial.println("Break");
digitalWrite(lm2,LOW);
digitalWrite(lm1,LOW);
digitalWrite(rm1,LOW);
digitalWrite(rm2,LOW);
}
Code for the US sensor:
const int trigPin=9;
const int echoPin=10;
float duration, distance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration*0.034/2
Serial.print("Distance = ");
Serial.print(distance);
Serial.println(" cm");
delay(200);
}
The codes (I attempted to) put together:
char data = 0; //Variable for storing received data
const int lm1=7;
const int lm2=6;
const int rm1=5;
const int rm2=4;
const int trigPin=9;
const int echoPin=10;
float duration, distance;
void setup()
{
Serial.begin(9600); //Sets the baud for serial data transmission
pinMode(lm1,OUTPUT); // Define inputs and outputs
pinMode(lm2,OUTPUT);
pinMode(rm1,OUTPUT);
pinMode(rm2,OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
{
digitalWrite(trigPin, LOW); // Clear the trigPin by setting it LOW:
delayMicroseconds(5);
digitalWrite(trigPin, HIGH); // Trigger the sensor by setting the trigPin high for 10 microseconds:
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // Read the echoPin. pulseIn() returns the duration (length of the pulse) in microseconds:
distance = (duration*0.034)/2; // Calculate the distance:
Serial.print("Distance: "); // Print the distance on the Serial Monitor (Ctrl+Shift+M):
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
if(Serial.available() > 0) // Send data only when you receive data:
{
data = Serial.read(); //Read the incoming data & store into data
Serial.print(data); //Print Value inside data in Serial monitor
Serial.print("\n");
if(data=='F'){
front();
Break;
}else if(data=='B'){
back();
Break;
}else if(data=='L'){
left();
Break;
}else if(data=='R'){
right();
Break;
}else if(data=='S'){
Break();
}
}
}
void front(){
Serial.println("Forward Move");
digitalWrite(lm2,HIGH);
digitalWrite(rm2,HIGH);
digitalWrite(lm1,LOW);
digitalWrite(rm1,LOW);
}
void back(){
Serial.println("Back Move");
digitalWrite(lm1,HIGH);
digitalWrite(rm1,HIGH);
digitalWrite(lm2,LOW);
digitalWrite(rm2,LOW);
}
void left(){
Serial.println("Left Move");
digitalWrite(rm2,HIGH);
digitalWrite(rm1,LOW);
digitalWrite(lm1,HIGH);
digitalWrite(lm2,LOW);
}
void right(){
Serial.println("Right Move");
digitalWrite(lm2,HIGH);
digitalWrite(lm1,LOW);
digitalWrite(rm1,HIGH);
digitalWrite(rm2,LOW);
}
void Break(){
Serial.println("Break");
digitalWrite(lm2,LOW);
digitalWrite(lm1,LOW);
digitalWrite(rm1,LOW);
digitalWrite(rm2,LOW);
Posted Apr 21, 2021, 1:06 AM UTC
Your first block of code reads distance from a sensor and saves it in 'distance'.
The second block of code looks for input from the serial monitor, and if so, does a typed command to the motors.
This then repeats endlessly.
The distance is never used. The serial monitor command seems pointless given your goal. So I don't understand how you thought this would have done what you described.
I really don't know much about code. What I wanted was for the sensor to read the distance in the serial monitor just for visual means. But it doesn't have to. But you're saying the input from the Bluetooth is read in the serial monitor and then that controls the motors? If that's the case I need a different string of code for the sensor to only stop the motors when it reads within a certain distance, basically I'm using the wrong string of code for the sensor? And also need to modify the motor code to pick up and react to the sensor?
A lot of people dive into Arduino not getting that you must have a rudimentary understanding of C or nothing will make sense. Trying different combinations of cut and paste will be frustrating for you. That's why there are block style code editors like Microsoft MakeCode for Arduino which presents a visual program flow that easier to understand.
Your question here didn't make sense to me at all. Your code doesn't read anything from bluetooth for starters, There's a bluetooth connection??
You said:
But then say the distance is just for visual means? So you don't care about distance?
Also I never said you had the wrong code for the sensor.
Anyway, I am super unclear what you want to do. You may find another poster here willing to work with you on it. But my advice is to step back and really understand what every line of code is doing. Otherwise it's difficult to make progress.
Sorry, it's connected to a bluetooth that it is receiving inputs F,B,L,R, or S The motors work fine based on these inputs.
When I said I just wanted the us sensor for visual means I meant I just wanted the serial monitor to read the distance so I can verify it is stopping at the specified distance, but the serial monitor wasn't as important as it actually stopping the motors.
Yes, I'm very quickly learning the importance of knowing C++
No problem good luck with your project.