I'm here regarding the issue I'm having building my project which is IR sensor controlled car with a LCD screen. I have built only 2 projects before this one that I have coded and designed their circuits, hence, I'm fairly new to all this. I expected this car to go forward, turn left & right, and to stop. I first used the code to identify the key code of my IR remote for the buttons I want to use to control my project, then I wrote the code given below. But, whenever I use the buttons I have programmed, the project respond only once to the IR remote and then freezes and continues on. Suppose, If I click the button which I have programmed it to go forward, it starts going forward, but then it stops responding to other buttons. I've tried using higher voltage and current batter but that doesn't seem to help.
Here is the code:
// Setting up LCD Display Here.
#include<LiquidCrystal.h>
int RS = 13;
int E = 12;
int D4 = 11;
int D5 = 10;
int D6 = 6;
int D7 = 2;
LiquidCrystal lcd(RS,E,D4,D5,D6,D7);
// Setting up IR reciver sensor here.
#include<IRremote.h>
int IR_Reciver_Pin = A5;
IRrecv irrecv(IR_Reciver_Pin);
decode_results results;
// Setting up DC motor pins.
/* Motor A connections */
int enA = 9;
int in1 = 8;
int in2 = 7;
/* Motor B connections */
int enB = 3;
int in3 = 5;
int in4 = 4;
void setup() {
// Initiating LCD display here.
lcd.begin(16,2);
lcd.print("Welcome!");
// Initiating IR reciver sensor here.
irrecv.enableIRIn();
// Initiating Serial Monitor.
Serial.begin(9600);
// Initiating DC motors.
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
// Turning motors off - Initial state
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
void loop() {
// Reading IR remote value.
if(irrecv.decode(&results))
{
int value = results.value;
Serial.print(F("CODE: "));
Serial.println(results.value);
irrecv.resume();
}
// Code for providing 5V to L293D H-Brigde.
analogWrite(enA, 255);
analogWrite(enB, 255);
// Code for going forward.
if(results.value==3772778233)
{
lcd.clear();
lcd.print(F("Rolling forward"));
lcd.setCursor(0,2);
lcd.print(F("captain!"));
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
// Code for turning right.
if(results.value==3772794553)
{
lcd.clear();
lcd.print(F("Turning towards"));
lcd.setCursor(0,2);
lcd.print(F("right."));
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
// Code for turning left.
if(results.value==3772819033)
{
lcd.clear();
lcd.print(F("Turning towards"));
lcd.setCursor(0,2);
lcd.print(F("left."));
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
// Code for stoping.
if(results.value==3772782313)
{
lcd.clear();
lcd.print(F("Halting captain!"));
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
}
This is the circuit diagram.
At first glance, I would say that the problem is that your if statments in loop should be nested inside : if(irrecv.decode(&results)){}
So, your loop should look something like this:
void loop() {
analogWrite(enA, 255);
analogWrite(enB, 255);
while(!(irrecv.decode(&results))); //It waits until no button is pressed
if(irrecv.decode(&results))
{
int value = results.value;
Serial.print(F("CODE: "));
Serial.println(results.value);
// Code for going forward.
if(results.value==3772778233)
{
lcd.clear();
lcd.print(F("Rolling forward"));
lcd.setCursor(0,2);
lcd.print(F("captain!"));
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
// Code for turning right.
if(results.value==3772794553)
{
lcd.clear();
lcd.print(F("Turning towards"));
lcd.setCursor(0,2);
lcd.print(F("right."));
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
// Code for turning left.
if(results.value==3772819033)
{
lcd.clear();
lcd.print(F("Turning towards"));
lcd.setCursor(0,2);
lcd.print(F("left."));
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
// Code for stoping.
if(results.value==3772782313)
{
lcd.clear();
lcd.print(F("Halting captain!"));
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
irrecv.resume();
}
}
Related
I written a code for a motor car and written basic functions like go_forward(), go_backward(), turn_left(), turn_right() and stop(). all functions are working properly in the loop() function.
but when I added the code lines for servo motor:
#include <Servo.h>
Servo servo = Servo();
void setup() {
servo.attach(A5);
}
only one wheel of the vehicle works.
the full code:
#include <Servo.h>
Servo servo = Servo();
int EnA = 3;
int In1 = 4;
int In2 = 5;
int EnB = 9;
int In3 = 12;
int In4 = 13;
void setup() {
servo.attach(A5);
//turn moter A
pinMode(EnA, OUTPUT);
pinMode(In1, OUTPUT);
pinMode(In2, OUTPUT);
//turn moter B
pinMode(EnB, OUTPUT);
pinMode(In3, OUTPUT);
pinMode(In4, OUTPUT);
analogWrite(EnA, 150);
//lowest - 100 highest - 200
analogWrite(EnB, 150);
//servo.write(150);
//Serial.begin(9600);
}
void loop() {
// In1 -> HIGH & In2 -> LOW ---- backward
go_forward();
//char c = Serial.read();
//Serial.println(c);
//delay(1000);
//if (c == '1'){
// digitalWrite(13, HIGH);
//}
//if (c == '2'){
// digitalWrite(13, LOW);
//}
}
void go_forward(){
// In1 -> HIGH & In2 -> LOW ---- backward
digitalWrite(In1, HIGH);
digitalWrite(In2, LOW);
digitalWrite(In3, LOW);
digitalWrite(In4, HIGH);
}
void go_backward(){
// In1 -> HIGH & In2 -> LOW ---- backward
digitalWrite(In1, LOW);
digitalWrite(In2, HIGH);
digitalWrite(In3, HIGH);
digitalWrite(In4, LOW);
}
void stop(){
// In1 -> HIGH & In2 -> LOW ---- backward
digitalWrite(In1, LOW);
digitalWrite(In2, LOW);
digitalWrite(In3, LOW);
digitalWrite(In4, LOW);
}
void turn_left(){
// In1 -> HIGH & In2 -> LOW ---- backward
digitalWrite(In1, LOW);
digitalWrite(In2, HIGH);
digitalWrite(In3, LOW);
digitalWrite(In4, HIGH);
}
void turn_right(){
// In1 -> HIGH & In2 -> LOW ---- backward
digitalWrite(In1, HIGH);
digitalWrite(In2, LOW);
digitalWrite(In3, HIGH);
digitalWrite(In4,LOW);
}
I just change the attach pin to 3(PWM digital pin and more other pins) but still same... and double checked that I've connected wires properly... and all are ok...
From the manual:
The Servo library supports up to 12 motors on most Arduino boards and
48 on the Arduino Mega. On boards other than the Mega, use of the
library disables analogWrite() (PWM) functionality on pins 9 and 10,
whether or not there is a Servo on those pins. On the Mega, up to 12
servos can be used without interfering with PWM functionality; use of
12 to 23 motors will disable PWM on pins 11 and 12.
You're using pin 9 for motor B.
Please read manuals. They exist for good reasons.
This code if for a simple robot car.
I'm trying to control the robot with 4 geared motors and L289 driver and standard RC Tx/Rx.
I have used some print statements to debug any errors.
When I try to move the robot forward/backward, I can see serial monitor printing froward/backward, but the robot doesn't move.
When I try to move if left/right it works fine. On commenting the left-right moving statements in code the robot does move forward and backward but fails to do so with all the if else statements uncommented.
Here's the code.
//Receiver pin
byte CH1_PIN = 9;
byte CH2_PIN = 10;
//Motor driver pins
int left_motor_pin1 = 4;
int left_motor_pin2 = 5;
int right_motor_pin1 = 6;
int right_motor_pin2 = 7;
void setup() {
// put your setup code here, to run once:
pinMode(CH1_PIN, INPUT);
pinMode(CH2_PIN, INPUT);
pinMode(left_motor_pin1, OUTPUT);
pinMode(left_motor_pin2, OUTPUT);
pinMode(right_motor_pin1, OUTPUT);
pinMode(right_motor_pin2, OUTPUT);
digitalWrite(left_motor_pin1, LOW);
digitalWrite(left_motor_pin2, LOW);
digitalWrite(right_motor_pin1, LOW);
digitalWrite(right_motor_pin2, LOW);
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
int ch_1 = pulseIn(CH1_PIN, HIGH);
int ch_2 = pulseIn(CH2_PIN, HIGH);
drive(ch_1, ch_2);
delay(5);
}
void drive(int move_left_right, int move_fwd_back) {
// Set direction for moving forward
if ( move_fwd_back > 1700 ) {
digitalWrite(left_motor_pin1, HIGH);
digitalWrite(left_motor_pin2, LOW);
digitalWrite(right_motor_pin1, HIGH);
digitalWrite(right_motor_pin2, LOW);
Serial.println("forward");
}
// Set direction for moving backwards.
else if (move_fwd_back < 1300) {
digitalWrite(left_motor_pin1, LOW);
digitalWrite(left_motor_pin2, HIGH);
digitalWrite(right_motor_pin1, LOW);
digitalWrite(right_motor_pin2, HIGH);
Serial.println("reverse");
}
else {
digitalWrite(left_motor_pin1, LOW);
digitalWrite(left_motor_pin2, LOW);
digitalWrite(right_motor_pin1, LOW);
digitalWrite(right_motor_pin2, LOW);
Serial.println("NONE");
}
// Set direction for moving left
if ( move_left_right < 1300 ) {
digitalWrite(left_motor_pin1, HIGH);
digitalWrite(left_motor_pin2, LOW);
digitalWrite(right_motor_pin1, LOW);
digitalWrite(right_motor_pin2, HIGH);
Serial.println("left");
}
//set directionfor moving right
else if (move_left_right > 1700) {
digitalWrite(left_motor_pin1, LOW);
digitalWrite(left_motor_pin2, HIGH);
digitalWrite(right_motor_pin1, HIGH);
digitalWrite(right_motor_pin2, LOW);
Serial.println("right");
}
else {
digitalWrite(left_motor_pin1, LOW);
digitalWrite(left_motor_pin2, LOW);
digitalWrite(right_motor_pin1, LOW);
digitalWrite(right_motor_pin2, LOW);
Serial.println("NONE");
}
}
The issue is that you have two if-else conditions - both changing the same outputs. So the 2nd if-else condition will always override what the 1st one has done.
eg. if you want the motor to just move forward, the code would set the motors to both move forward - however, immediately afterwards, the code decides there is no left/right input so sets the motors to stop. This is so fast you don't see any movement in the motors.
To start with, I would change the code so that the decision regarding the left/right input is inside the else condition of the forward/backward condition. This would give the forward/backward input priority over the left/right input.
i.e.
if ( move_fwd_back > 1700 ) {
digitalWrite(left_motor_pin1, HIGH);
digitalWrite(left_motor_pin2, LOW);
digitalWrite(right_motor_pin1, HIGH);
digitalWrite(right_motor_pin2, LOW);
Serial.println("forward");
}
// Set direction for moving backwards.
else if (move_fwd_back < 1300) {
digitalWrite(left_motor_pin1, LOW);
digitalWrite(left_motor_pin2, HIGH);
digitalWrite(right_motor_pin1, LOW);
digitalWrite(right_motor_pin2, HIGH);
Serial.println("reverse");
}
else {
// Set direction for moving left
if ( move_left_right < 1300 ) {
digitalWrite(left_motor_pin1, HIGH);
digitalWrite(left_motor_pin2, LOW);
digitalWrite(right_motor_pin1, LOW);
digitalWrite(right_motor_pin2, HIGH);
Serial.println("left");
}
//set directionfor moving right
else if (move_left_right > 1700) {
digitalWrite(left_motor_pin1, LOW);
digitalWrite(left_motor_pin2, HIGH);
digitalWrite(right_motor_pin1, HIGH);
digitalWrite(right_motor_pin2, LOW);
Serial.println("right");
}
else {
digitalWrite(left_motor_pin1, LOW);
digitalWrite(left_motor_pin2, LOW);
digitalWrite(right_motor_pin1, LOW);
digitalWrite(right_motor_pin2, LOW);
Serial.println("NONE");
}
}
//Motor A
const int motorpin1 = 6; // Pin 6 of L293
const int motorpin2 = 9; // Pin 3 of L293
void setup() {
pinMode(motorpin1, OUTPUT);
pinMode(motorpin2, OUTPUT);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
Serial.begin(9600);
}
// put your main code here, to run repeatedly:
void loop(){
if(Serial.available()>0)
{
char incomingByte = Serial.read();
Serial.println(incomingByte);
if(incomingByte=='a'){
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
delay(200);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, HIGH);
Serial.println("one way");
}
else if(incomingByte=='s'){
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
delay(200);
digitalWrite(motorpin1, HIGH);
digitalWrite(motorpin2, LOW);
Serial.println("other way");
}
else{
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
}
}
}
If we input a in the serial monitor the motor should rotate in one direction and if we input s the motor should rotate in another direction but it is not happening the motor is idle but i get the output like this:
a
one way
s
other way
There is no problem with hardware connections.
Could you please help me with this.Thanks in advance
According to the comments in your code, you are conecting arduino-pin-6 to the l293-pin-6, and arduino-pin-9 to the l293-pin-3.
According to this datasheet, the control pins in L293 are: 2, 7, 10, 15. So, I believe you are connecting it wrong. Also the pulses are being done in the wrong order (from HIGH to LOW and so on).
This should be the correct code (please look at the comments in the code):
//Motor A
const int motorpin1 = 6; // Pin 7 of L293
const int motorpin2 = 9; // Pin 2 of L293
const int motorenablepin = 10; // Pin 1 of L293
void setup() {
pinMode(motorpin1, OUTPUT);
pinMode(motorpin2, OUTPUT);
pinMode(motorenablepin, OUTPUT);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
digitalWrite(motorenablepin, HIGH); // we can let it enabled
Serial.begin(9600);
}
// put your main code here, to run repeatedly:
void loop(){
if(Serial.available()>0)
{
char incomingByte = Serial.read();
Serial.println(incomingByte);
if(incomingByte=='a'){
digitalWrite(motorpin1, HIGH);
digitalWrite(motorpin2, LOW);
Serial.println("one way");
}
else if(incomingByte=='s'){
digitalWrite(motorpin1, HIGH);
digitalWrite(motorpin2, LOW);
Serial.println("other way");
}
delay(200);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);
}
}
Please note that I added the motorenablepin that was missing. It must be conected to the l293-pin-1.
Also, since the LOW and LOW states are common to the code, you can simplify it as I did.
There was small mistake in the logic:
if(incomingByte=='a'){
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, HIGH);//changed to high
delay(10000);
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, LOW);//changed to low
Serial.println("one way");
}
else if(incomingByte=='s'){
digitalWrite(motorpin1, LOW);
digitalWrite(motorpin2, HIGH);//changed to high
delay(2000);
digitalWrite(motorpin1, LOW);//changed to low
digitalWrite(motorpin2, LOW);
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);
}
I build a bluetooth controled car that i have some problem with the code to it. Im using a switch case Statements to control it from my phone. The code works fine but i got a problem with the default: i want it to stop the car if it doesn't receive anything via the bluetooth. My code doesent seem to execute the default: at all. and i dont know whats the problem.
well here is my code.
int IN1 = 7;
int IN2 = 5;
int IN3 = 4;
int IN4 = 2;
int inByte = 0;
void setup() {
Serial.begin(9600);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
}
void loop() {
//read my bluetooth
if (Serial.available() > 0) {
inByte = Serial.read();
Serial.print("I received: ");
Serial.println(inByte);
switch (inByte) {
case 'i':
Serial.println("forward");
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
break;
case 'j':
Serial.println("left");
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
case 'l':
Serial.println("right");
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
break;
case 'k':
Serial.println("reverse");
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
break;
default:
Serial.println("stop");
digitalWrite(IN1, HIGH);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, HIGH);
}
}
}