Code:
int motorPin = 9; // the pin the motor is connected to
void setup()
{
pinMode(motorPin, OUTPUT);
}
void loop()
{
motorOnThenOff(); //motorAcceleration
}
void motorOnThenOff(){
int onTime = 2500; //the number of milliseconds for the motor to turn on for
int offTime = 1000; //the number of milliseconds for the motor to turn off for
digitalWrite(motorPin, HIGH); // turns the motor On
delay(onTime); // waits for onTime milliseconds
digitalWrite(motorPin, LOW); // turns the motor Off
delay(offTime); // waits for offTime milliseconds
}
void motorOnThenOffWithSpeed(){
int onSpeed = 200; // a number between 0 (stopped) and 255 (full speed)
int onTime = 2500; //the number of milliseconds for the motor to turn on for
int offSpeed = 50; // a number between 0 (stopped) and 255 (full speed)
int offTime = 1000; //the number of milliseconds for the motor to turn off for
analogWrite(motorPin, onSpeed); // turns the motor On
delay(onTime); // waits for onTime milliseconds
analogWrite(motorPin, offSpeed); // turns the motor Off
delay(offTime); // waits for offTime milliseconds
}
void motorAcceleration(){
int delayTime = 50; //milliseconds between each speed step
//Accelerates the motor
for(int i = 0; i < 256; i++){ //goes through each speed from 0 to 255
analogWrite(motorPin, i); //sets the new speed
delay(delayTime); // waits for delayTime milliseconds
}
//Decelerates the motor
for(int i = 255; i >= 0; i--){ //goes through each speed from 255 to 0
analogWrite(motorPin, i); //sets the new speed
delay(delayTime); // waits for delayTime milliseconds
}
}
This code spins the motor. In the circuit, I have a button which I want to toggle the spinning of the motor.
This is the circuit:
https://circuits.io/circuits/4267039-spin-motor-spin/edit
How would I make it so that the button toggles the spinning of the motor? like a fan and its switch?
1 Answer 1
add this variable at the top of your program:
const int buttonPin = XX; // the pin for the button
bool bState = false;
in the setup() function:
pinMode(buttonPin, INPUT_PULLUP);
// in case INPUT_PULLUP is not defined, comment the above line and uncomment below
//pinMode(buttonPin, INPUT);
//digitalWrite (buttonPin, HIGH); // enable internal pull-up
in the loop() function, change your code with this:
update_bState();
if (bState) { digitalWrite(motorPin, HIGH); }
else { digitalWrite(motorPin, LOW); }
then, at the end of your program, add the getButton function:
void update_bState(void)
{
static bool buttonDone = false;
static uint16_t timeButton;
if (!digitalRead(buttonPin))
{
if ((!buttonDone) && ((uint16_t) (millis() - timeButton) > 100))
{ // Do once when button is pressed for at least 100ms (debounce)
bState ^= true; // Toggle button state
buttonDone = true;
}
}
else
{
buttonDone = false;
timeButton = millis();
}
}
Now that all the work is done, you should add acceleration when launching and deceleration when launching and stopping :D
By the way, I did not tested any code, so there may be some typo errors...
-
Yes I do, first line I tell to add in the loopSMFSW– SMFSW2017年03月15日 16:11:18 +00:00Commented Mar 15, 2017 at 16:11
-
I beg your pardon, so you doMark Smith– Mark Smith2017年03月15日 16:20:44 +00:00Commented Mar 15, 2017 at 16:20
-
No problem. I checked, I could have forgotten something.SMFSW– SMFSW2017年03月15日 16:23:59 +00:00Commented Mar 15, 2017 at 16:23
-
1@Muhammad Did you notice that the button is initialized as INPUT_PULLUP? This enables internal microcontroller pull-up resistor for the pin, thus avoiding the use of external pull for the button.SMFSW– SMFSW2017年04月14日 11:03:00 +00:00Commented Apr 14, 2017 at 11:03
-
1@Muhammad depending your board definitions or arduino version, INPUT_PULLUP may not be defined. In this case, pinMode(buttonPin, INPUT); digitalWrite(buttonPin, HIGH); should work. Writing a 1 to an input declared pin turns the internal pull-up on.SMFSW– SMFSW2017年04月14日 11:39:26 +00:00Commented Apr 14, 2017 at 11:39