Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdint.h>
- #define In1 7
- #define In2 13
- #define pwm1 12
- #define pwm2 8
- void setup() {
- // put your setup code here, to run once:
- pinMode (In1, OUTPUT);
- pinMode (In2, OUTPUT);
- pinMode (pwm1, OUTPUT);
- pinMode (pwm2, OUTPUT);
- digitalWrite (In1,LOW);
- digitalWrite (In2,LOW);
- }
- void motor_dir (bool dir) {
- if (dir) {
- digitalWrite (In1, HIGH);
- digitalWrite (In2, LOW);
- }
- if (!(dir)) {
- digitalWrite (In1, LOW);
- digitalWrite (In2, HIGH);
- }
- }
- void motor_setup (uint16_t speed_hz, uint16_t time_ms, bool dir) {
- uint16_t speed_ms = 1000/speed_hz;
- motor_dir (dir);
- uint32_t startTime = millis ();
- while (millis () - startTime < time_ms){
- if (dir){
- digitalWrite (pwm2, HIGH);
- delay (speed_ms-2);
- digitalWrite (pwm2, LOW);
- delay (speed_ms-2);
- }
- if (!(dir)){
- digitalWrite (pwm1, HIGH);
- delay (speed_ms-2);
- digitalWrite (pwm1, LOW);
- delay (speed_ms-2);
- }
- }
- }
- void loop() {
- // put your main code here, to run repeatedly:
- bool dir;
- dir = 1;
- motor_setup (2, 1000, dir);
- dir = 0;
- motor_setup (2, 1000, dir);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement