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
/* 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
}
}
