[ad_1]
i made a circuit that management motor velocity and point out the motor velocity ranges with leds, the push button management the velocity degree and leds
//********************************************^************************************************
// https://discussion board.arduino.cc/t/control-motor-speed-with-npn-and-push-button-no-driver/1184925
//
//
//
// Model YY/MM/DD Feedback
// ======= ======== ====================================================================
// 1.00 20/23/02 Working code
// 1.10 20/23/03 Added motor and LEDs OFF if change pressed for >= 3 sec.
//
//
//
#outline PRESSED HIGH
#outline RELEASED LOW
#outline ENABLED true
#outline DISABLED false
#outline LEDon HIGH
#outline LEDoff LOW
#outline MOTORoff 0
//GPIOs
const byte Button = 2;
const byte ledpin = 3;
const byte ledpin1 = 4;
const byte ledpin2 = 5;
const byte Motor = 9;
const byte heartbeatLED = 13;
//Variables
byte lastButton = RELEASED;
byte currSwitch;
int Speed_Level;
bool bCheckingSwitch = DISABLED;
//timing stuff
unsigned lengthy heartbeatTime;
unsigned lengthy switchesTime;
unsigned lengthy threeSecondTime;
// s e t u p ( )
//********************************************^************************************************
void setup()
{
pinMode(Button, INPUT);
pinMode(ledpin, OUTPUT);
pinMode(ledpin1, OUTPUT);
pinMode(ledpin2, OUTPUT);
pinMode(Motor, OUTPUT);
pinMode(heartbeatLED, OUTPUT);
} //END of setup()
// l o o p ( )
//********************************************^************************************************
void loop()
{
//************************************************ T I M E R heartbeatLED
//is it time to toggle the heartbeat LED ?
if (millis() - heartbeatTime >= 500ul)
{
//restart this TIMER
heartbeatTime = millis();
//toggle the heartbeat LED
if (digitalRead(heartbeatLED) == HIGH) digitalWrite(heartbeatLED, LOW);
else digitalWrite(heartbeatLED, HIGH);
}
//************************************************ T I M E R examine switches
//is it time to examine our switches ?
if (millis() - switchesTime >= 50ul)
{
//restart this TIMER
switchesTime = millis();
checkSwitches();
}
//************************************************ T I M E R three seconds
//if enabled, is it time to show issues OFF ?
if (bCheckingSwitch == ENABLED && millis() - threeSecondTime >= 3000ul)
{
//we're completed with this TIMER
bCheckingSwitch = DISABLED;
//restart the sequence
Speed_Level = -1;
analogWrite(Motor, MOTORoff);
digitalWrite(ledpin, LEDoff);
digitalWrite(ledpin1, LEDoff);
digitalWrite(ledpin2, LEDoff);
}
} //END of loop()
// c h e c okay S w i t c h e s ( )
//********************************************^************************************************
void checkSwitches()
{
byte state;
//************************************************ Button
state = digitalRead(Button);
//has there been a state change within the change ?
if (lastButton != state)
{
//replace to the brand new state
lastButton = state;
//*******************************
if (state == PRESSED)
{
//allow the TIMER
bCheckingSwitch = ENABLED;
//begin the TIMER
threeSecondTime = millis();
}
//*******************
//the change was launched
else
{
//disable the TIMER
bCheckingSwitch = DISABLED;
Speed_Level++;
//do not go over 3
if (Speed_Level >= 4)
{
Speed_Level = 0;
}
//*******************
if (Speed_Level == 1)
{
analogWrite(Motor, 50);
digitalWrite(ledpin, LEDon);
digitalWrite(ledpin1, LEDoff);
digitalWrite(ledpin2, LEDoff);
}
//*******************
else if (Speed_Level == 2)
{
analogWrite(Motor, 75);
digitalWrite(ledpin, LEDoff);
digitalWrite(ledpin1, LEDon);
digitalWrite(ledpin2, LEDoff);
}
//*******************
else if (Speed_Level == 3)
{
analogWrite(Motor, 100);
digitalWrite(ledpin, LEDoff);
digitalWrite(ledpin1, LEDoff);
digitalWrite(ledpin2, LEDon);
}
//*******************
else
{
analogWrite(Motor, MOTORoff);
digitalWrite(ledpin, LEDoff);
digitalWrite(ledpin1, LEDoff);
digitalWrite(ledpin2, LEDoff);
}
}
}
}//END of checkSwitches()
//********************************************^************************************************
now i wish to make battery low voltage indicator so the battery led blinking in crimson when the battery decrease than 680
and this battery led flip on and off with the degrees leds so if
motor and leds are working = the battery led blinking
motor and leds cease = the leds stops
i attempted this code however it not working in any respect simply blinking the battery led and no motor management
//********************************************^************************************************
// https://discussion board.arduino.cc/t/control-motor-speed-with-npn-and-push-button-no-driver/1184925
//
//
//
// Model YY/MM/DD Feedback
// ======= ======== ====================================================================
// 1.00 20/23/02 Working code
// 1.10 20/23/03 Added motor and LEDs OFF if change pressed for >= 3 sec.
//
//
//
#outline PRESSED HIGH
#outline RELEASED LOW
#outline ENABLED true
#outline DISABLED false
#outline LEDon HIGH
#outline LEDoff LOW
#outline MOTORoff 0
#outline Purple 6
#outline lipo A0
float lipoV = 0;
//GPIOs
const byte Button = 2;
const byte ledpin = 3;
const byte ledpin1 = 4;
const byte ledpin2 = 5;
const byte Motor = 9;
const byte heartbeatLED = 13;
//Variables
byte lastButton = RELEASED;
byte currSwitch;
int Speed_Level;
bool bCheckingSwitch = DISABLED;
//timing stuff
unsigned lengthy heartbeatTime;
unsigned lengthy switchesTime;
unsigned lengthy threeSecondTime;
// s e t u p ( )
//********************************************^************************************************
void setup()
{
pinMode(Button, INPUT);
pinMode(ledpin, OUTPUT);
pinMode(ledpin1, OUTPUT);
pinMode(ledpin2, OUTPUT);
pinMode(Motor, OUTPUT);
pinMode(heartbeatLED, OUTPUT);
pinMode(Purple,OUTPUT);
pinMode(lipo,INPUT);
} //END of setup()
// l o o p ( )
//********************************************^************************************************
void loop()
{
//************************************************ T I M E R heartbeatLED
lipoV = analogRead(lipo);
Serial.println(lipoV);
digitalWrite(Purple, HIGH); // flip the LED on (HIGH is the voltage degree)
delay(1000); // look ahead to a second
digitalWrite(Purple, LOW); // flip the LED off by making the voltage LOW
delay(1000);
//is it time to toggle the heartbeat LED ?
if (millis() - heartbeatTime >= 500ul)
{
//restart this TIMER
heartbeatTime = millis();
//toggle the heartbeat LED
if (digitalRead(heartbeatLED) == HIGH) digitalWrite(heartbeatLED, LOW);
else digitalWrite(heartbeatLED, HIGH);
}
//************************************************ T I M E R examine switches
//is it time to examine our switches ?
if (millis() - switchesTime >= 50ul)
{
//restart this TIMER
switchesTime = millis();
checkSwitches();
}
//************************************************ T I M E R three seconds
//if enabled, is it time to show issues OFF ?
if (bCheckingSwitch == ENABLED && millis() - threeSecondTime >= 2000ul)
{
//we're completed with this TIMER
bCheckingSwitch = DISABLED;
//restart the sequence
Speed_Level = -1;
analogWrite(Motor, MOTORoff);
digitalWrite(ledpin, LEDoff);
digitalWrite(ledpin1, LEDoff);
digitalWrite(ledpin2, LEDoff);
}
} //END of loop()
// c h e c okay S w i t c h e s ( )
//********************************************^************************************************
void checkSwitches()
{
byte state;
//************************************************ Button
state = digitalRead(Button);
//has there been a state change within the change ?
if (lastButton != state)
{
//replace to the brand new state
lastButton = state;
//*******************************
if (state == PRESSED)
{
//allow the TIMER
bCheckingSwitch = ENABLED;
//begin the TIMER
threeSecondTime = millis();
}
//*******************
//the change was launched
else
{
//disable the TIMER
bCheckingSwitch = DISABLED;
Speed_Level++;
//do not go over 3
if (Speed_Level >= 4)
{
Speed_Level = 0;
}
//*******************
if (Speed_Level == 1)
{
analogWrite(Motor, 50);
digitalWrite(ledpin, LEDon);
digitalWrite(ledpin1, LEDoff);
digitalWrite(ledpin2, LEDoff);
}
//*******************
else if (Speed_Level == 2)
{
analogWrite(Motor, 75);
digitalWrite(ledpin, LEDoff);
digitalWrite(ledpin1, LEDon);
digitalWrite(ledpin2, LEDoff);
}
//*******************
else if (Speed_Level == 3)
{
analogWrite(Motor, 100);
digitalWrite(ledpin, LEDoff);
digitalWrite(ledpin1, LEDoff);
digitalWrite(ledpin2, LEDon);
}
//*******************
else
{
analogWrite(Motor, MOTORoff);
digitalWrite(ledpin, LEDoff);
digitalWrite(ledpin1, LEDoff);
digitalWrite(ledpin2, LEDoff);
digitalWrite(Purple,LOW);
}
if(lipoV < 680){
digitalWrite(Purple,HIGH);
}
else
{
digitalWrite(Purple,LOW);
}
}
}
}//END of checkSwitches()
//********************************************^************************************************
[ad_2]