Created
October 22, 2014 11:21
-
-
Save roboter/835308a2c1b3aa6c8871 to your computer and use it in GitHub Desktop.
basic motor test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Blink | |
Turns on an LED on for one second, then off for one second, repeatedly. | |
This example code is in the public domain. | |
*/ | |
#define D1 3 // Направление вращение двигателя 1 | |
#define M1 4 // ШИМ вывод для управления двигателем 1 | |
#define D2 6 // Направление вращение двигателя 2 | |
#define M2 5 // ШИМ вывод для управления двигателем 2 | |
// Pin 13 has an LED connected on most Arduino boards. | |
// give it a name: | |
#define LED 2 | |
int pin1 = 8; | |
int pin2 = 9; | |
// the setup routine runs once when you press reset: | |
void setup() { | |
Serial.begin(9600); | |
// initialize the digital pin as an output. | |
pinMode(LED, OUTPUT); | |
pinMode(pin1, INPUT); // set pin to input | |
digitalWrite(pin1, HIGH); // turn on pullup resistors | |
pinMode(pin2, INPUT); // set pin to input | |
digitalWrite(pin2, HIGH); // turn on pullup resistors | |
pinMode(D1, OUTPUT); | |
pinMode(M1, OUTPUT); | |
pinMode(D2, OUTPUT); | |
pinMode(M2, OUTPUT); | |
} | |
int i=0; | |
// the loop routine runs over and over again forever: | |
void loop() { | |
// | |
delay(100); // wait for a second | |
// | |
// delay(1000); // wait for a second | |
if(digitalRead(pin1) == 1) | |
{ | |
Serial.println(i); | |
digitalWrite(D1, HIGH); | |
analogWrite(M1, 255 - i); | |
digitalWrite(D2, HIGH); | |
analogWrite(M2, 255 - i); | |
i++; | |
} | |
if(digitalRead(pin2) == 1) | |
{ | |
Serial.println(i); | |
digitalWrite(D1, LOW); | |
analogWrite(M1, i); | |
digitalWrite(D2, LOW); | |
analogWrite(M2, i); | |
i--; | |
} | |
if(i>255) i =255; | |
if(i<0) i=0; | |
analogWrite(LED, i); // turn the LED on (HIGH is the voltage level) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment