I am busy whit project to control ibt2 whit softstart, current sensing.
when current is to high two things can happen, or program stop motor and wait 1sec
or it turns the motor the orther way around.
this using arduino nano
for information on display and the amount of if else i used "void oled()" to easily put the code on several places
softstart/stop is working but only right is working correctly
if i turn on leftsoft(); than the display does not show anything anymore, also if i switch to right.
but whit oled() it does work correctly
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// On an arduino UNO: A4(SDA), A5(SCL)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int amp = A0; // amperage meter on pin A0
int pot1 = A6; //potentionmeter 1
int pot2 = A7; //potentionmeter 2
int switchab = A1; // switch a and b
int switchcd = A2; // switch c and d
int pwm1 = 5; // send left pwm speed
int pwm2 = 6; // send right pwm speed
int val = 0;
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int maxc = 50;
int settime = 1;
int setpwm1 = 0;
int setpwm2 = 0;
int testpwm1 = 0;
int testpwm2 = 0;
int turn1 = 0;
int turn2 = 0;
int turn3=0;
int i=0;
float ampsens; // measured value from currentsensor
float amptest; // to calculate how mutch current (can be negative and positive)
float current; // amptest value turned negative to positive, keep positive value positive
void setup()
{
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Show initial display buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.
display.display();
delay(20); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
}
void loop(){
//--------------------------------------------------------------------------------------------------------
//clear display and set
display.clearDisplay();
display.setTextSize(1); // Draw 2X-scale text
display.setTextColor(SSD1306_WHITE);
//--------------------------------------------------------------------------------------------------------
//Read all inputs
ampsens = analogRead(amp); // currentsensor
int AB = analogRead (switchab); //switch a and b
int CD = analogRead (switchcd); // switch c and d
int p1 = analogRead (pot1); //potentionmeter 1
int p2 = analogRead (pot2); //potentionmeter 2
turn2=map(p2,0,1023,0,255);
//--------------------------------------------------------------------------------------------------------
//turn to amperage and set negative value to positve
amptest = (2.5 - (ampsens * (5.0 / 1024.0)) )/0.066; // turn measured currentsensor value to Amperage value
if (amptest < 0) { // if else to turn negative to positive and keep positive positive
current = (amptest*(-1));
}
else {
current = amptest;
}
//------------------------------------------------------------------------------------------------------
//crapping whitch switch is on or off
//switch ab value same as for switch cd
// A on at507, 625
// A off at 0 and 382
// B on at 382 and 625
// B off at 0 and 507
if ((AB > 575 && AB < 675) || (AB > 450 && AB < 550)) {
a = 1; }
else if (AB < 50 || (AB > 330 && AB < 430)) {
a = 0; }
else {
a = 2; }
if ((AB > 575 && AB < 675) || (AB > 330 && AB < 430)) {
b = 1; }
else if (AB < 50 || (AB > 450 && AB < 550)) {
b =0; }
else {
b = 2; }
//switch cd
if ((CD > 575 && CD < 675) || (CD > 450 && CD < 550)) {
c = 1; }
else if (CD < 50 || (CD > 330 && CD < 430)) {
c = 0; }
else {
c = 2; }
if ((CD > 575 && CD < 675) || (CD > 330 && CD < 430)) {
d = 1; }
else if (CD < 50 || (CD > 450 && CD < 550)) {
d =0; }
else {
d = 2; }
//--------------------------------------------------------------------------------------------------------
// to set time or max current whit switch d
if (d==0){
maxc = map(p1,0,1023,0,50);
}
else if (d==1){
settime = map(p1,0,1023,0,50);} //50 ms for delay
else{
maxc = 0;
settime = 0;
}
//-------------------------------------------------------------------------------------------------------
//autorotate
if (a == 1) {
display.setCursor(0,40);
display.print("AUTO");
if (current<maxc && setpwm1 == 0)
{
// analogWrite(pwm1, 0);
delay (10);
// analogWrite(pwm2, turn1);
// testpwm1=0;
// testpwm2=turn1;
display.print(", left");
if (b == 0)
{
setpwm1 = 0;
setpwm2 = 1;
}
else
{
setpwm1 = 1;
setpwm2 = 0;
}
}
else if (current<maxc && setpwm2 == 0)
{
// analogWrite(pwm2, 0);
delay (10);
// analogWrite(pwm1, turn1);
// testpwm1=turn1;
// testpwm2=0;
display.print(", right");
if (b == 1)
{
setpwm1 = 1;
setpwm2 = 0;
}
else
{
setpwm1 = 0;
setpwm2 = 1;
}
}
else if (current>=maxc && setpwm1 == 0)
{
// analogWrite(pwm2, 0);
delay (10);
// analogWrite(pwm1, turn1);
setpwm1 = 1;
setpwm2 = 0;
// testpwm1=turn1;
// testpwm2=0;
display.print(", right high");
}
else if (current>=maxc && setpwm2 == 0)
{
// analogWrite(pwm1, 0);
delay (10);
// analogWrite(pwm2, turn1);
setpwm1 = 0;
setpwm2 = 1;
// testpwm1=0;
// testpwm2=turn1;
display.print(", left high");
}
}
//------------------------------------------------------------------------------------------------
// Only turning and stopping when current is to high
else if(a==0)
{
display.setCursor(0,40);
display.print("Turn");
if (b == 1)
{
if (current<=maxc)
{
// analogWrite(pwm1, turn1);
// analogWrite(pwm2, 0);
// testpwm1=turn1;
// testpwm2=0;
setpwm1 = 1;
setpwm2 = 0;
display.print(", right");
}
else
{
// analogWrite(pwm1, 0);
// analogWrite(pwm2, 0);
// testpwm1=0;
// testpwm2=0;
setpwm1 = 1;
setpwm2 = 0;
display.print(", right stopped!");
}
}
else
{
if (current<=maxc)
{
analogWrite(pwm2, turn1);
analogWrite(pwm1, 0);
// testpwm1=0;
// testpwm2=turn1;
setpwm1 = 0;
setpwm2 = 1;
display.println(", left");
}
else
{
analogWrite(pwm1, 0);
analogWrite(pwm2, 0);
// testpwm1=0;
// testpwm2=0;
display.print(", left stopped!");
}
}
}
else
{
display.println(", error");
display.display();
analogWrite(pwm1, 0);
analogWrite(pwm2, 0);
}
//--------------------------------------------------------------------------------------------------------
// softstart on/off switch c
if (c == 1){
if (setpwm1==1 &&setpwm2==0)//turn right softstart
{
softright(); //run void softright
}
else if (setpwm2==1 && setpwm1==0)//turn left softstart
{
oled();
//leftsoft(); //run void softleft
}
else if (setpwm1==0&&setpwm2==0)
{
analogWrite(pwm1,0);
analogWrite(pwm2,0);
display.setCursor(0,50);
display.print("sofstart both off");
oled(); //run void oled
}
else //both off or when error occured.
{
analogWrite(pwm1,0);
analogWrite(pwm2,0);
display.setCursor(0,50);
display.print ("softstart on error");
oled(); //run void oled
}
}
else
{
if (setpwm1==1 &&setpwm2==0)//turn right
{
analogWrite(pwm1,turn2);
analogWrite(pwm2,0);
testpwm1 = turn2;
testpwm2 = 0;
display.setCursor(0,50);
display.print("softstart off right");
oled(); //run void oled
}
else if (setpwm2==1 && setpwm1==0)//turn left
{
analogWrite(pwm1,0);
analogWrite(pwm2,turn2);
testpwm1 = 0;
testpwm2 = turn2;
display.setCursor(0,50);
display.print("softstart off left");
oled(); //run void oled
}
else if (setpwm1==0&&setpwm2==0)
{
analogWrite(pwm1,0);
analogWrite(pwm2,0);
testpwm1 = 0;
testpwm2 = 0;
display.setCursor(0,50);
display.print("sofstart both off");
oled(); //run void oled
}
else //both off or when error occured.
{
analogWrite(pwm1,0);
analogWrite(pwm2,0);
testpwm1 = 0;
testpwm2 = 0;
display.setCursor(0,50);
display.print ("softstart off error");
oled(); //run void oled
}
}
}
//--------------------------------------------------------------------------------------------
//code for softstart right
void softright() {
if (testpwm2>0){ //if left is turning slow it down first, than speed up
for (i=testpwm2; i>=0; i--){ // slow down left-----
//analogWrite(pwm2,i);
testpwm2 = i;
display.setCursor(0,50);
display.println("softstart ON RIGHT");
display.print("left slowdown");
oled();
delay(settime);
}
}
else if (testpwm2==0)
{
if (testpwm1<turn2) //speed up right to turn2 value
{
for (i=testpwm1; i<=turn2; i++){ //speed up right------
// analogWrite(pwm1,i);
testpwm1 = i;
display.setCursor(0,50);
display.println("softstart ON RIGHT");
display.print("right SPEED UP");
oled();
delay(settime);
}
}
else if(testpwm1>turn2) // speed down right to turn2 value
{
for (i=testpwm1; i>=turn2; i--){ // slow down left-----
//analogWrite(pwm2,i);
testpwm1 = i;
display.setCursor(0,50);
display.println("softstart ON RIGHT");
display.print("right SPEED DOWN");
oled();
delay(settime);
}
}
else
{
testpwm1=turn2;
display.setCursor(0,50);
display.println("softstart ON RIGHT");
oled();
delay(settime);
}
}
else //right error
{
for (i=turn2; i>=0; i--){ // slow down right-----
testpwm1 = i;
testpwm2=i;
analogWrite(pwm1,i);
analogWrite(pwm2,i);
display.setCursor(0,50);
display.println("softstart ON RIGHT");
display.print("right ERROR DOWN");
oled();
delay(settime);
}
}}
//---------------------------------------------------------------------------------------------------
//softstart left
void leftsoft() {
if (testpwm1>0){ //if left is turning slow it down first, than speed up
for (i=testpwm1; i>=0; i--){ // slow down right-----
//analogWrite(pwm1,i);
testpwm1 = i;
display.setCursor(0,50);
display.println("softstart ON LEFT");
display.print("left slowdown");
oled();
delay(settime);
}
}
else if (testpwm1==0)
{
if (testpwm2<turn2) //speed up right to turn2 value
{
for (i=testpwm2; i<=turn2; i++){ //speed up left------
// analogWrite(pwm2,i);
testpwm2 = i;
display.setCursor(0,50);
display.println("softstart ON LEFT");
display.print("left SPEED UP");
oled();
delay(settime);
}
}
else if(testpwm2>turn2) // speed down right to turn2 value
{
for (i=testpwm2; i>=turn2; i--){ // slow down left-----
//analogWrite(pwm1,i);
testpwm2 = i;
display.setCursor(0,50);
display.println("softstart ON LEFT");
display.print("left SPEED DOWN");
oled();
delay(settime);
}
}
else
{
testpwm2=turn2;
display.setCursor(0,50);
display.println("softstart ON LEFT");
oled();
delay(settime);
}
}
else //left error
{
for (i=turn2; i>=0; i--){ // slow down both-----
testpwm1 = i;
testpwm2=i;
analogWrite(pwm1,i);
analogWrite(pwm2,i);
display.setCursor(0,50);
display.println("softstart ON LEFT");
display.print("left ERROR DOWN");
oled();
delay(settime);
}
}}
//---------------------------------------------------------------------------------------------------
//display oled
void oled() {
display.setCursor(0, 0);
display.print(ampsens,0);
display.print("mV ");
display.print(testpwm1);
display.print(" ");
display.print(testpwm2);
display.print(" ");
display.print( turn2);
display.setCursor(0, 10);
display.print("AMP = ");
display.print(current,4);
display.print("A");
display.setCursor(0,20);
display.print("max current set = ");
display.print(maxc);
display.setCursor(0,30);
display.print("set time = ");
display.print(settime);
display.display(); // Show initial text
}
the code for softright is copied to leftsoft. but changed all left to right and other way around also.
that is why i find it strange it does not put any info on the display.
just if arduino is stopped working al together
// softstart on/off switch c
if (c == 1){
if (setpwm1==1 &&setpwm2==0)//turn right softstart
{
softright(); //run void softright
}
else if (setpwm2==1 && setpwm1==0)//turn left softstart
{
oled();
//leftsoft(); //run void softleft
the bottom two lines are the issue.
whit oled it works correctly and with leftsoft nothing shows up on display