Aurdino coding to control a motor - arduino

//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);

Related

Arduino only executing one set of if-else

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");
}
}

Issues regarding IR sensor controlled car

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();
}
}

changing the value of a variable multiple times and keeping that variable value throughout the entire loop in arduino

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.

A simple problem with arduino UNO about optimizing code

It this a really simple code about turning on an off some LEDs, but I want it not to be so repetititve
I've tried to make a loop, but i couldn't get it, I did my best, but I'm really bad at this :(( pls need someone's jelp
`
#define LED 2
#define LED2 3
#define LED3 4
#define LED4 5
#define LED5 6
void setup()
{
pinMode(LED, OUTPUT);
pinMode (LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
}
void loop()
{
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED2, HIGH);
delay(500);
digitalWrite(LED3, HIGH);
delay(250);
digitalWrite(LED4, HIGH);
delay(125);
digitalWrite(LED5, HIGH);
delay(500);
digitalWrite(LED, LOW);
delay(1000);
digitalWrite(LED2, LOW);
delay(500);
digitalWrite(LED3, LOW);
delay(250);
digitalWrite(LED4, LOW);
delay(125);
digitalWrite(LED5, LOW);
delay(500);
}`
I'll rewrite what Oleg Mazurov said in the comment:
#define NUMBER_OF_LEDS 5
static const uint8_t a_led[NUMBER_OF_LEDS] = {2, 3, 4, 5, 6};
static const uint16_t a_delay[NUMBER_OF_LEDS] = {1000, 500, 250, 125, 500};
void setup() {
for (int i = 0; i < NUMBER_OF_LEDS; i++) {
pinMode(a_led[i], OUTPUT);
}
}
void loop() {
for (int i = 0; i < NUMBER_OF_LEDS; i++) {
digitalWrite(a_led[i], !digitalRead(a_led[i]));
delay(a_delay[i]);
}
}

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);
}

Resources