Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*************************************************************
- Motor Shield Stepper Demo
- by Randy Sarafan
- For more information see:
- https://www.instructables.com/id/Arduino-Motor-Shield-Tutorial/
- *************************************************************/
- int delaylegnth = 10;
- int current_pos = 0;
- void setup() {
- //establish motor direction toggle pins
- pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
- pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???
- //establish motor brake pins
- pinMode(9, OUTPUT); //brake (disable) CH A
- pinMode(8, OUTPUT); //brake (disable) CH B
- Serial.begin(9600);
- }
- int parseIntFast(int numberOfDigits){
- /*
- This function returns the converted integral number as an int value.
- If no valid conversion could be performed, it returns zero.*/
- char theNumberString[numberOfDigits + 1];
- int theNumber;
- for (int i = 0; i < numberOfDigits; theNumberString[i++] = Serial.read()){
- delay(5);
- };
- theNumberString[numberOfDigits] = 0x00;
- theNumber = atoi(theNumberString);
- return theNumber;
- }
- void ReadSerialCommands(){ // Plot PID Values using Serial Plotter
- if (Serial.available()){
- char cmdByte = Serial.read();
- int setting;
- switch (cmdByte) {
- case 'M': // Change Proportianal Gain
- setting = parseIntFast(4);
- takeNSteps(setting);
- Serial.print("Set Throttle to ");
- Serial.println(setting);
- break;
- case 'Z': // Change Integral Gain
- current_pos = 0;
- Serial.print("Set Steering to 0");
- break;
- }
- Serial.flush();
- }
- }
- void takeNSteps(int a){
- bool go_right = a>0;
- a = abs(a);
- if (go_right) {
- while (a-- > 0){
- digitalWrite(9, LOW); //ENABLE CH A
- digitalWrite(8, HIGH); //DISABLE CH B
- digitalWrite(12, HIGH); //Sets direction of CH A
- analogWrite(3, 255); //Moves CH A
- delay(delaylegnth);
- digitalWrite(9, HIGH); //DISABLE CH A
- digitalWrite(8, LOW); //ENABLE CH B
- digitalWrite(13, LOW); //Sets direction of CH B
- analogWrite(11, 255); //Moves CH B
- delay(delaylegnth);
- digitalWrite(9, LOW); //ENABLE CH A
- digitalWrite(8, HIGH); //DISABLE CH B
- digitalWrite(12, LOW); //Sets direction of CH A
- analogWrite(3, 255); //Moves CH A
- delay(delaylegnth);
- digitalWrite(9, HIGH); //DISABLE CH A
- digitalWrite(8, LOW); //ENABLE CH B
- digitalWrite(13, HIGH); //Sets direction of CH B
- analogWrite(11, 255); //Moves CH B
- delay(delaylegnth);
- }
- } else {
- while(a-->0){
- digitalWrite(9, LOW); //ENABLE CH A
- digitalWrite(8, HIGH); //DISABLE CH B
- digitalWrite(12, HIGH); //Sets direction of CH A
- analogWrite(3, 255); //Moves CH A
- delay(delaylegnth);
- digitalWrite(9, HIGH); //DISABLE CH A
- digitalWrite(8, LOW); //ENABLE CH B
- digitalWrite(13, HIGH); //Sets direction of CH B
- analogWrite(11, 255); //Moves CH B
- delay(delaylegnth);
- digitalWrite(9, LOW); //ENABLE CH A
- digitalWrite(8, HIGH); //DISABLE CH B
- digitalWrite(12, LOW); //Sets direction of CH A
- analogWrite(3, 255); //Moves CH A
- delay(delaylegnth);
- digitalWrite(9, HIGH); //DISABLE CH A
- digitalWrite(8, LOW); //ENABLE CH B
- digitalWrite(13, LOW); //Sets direction of CH B
- analogWrite(11, 255); //Moves CH B
- delay(delaylegnth);}}}
- void loop(){
- ReadSerialCommands();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement