LED dimmer than should be and some other stuff related to motors. L293D

I’ve got two problems, one which i will explain here and the other as comments in my code.

  1. LED’s are dimmer than they should be, meaning that normally they are brighter. I have all the output pins set properly and set them HIGH when they need to be. Code explains its self fairly good enough.

  2. I can’t seem to get the POT value to start from zero in the Backwards if statement :

#define enablePin 9 // PIN 1 OF L293D
#define inPin1 10 // IN1 pin
#define inPin2 11 // IN2 pin
#define gLed 6 // Forward LED indicator
#define rLed 7 // Backwards LED indicator

void setup() {
  Serial.begin(9600);
  pinMode(enablePin, OUTPUT);
  pinMode(inPin1, OUTPUT);
  pinMode(inPin2, OUTPUT);
  pinMode(rLed, OUTPUT);
  pinMode(gLed, OUTPUT);
}

void loop() {
  boolean forward;
  if ( 120 < getSpeed() < 130 ) {
    setMotor(0, forward);
    digitalWrite(gLed , LOW);
    digitalWrite(rLed , LOW);
  }
  if ( getSpeed() > 130 ) { // This part works flawlessly and i get a smooth rpm from 0 to 255
    int motor_speed_forward = map(getSpeed(), 130, 255, 0, 255); // This is where the magic is happening
    setMotor(motor_speed_forward, !forward);
    Serial.print("Motor Speed: ");
    Serial.print(motor_speed_forward);
    Serial.println(" Forward");
    digitalWrite(gLed , HIGH); // Not as bright as should be
  }
  if ( getSpeed() < 120) {  // This is where i have a problem. Check Serial Monitor for more information. It doesn't start from 0
                            // Rather it starts from 255 and counts to 0. How to solve this problem?
    int motor_speed_backward = map(getSpeed(), 0, 120, 0, 255); // I cant figure out how to make *1 work from 0 to 255
    setMotor(motor_speed_backward, !forward);
    Serial.print("Motor Speed: ");
    Serial.print(motor_speed_backward); // *1
    Serial.println(" Backwards"); 
    digitalWrite(rLed , HIGH);
  }
}

// This is where i get my pot value and convert it to 0-255
int getSpeed() {
  int pot_val = analogRead(A0);
  int pot_val_convert = map(pot_val, 0, 1023, 0, 255);
  return pot_val_convert;
}

// Motor function
void setMotor(int motor_speed, boolean forward) {
  analogWrite(enablePin, motor_speed);
  digitalWrite(inPin1, forward);
  digitalWrite(inPin2, !forward);
}

The problems are fairly simple yet i don’t understand what the problems are…

if ( 120 < getSpeed() < 130 ) {

Wrong-O. You don’t get to invent syntax. You have to use that C++ expects.

What value resistors do you have for the leds?

I don’t understand the part about the mapping. Are you saying that you turn the pot down and as it hits 120 it gives you a value of 255? That’s what you told map to do. If you want the pot value of 120 to mean 0 and the pot value of 0 to give 255 then you need to revers the last two numbers in your map call.

If you want to use a potentiometer to give directional speed control with centre off then do it like this

potVal = analogRead(potPin); // range 0 - 1023
potVal = potVal - 512; // range -512 to + 511
If (potVal < 0) {
  direction = 'R';
}
else {
  direction = 'F';
}
motorPWMval = abs(potVal / 2) - 1; // speed range from 0 - 255
if (motorPWMval < 10) {   // create a dead band to make it easier to stop the motor 
  motorPWMval = 0;
}

Also, there is no advantage in calling the function getVal() lots of times. Just call it once in loop() and save its value to a variable. Then all your tests are using the same value which makes debugging a great deal easier.

…R

if ( 120 < getSpeed() < 130 )

C doesn’t work like that, if that is what you want to do you have to do it:-

int s = getSpeed(); 
if ( s >120  && s< 130 )

Delta_G:

if ( 120 < getSpeed() < 130 ) {

Wrong-O. You don’t get to invent syntax. You have to use that C++ expects.

What value resistors do you have for the leds?

I don’t understand the part about the mapping. Are you saying that you turn the pot down and as it hits 120 it gives you a value of 255? That’s what you told map to do. If you want the pot value of 120 to mean 0 and the pot value of 0 to give 255 then you need to revers the last two numbers in your map call.

Ok, Mapping fixed.

Grumpy_Mike:

if ( 120 < getSpeed() < 130 )

C doesn’t work like that, if that is what you want to do you have to do it:-

int s = getSpeed(); 

if ( s >120  && s< 130 )

Great :smiley:
Now LED’s work fine.

New problem :frowning:

The speed of my motor is no longer linear. What i mean is that when it reaches 255, say when going forward, it lags behind what is being showed in Serial monitor; It takes some time to reach full speed.
Same issue on backwards.

The code has changed? Post the new version.

Delta_G:
The code has changed? Post the new version.

not much:

#define enablePin 9 // PIN 1 OF L293D
#define inPin1 10 // IN1 pin
#define inPin2 11 // IN2 pin
#define gLed 6 // Forward LED indicator
#define rLed 7 // Backwards LED indicator

void setup() {
  Serial.begin(9600);
  pinMode(enablePin, OUTPUT);
  pinMode(inPin1, OUTPUT);
  pinMode(inPin2, OUTPUT);
  pinMode(rLed, OUTPUT);
  pinMode(gLed, OUTPUT);
}

void loop() {
  boolean forward;
  if ( getSpeed()< 130 && getSpeed()> 120 ) {
    setMotor(0, forward);
    Serial.print("Motor Speed: ");
    Serial.println(getSpeed());
    digitalWrite(gLed , LOW);
    digitalWrite(rLed , LOW);
  }
  if ( getSpeed() > 130 ) { // This part works flawlessly and i get a smooth rpm from 0 to 255
    int motor_speed_forward = map(getSpeed(), 130, 255, 0, 255); // This is where the magic is happening
    setMotor(motor_speed_forward, !forward);
    Serial.print("Motor Speed: ");
    Serial.print(motor_speed_forward);
    Serial.println(" Forward");
    digitalWrite(gLed , HIGH);
  }
  if ( getSpeed() < 120) {  // This is where i have a problem. Check Serial Monitor for more information. It doesn't start from 0
                            // Rather it starts from 255 and counts to 0. How to solve this problem? ( *2 FIXED )
    int motor_speed_backward = map(getSpeed(), 0, 120, 255, 0); // I cant figure out how to make * work from 0 to 255 ( *2 MAPPING CHANGED )
    setMotor(motor_speed_backward, forward);
    Serial.print("Motor Speed: ");
    Serial.print(motor_speed_backward);
    Serial.println(" Backwards");
    digitalWrite(rLed , HIGH);
  }
}

// This is where i get my pot 
int getSpeed() {
  int pot_val = analogRead(A0);
  int pot_val_convert = map(pot_val, 0, 1023, 0, 255);
  return pot_val_convert;
}

void setMotor(int motor_speed, boolean forward) {
  analogWrite(enablePin, motor_speed);
  digitalWrite(inPin1, forward);
  digitalWrite(inPin2, !forward);
}

What i mean is that when it reaches 255, say when going forward, it lags behind what is being showed in Serial monitor; It takes some time to reach full speed.

Yes it will especially if the motor is lightly loaded. That is how they work.