Go Down

Topic: Dc motor with L293D (Read 1 time) previous topic - next topic

Isinsane

I really hope i am posting this in the right place,
i only have been going around Arduino and lately and i am practically new to circuits too, so take it easy on me , i will try to make myself as clear as possible and try to ask the right question

As you will see in the diagram i've been trying to control a DC motor with L293D to control the speed and direction to get the motor to spin back and forth for an amount of periods that i change with analog potentiometer and display few things on an oled display ,
so far it works the right i hope  , but i have a gut feeling that i am doing something wrong i am pretty sure the code at least can be more efficient and i would really love to hear suggestions ,





//notes
The code is  mostly fragments from adafruit arduino and multitasking




Code: [Select]
/* Oled Display int */
//---------------------------------------------------

#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>

// OLED display TWI address
#define OLED_ADDR   0x3C

  
// reset pin not used on 4-pin OLED module
Adafruit_SSD1306 display(-1);  // -1 = no reset pin

// 128 x 64 pixel display
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
//---------------------------------------------------



// initialize Dc motor pins
//---------------------------------------------------
int in1 = 9; // used for control too
int in2 = 10;
int inspeed = 11;



int butt = 7; // button
int red = 2; //led 1
int yellow = 3; // led 2

int in1state = LOW; //in1 state update
int in2state = HIGH; //in2 state update
unsigned long potvalue = map(analogRead(A0), 0, 1023, 60000 ,900000);
unsigned long previousMillis = potvalue;        // update millis

int buttonState = 0;         // current state of the button



 
void setup()
{
 // initialize Dc motor pins
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(inspeed, OUTPUT);  
  pinMode(butt, OUTPUT);   //button
  pinMode(red, OUTPUT);
  pinMode(yellow, OUTPUT);



  
  // initialize and clear display
  //---------------------------------------------------
  display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
  display.clearDisplay();
  display.display();
  // display a line of text
  display.setTextSize(1.5);
  display.setTextColor(WHITE);
  display.setCursor(0,10);
  display.print("Sam Fishing");
    display.setTextSize(2);
  display.setCursor(0,25);
  display.print("><(((('>");

  
  // update display with all of the above graphics
  display.display();
  // Small delay before clearing the display
  delay(1000);
  display.clearDisplay();
}



void loop()
{
    buttonState = digitalRead(butt);

  //Display
  //---------------------------------------------------
  unsigned long podisplay = (map(analogRead(A0), 0, 1023, 60000 ,900000)/60000);
  display.clearDisplay();
  display.setTextSize(1.5);
  display.setTextColor(WHITE);
  display.setCursor(10,10);
  display.print("Round Time");

  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(10,30);
  display.print(podisplay);

  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(60,50);
  display.print("Mints");
  
  // update display with all of the above graphics
  display.display();
  delay(1);        // delay in between reads for stability


if (buttonState == LOW) {
 //Set the speed
  //---------------------------------------------------
  analogWrite(inspeed, 175); //  speed
  // check to see if it's time to change the state
  //---------------------------------------------------
  unsigned long currentMillis = millis();
  // DC motor update
  //---------------------------------------------------
  if((in1state == HIGH) && (currentMillis - previousMillis >= potvalue))
  {
    in1state = LOW;  // Turn it off
    previousMillis = currentMillis;  // Remember the time
    
    digitalWrite(in1, in1state);  // Update the actual values
    digitalWrite(in2, HIGH);  // Update the actual values
    digitalWrite(red, HIGH);  // red on
    digitalWrite(yellow, LOW);  // yellow off
  }
  else if ((in1state == LOW) && (currentMillis - previousMillis >= potvalue))
  {
    in1state = HIGH;  // turn it on
    previousMillis = currentMillis;   // Remember the time
    digitalWrite(in1, in1state);    // Update the actual values
    digitalWrite(in2, LOW);  // Update the actual values
    digitalWrite(red, LOW);  // red off
    digitalWrite(yellow, HIGH);  // yellow on
  }
    } else {
  unsigned long potvalue = map(analogRead(A0), 0, 1023, 60000 ,900000);
  
  display.clearDisplay();
  display.setTextSize(1.5);
  display.setTextColor(WHITE);
  display.setCursor(10,20);
  display.print("New round time OK");
  // update display with all of the above graphics
  display.display();
  delay(1000);        // delay in between reads for stability
    }
}

Due_unto

I don't see how your project is powered.  Since you say it works right then the physical layout includes power connections which are missing in your Fritzy.  Very important to show exactly how you are powering these things, especially when a motor is involved.
A good rule regarding motors is to use a seperate power source for the motor circuit and only share a common ground and logic supply to the L293D.  Reason being that DC brushed motors are extremely noisy electrically which can upset the Arduino.
As far as improving your code, if it is working to your expectations then it would be a great learning experience for you to streamline portions of your code yourself. I have always found that to be very satisfying to come up with my own solutions and still keep the script running properly but leaner.
Do not look into laser with remaining good eye.

TomGeorge

Hi,
Just a couple of edits to make your code easier to read.


Code: [Select]
int in1 = 9; // used for control too
int in2 = 10;
int inspeed = 11;

int butt = 7; // button
int red = 2; //led 1
int yellow = 3; // led 2


Change to;

Code: [Select]
byte in1Pin = 9; // used for control too
byte in2Pin = 10;
byte inspeedPin = 11;
byte buttPin = 7; // button
byte redLEDPin = 2; //led 1
byte yellowLEDPin = 3; // led 2


That way you will know they are Pin numbers not variables,  bytes because they will not be higher than 255, uses less memory.
As your button is an INPUT:

Code: [Select]
 pinMode(butt, OUTPUT);   //button

Should be;

Code: [Select]
 pinMode(butt, INPUT);   //button

With your code in the IDE, press  CTRL -T, it will give you logical indents to your loops.
Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

Isinsane

I don't see how your project is powered.  Since you say it works right then the physical layout includes power connections which are missing in your Fritzy.  Very important to show exactly how you are powering these things, especially when a motor is involved.
A good rule regarding motors is to use a seperate power source for the motor circuit and only share a common ground and logic supply to the L293D.  Reason being that DC brushed motors are extremely noisy electrically which can upset the Arduino.
As far as improving your code, if it is working to your expectations then it would be a great learning experience for you to streamline portions of your code yourself. I have always found that to be very satisfying to come up with my own solutions and still keep the script running properly but leaner.
Sorry this is my first project ever , i trying to be self taught and that comes with difficulties you know , but it makes sense what you said, so

i am using 5-12 V geared DC motor
2 sources 5 V to Arduino and 7.4 lippo battery to the Dc motor both grounded in the Arduino 

if you need these information packed with the fritzy sketch i can do it.

and yes i am trying to do what you said exactly about the code i discarded the whole code thing for now and just trying to study-like a bit before i get started with it again

thanks 

Go Up