Here is Arduino Motor PWM code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
//By Roee Bloch //Arduino Motor PWM, in this example I am changing motor speed and directions //All right Reserved // Copyright (c) 2015 All Right Reserved, http://www.electronics-freak.com // // This source is subject to the Roee Bloch License. // Please see the License.txt file for more information. // All other rights reserved. // // THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A // PARTICULAR PURPOSE. // // </copyright> // <author>Roee Bloch</author> // <email>roeebloch@walla.co.il</email> // <date>June 2015</date> int M1 = 3; // Motor 1 this is connected to PWMR int M2 = 5; // Motor 1this is connected to PWML // Also connect 2 enables pins to 5V (enable) int val = 0; // variable to store the read value void setup() { pinMode(M1, OUTPUT); // sets the pin as output pinMode(M2, OUTPUT); // sets the pin as output pinMode(8, OUTPUT); // sets the pin as output pinMode(9, OUTPUT); // sets the pin as output digitalWrite(8, HIGH); // enable Motor M1 digitalWrite(9, HIGH); // enable Motor M1 } void loop() { for (int i = 1; i < 255; i++) { analogWrite(3, i); analogWrite(5, 0); delay(50); } delay(200); for (int i = 1; i < 255; i++) { analogWrite(5, i); analogWrite(3, 0); delay(50); } } |