Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- char commanddata[2];
- byte speeddata;
- unsigned long time;
- unsigned long c1timer;
- int pwm_a = 6; //PWM control for motor outputs 1 and 2 is on digital pin 3
- int pwm_b = 9; //PWM control for motor outputs 3 and 4 is on digital pin 11
- int dir_a = 7; //direction control for motor outputs 1 and 2 is on digital pin 12
- int dir_b = 8; //direction control for motor outputs 3 and 4 is on digital pin 13
- void setup()
- {
- // initialize the serial communication:
- Serial.begin(9600);
- Serial.setTimeout(3);
- pinMode(pwm_a, OUTPUT); //Set control pins to be outputs
- pinMode(pwm_b, OUTPUT);
- pinMode(dir_a, OUTPUT);
- pinMode(dir_b, OUTPUT);
- analogWrite(pwm_a, 0); //set both motors to run at (100/255 = 39)% duty cycle (slow)
- analogWrite(pwm_b, 0);
- }
- void loop() {
- byte command;
- byte relaycommand;
- // check if data has been sent from the computer:
- if(Serial.readBytes(commanddata,2)){
- // read the most recent byte (which will be from 0 to 255):
- command = commanddata[0];
- speeddata = commanddata[1];
- switch (command) {
- case 90: // get software version 5a
- Serial.write(15);
- Serial.write(1);
- break;
- case 91: // get relay states 5b
- //Serial.write(50);
- break;
- case 93: // get relay states 5b
- Serial.write(50);
- break;
- case 100: // motorA forward
- digitalWrite(dir_a, LOW); //Set motor direction, 1 low, 2 high
- analogWrite(pwm_a, speeddata);
- c1timer=millis();
- break;
- case 101: // motorB forward
- digitalWrite(dir_b, LOW); //Set motor direction, 3 high, 4 low
- analogWrite(pwm_b, speeddata);
- c1timer=millis();
- break;
- case 102: // motorA backward
- digitalWrite(dir_a, HIGH); //Reverse motor direction, 1 high, 2 low
- analogWrite(pwm_a, speeddata);
- c1timer=millis();
- break;
- case 103: // motorB backward
- digitalWrite(dir_b, HIGH); //Reverse motor direction, 3 low, 4 high
- analogWrite(pwm_b, speeddata);
- c1timer=millis();
- break;
- }
- }
- time = millis();
- if((time-c1timer)>500){
- analogWrite(pwm_a, 0); //set both motors to run at (100/255 = 39)% duty cycle (slow)
- analogWrite(pwm_b, 0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement