My First Project

Hey there!

So this is my first project and I am trying to figure out if there is any problems with this configuration before I start on a standalone version. It is a sort of remote control I am building and it is functioning the way I have intended. Please keep in mind the power source will be replaced and the purpose is to power the unit via means of plugging it in. The motor is turned on by one of the push buttons but the settings can be adjusted prior to turning on the motor. The arduino is power by the power module.

My biggest concern is the wiring and use of my capacitor. Should I be using a diode somewhere as well to prevent back flow from the motor?

Please see attachments. If you feel my code is required please let me know and I will gladly post it.

Components:

-Mega 2560 R3
-Elegoo Power Module
-LCD 16x2
-3.3v Motor
-L293D
-100uF 50v capacitor
-4 push buttons

If this motor is reversible, the capacitor will go bang.

But we cannot tel as there is no code :frowning:

FYI

Thank you for replying! Yes the motor is reversible so that certainly concerns me :frowning:

Here is the code I am using, I will be building a class for the button eventually to eliminate some variables.

#include <LiquidCrystal.h>

//LCD Pins
LiquidCrystal LCD(0,1,6,5,4,3);

//Motor Pins
int ENABLE = 9;
int DIRA = 8;
int DIRB = 7;

//Button Pins
int P_BUTTON = 13;
int SU_BUTTON = 12;
int SD_BUTTON = 11;
int M_BUTTON = 10;

//Global Variables
int motor_toggleState;
int motor_lastButtonState = 1;
long unsigned int motor_lastPress;

int increase_lastButtonState = 1;
long unsigned int increase_lastPress;

int decrease_lastButtonState = 1;
long unsigned int decrease_lastPress;

int mode_lastButtonState = 1;
long unsigned int mode_lastPress;

int debounceTime = 20;

int mode = 1;
String modeText = "forward";

float motorSpeed = 127.5;
bool motorOn = false;
 
void setup() 
{ 
  //initialize LCD
  LCD.begin(16,2);
  LCD.print("Remote Control");
  LCD.setCursor(0,1);
  LCD.print("By Me");
  delay(2000); //give time to read message/brand
  LCD.clear(); //clear LCD for display values
  
  //set outputs
  pinMode(ENABLE,OUTPUT);
  pinMode(DIRA,OUTPUT);
  pinMode(DIRB,OUTPUT);

  //set inputs
  pinMode(P_BUTTON,INPUT_PULLUP); // pull up resistor to eliminate "noise" 
  pinMode(SU_BUTTON,INPUT_PULLUP); 
  pinMode(SD_BUTTON,INPUT_PULLUP); 
  pinMode(M_BUTTON,INPUT_PULLUP);
}

void loop() 
{
  handleLCD();      
  handleMotor();
  handleModes();
  
  speedUp(); 
  slowDown();
  changeMode();    
}

void handleLCD()
{
  LCD.setCursor(0,0);
  LCD.print("Mode: ");
  LCD.print(modeText);

  LCD.setCursor(0,1);
  LCD.print("Power: ");
  LCD.print((int)((motorSpeed/255) * 100)); //display motorSpeed as a percentage
  LCD.print("% ");
}

void handleModes()
{
  if(mode == 1) //forward
  {
    modeText = "forward";
    digitalWrite(DIRA,HIGH); 
    digitalWrite(DIRB,LOW); 
  }
  else if(mode == 2) //reverse
  {
    modeText = "reverse";
    digitalWrite(DIRA,LOW); 
    digitalWrite(DIRB,HIGH); 
  }
}

void handleMotor()
{
  int motor_buttonState = digitalRead(P_BUTTON);

  if((millis() - motor_lastPress) > debounceTime)
  {
    motor_lastPress = millis();

    if(motor_buttonState == 0 && motor_lastButtonState == 1)
    {
      motor_toggleState =! motor_toggleState;
      if(motor_toggleState == 0) 
      {
        motorOn = false;      
      } 
      else 
      {
        motorOn = true;                         
      }
      motor_lastButtonState = 0;
    }
    else if(motor_buttonState == 1 && motor_lastButtonState == 0)
    {
      motor_lastButtonState = 1;
    }
  }  

  if(!motorOn)
  {
    digitalWrite(ENABLE,LOW); 
    digitalWrite(DIRA,LOW); 
    digitalWrite(DIRB,LOW);
  }
  else
  {
    analogWrite(ENABLE,motorSpeed);
  }
}

void speedUp()
{
  int increase_buttonState = digitalRead(SU_BUTTON);

  if((millis() - increase_lastPress) > debounceTime)
  {
    increase_lastPress = millis();
  
    if(increase_buttonState == 0 && increase_lastButtonState == 1)
    {
      if(motorSpeed <= 191.25) 
      {
        motorSpeed += 63.75; 
      } 
      else
      {
        motorSpeed = 255; //max speed
      }
      increase_lastButtonState = 0;
    }
    else if(increase_buttonState == 1 && increase_lastButtonState == 0)
    {
      increase_lastButtonState = 1;
    }
  }
}

void slowDown()
{
  int decrease_buttonState = digitalRead(SD_BUTTON);

  if((millis() - decrease_lastPress) > debounceTime)
  {
    decrease_lastPress = millis();
  
    if(decrease_buttonState == 0 && decrease_lastButtonState == 1)
    {
      if(motorSpeed >= 127.5) 
      {
        motorSpeed -= 63.75; 
      } 
      else
      {
        motorSpeed = 63.75; //min speed
      }
      decrease_lastButtonState = 0;
    }
    else if(decrease_buttonState == 1 && decrease_lastButtonState == 0)
    {
      decrease_lastButtonState = 1;
    }
  }
}

void changeMode()
{
  int mode_buttonState = digitalRead(M_BUTTON);

  if((millis() - mode_lastPress) > debounceTime)
  {
    mode_lastPress = millis();
  
    if(mode_buttonState == 0 && mode_lastButtonState == 1)
    {
      if(mode < 2) 
      {
        mode++;
      } 
      else
      {
        mode = 1;
      }
      mode_lastButtonState = 0;
    }
    else if(mode_buttonState == 1 && mode_lastButtonState == 0)
    {
      mode_lastButtonState = 1;
    }
  }
}

Remove the capacitor across the motor leads.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html . Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read

Pin 16 of the L293 needs to be connected to 5V, this is the internal logic circuit supply.
Pin 8 is the motor power supply.

Thanks... Tom... :slight_smile:

Thanks again for the replies everyone! I have now removed the capacitor and hooked up pin 16 of the L293D to the 5v supply. I was under the impression that pin 16 was to power another motor for outputs 3 and 4. Am I correct to assume what I should be looking into is a diode rectifier to prevent feedback from the motor?

Diodes are in the I.C.