[ad_1]
I’ve an Arduino venture to manage a motor’s velocity at 3 ranges, indicated by 3 LEDs, so degree 1 is velocity 1 and LED 1, and so forth for ranges 2/3.
Additionally, I added a low-battery voltage indicator to watch the battery degree.
I’m going to make use of an ATtiny45, which has 8 pins(Vcc, GND, 1 analog pin, 5 digital pins). The venture wants 6 digital pins and 1 analog pin, so just one extra pin is required.
I don’t need to use an expander or I2C, as they are going to make the venture extra advanced, and it’s only one pin!
I can use a bigger microcontroller however it will be the final selection.
Right here is my venture’s diagram and code:
//********************************************^************************************************
// 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 Operating 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 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;
const byte Pink = 6;
//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(lipo, INPUT);
pinMode(ledpin, OUTPUT);
pinMode(ledpin1, OUTPUT);
pinMode(ledpin2, OUTPUT);
pinMode(Motor, OUTPUT);
pinMode(heartbeatLED, OUTPUT);
pinMode(Pink, 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 verify switches
//is it time to verify 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);
digitalWrite(Pink, 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++;
lipoV = analogRead(lipo);
//battery checker
if(lipoV < 680){
digitalWrite(Pink,LEDon);
}
else
{
digitalWrite(Pink,LEDoff);
}
//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(Pink,LEDoff);
}
}
}
}//END of checkSwitches()
//********************************************^************************************************
Can I take advantage of tA0 as enter and output on the similar time like this?
Or can I cut back the LED pins, that means use 1 pins for two LEDs?
[ad_2]