Control a motor with a button - Arduino Stack Exchange most recent 30 from arduino.stackexchange.com 2019-12-06T14:18:04Z https://arduino.stackexchange.com/feeds/question/35833 https://creativecommons.org/licenses/by-sa/4.0/rdf https://arduino.stackexchange.com/q/35833 1 Control a motor with a button Utsav https://arduino.stackexchange.com/users/31239 2017-03-15T04:09:02Z 2018-06-11T13:37:43Z <p>Code:</p> <pre><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 &lt; 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 &gt;= 0; i--){ //goes through each speed from 255 to 0 analogWrite(motorPin, i); //sets the new speed delay(delayTime); // waits for delayTime milliseconds } } </code></pre> <p>This code spins the motor. In the circuit, I have a button which I want to toggle the spinning of the motor. </p> <p>This is the circuit:</p> <p><a href="https://circuits.io/circuits/4267039-spin-motor-spin/edit" rel="nofollow noreferrer">https://circuits.io/circuits/4267039-spin-motor-spin/edit</a></p> <p>How would I make it so that the button toggles the spinning of the motor? like a fan and its switch?</p> https://arduino.stackexchange.com/questions/35833/-/35836#35836 1 Answer by SMFSW for Control a motor with a button SMFSW https://arduino.stackexchange.com/users/31802 2017-03-15T07:24:19Z 2017-04-14T12:17:21Z <p>add this variable at the top of your program: </p> <pre><code>const int buttonPin = XX; // the pin for the button bool bState = false; </code></pre> <p>in the setup() function:</p> <pre><code>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 </code></pre> <p>in the loop() function, change your code with this:</p> <pre><code>update_bState(); if (bState) { digitalWrite(motorPin, HIGH); } else { digitalWrite(motorPin, LOW); } </code></pre> <p>then, at the end of your program, add the getButton function:</p> <pre><code>void update_bState(void) { static bool buttonDone = false; static uint16_t timeButton; if (!digitalRead(buttonPin)) { if ((!buttonDone) &amp;&amp; ((uint16_t) (millis() - timeButton) &gt; 100)) { // Do once when button is pressed for at least 100ms (debounce) bState ^= true; // Toggle button state buttonDone = true; } } else { buttonDone = false; timeButton = millis(); } } </code></pre> <p>Now that all the work is done, you should add acceleration when launching and deceleration when launching and stopping :D</p> <p>By the way, I did not tested any code, so there may be some typo errors...</p>