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");
}
}
Related
I made some simple code and car using 1 arduino, 1 hc-05, 2 L298N, and 4 batteries. I control them with the app Bluetooth RC controller on android. When I send a command on the app, the car sometimes has some delays, varying 4-8 seconds, sometimes it disconnects completely. Here is the code. I don't think the code has any problem and my friend brought up that the problem might be the distortion from a magnetic field because I use some lead to weld some wires
char t;
int fr1= 2;
int fr2=3;
int fl1=12;
int fl2=13;
int br1=4;
int br2=5;
int bl1 = 7;
int bl2=8;
int Mfr=6;//speed setting
int Mfl=9;
int Mbr=10;
int Mbl=11;
float Speed;
void setup() {
pinMode(fr1,OUTPUT); //fr1
pinMode(fr2,OUTPUT); //fr2
pinMode(fl1,OUTPUT); //fl1
pinMode(fl2,OUTPUT); //fl2
pinMode(br1,OUTPUT); //br1
pinMode(br2,OUTPUT); //br2
pinMode(bl1,OUTPUT); //bl1
pinMode(bl2,OUTPUT); //bl2
Serial.begin(9600);
}
void loop() {
if(Serial.available()){
t = Serial.read();
Serial.println(t);
}
if(t == 'F'){
digitalWrite(fr1, LOW);
digitalWrite(fr2, HIGH);
digitalWrite(fl1, LOW);
digitalWrite(fl2, HIGH);
digitalWrite(br1, LOW);
digitalWrite(br2, HIGH);
digitalWrite(bl1, LOW);
digitalWrite(bl2, HIGH);
}
else if(t == 'B'){
digitalWrite(fr1, HIGH);
digitalWrite(fr2, LOW);
digitalWrite(fl1, HIGH);
digitalWrite(fl2, LOW);
digitalWrite(br1, HIGH);
digitalWrite(br2, LOW);
digitalWrite(bl1, HIGH);
digitalWrite(bl2, LOW);
}
else if(t == 'L'){
digitalWrite(fr1, LOW);
digitalWrite(fr2, HIGH);
digitalWrite(fl1, HIGH);
digitalWrite(fl2, LOW);
digitalWrite(br1, HIGH);
digitalWrite(br2, LOW);
digitalWrite(bl1, LOW);
digitalWrite(bl2, HIGH);
}
else if(t == 'R'){
digitalWrite(fr1, HIGH);
digitalWrite(fr2, LOW);
digitalWrite(fl1, LOW);
digitalWrite(fl2, HIGH);
digitalWrite(br1, LOW);
digitalWrite(br2, HIGH);
digitalWrite(bl1, HIGH);
digitalWrite(bl2, LOW);
}
else if(t == 'S'){ //STOP (all motors stop)
digitalWrite(fr1, LOW);
digitalWrite(fr2, LOW);
digitalWrite(fl1, LOW);
digitalWrite(fl2, LOW);
digitalWrite(br1, LOW);
digitalWrite(br2, LOW);
digitalWrite(bl1, LOW);
digitalWrite(bl2, LOW);
}
}
Is it possible its gotten flooded by bytes, serial.read had to read? I imagine maybe the app is sending a lot of characters to the Arduino, is it possible to check/apply a (short) delay in that app?
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();
}
}
In Arduino I am having issues where I can't change the value of a variable in the void loop(). Here is the code I use:
void setup() {
pinMode(10, INPUT);
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
}
int speed = 1000;
int sub = 100;
void loop() {
if (digitalRead(13) == HIGH) {
speed = speed-sub;
} else {
speed = speed;
}
digitalWrite(0, HIGH);
digitalWrite(1, LOW);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
delay(speed);
if (digitalRead(13) == HIGH) {
speed = speed - sub;
} else {
speed = speed;
}
digitalWrite(0, LOW);
digitalWrite(1, HIGH);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
delay(speed);
if (digitalRead(13) == HIGH) {
speed = speed - sub;
} else {
speed = speed;
}
digitalWrite(0, LOW);
digitalWrite(1, LOW);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
delay(speed);
}
Is there any way I can change the value of a variable multiple times during one loop and have that variable value kept until the end of the loop? Also, will I need to add a new function, and if so how should I go about it?
It seems that you want to control the speed of a running light using push button.
but in your code that is not possible because you use blocking function that will disable reading of button for a while.
i suggest you run the code in your mind line by line. then you will understood what i mean.
//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);
I am trying to run a for loop inside an if statement but it keeps repeating. I basically want pin 4 to blink 6 times when I press a button on pin 2. When z becomes a 6 in the for loop, the if statement makes it so that z is reset to 0 and the for loop restart all over again. Therefore the LED on pin 4 keeps blinking on and on. Anyone can help so that it blinks only 6 times? Here is the code:
int switchState = 0;
void setup()
{
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(2, INPUT);
}
void loop()
{
switchState = digitalRead(2);
if (switchState == LOW)
{
digitalWrite(5, LOW);
digitalWrite(4, LOW);
}
else
{
digitalWrite(5, HIGH);
for (int z=0; z<6; z++)
{
digitalWrite(4, HIGH);
delay(100);
digitalWrite(4, LOW);
delay(100);
}
}
}
Try out this:
void setup()
{
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(2, INPUT);
}
void loop()
{
//Deactivate all leds
digitalWrite(5, LOW);
digitalWrite(4, LOW);
if (digitalRead(2) == HIGH) { //If the button is pressed...
digitalWrite(5, HIGH);
//blink
for (int z=0; z<6; z++)
{
digitalWrite(4, HIGH);
delay(100);
digitalWrite(4, LOW);
delay(100);
}
while (digitalRead(2) == HIGH) { // Wait until release the button
delay(10);
}
}
}