Étape 2: Programmation la MegaMoto
Ce code a une routine de motion exemple programmée pour un Conseil MegaMoto. Le code est écrit pour avoir 3 planches empilées, mais les lignes sont commentées ainsi qu’une seule carte est activée. Vous pouvez commenter et décommenter les lignes pour contrôler jusqu'à 3 boucliers.
Ce code lit le capteur épingle, mais n’utilise pas les valeurs au-delà de les imprimer pour le serial monitor.
/* Sample code for the Robot Power MegaMoto. This board can be stacked up to three times to control three motors. The code is included for each motor in the setup routines, uncomment the lines of code to enable the motors you need. The main loop of this program ramps the speed of the first board from zero to max (0-255) over 1 second, holds it at max speed for 2 seconds, then ramps back down (255-0) for 1 second. The pins swap, so the direction changes, and the loop repeats. Modified by Progressive Automations, using the original example code from: http://www.robotpower.com/downloads/ Hardware: - 1 to 3 RobotPower MegaMoto control boards - Arduino Uno Wiring: -Connect the +/- of the actuator to the A/B motor channels -Connect the +/- of the power supply to the +/- BAT connections This example code is in the public domain. */ int EnablePin1 = 8; //int EnablePin2 = 12; //int EnablePin3 = 13; int duty; //Use the jumpers on the board to select which A and B signals you want int PWMPinA1 = 11; // Timer2 int PWMPinB1 = 3; //int PWMPinA2 = 9; // Timer0 //int PWMPinB2 = 10; // int PWMPinA3 = 6; // Timer1 // int PWMPinB3 = 5; const byte CPin1 = A0; // analog input channel //const byte CPin2 = A1; // analog input channel //const byte CPin3 = A4; // analog input channel int CRaw1; // raw A/D value //int CRaw2; //int CRaw3; void setup() { Serial.begin(9600); pinMode(EnablePin1, OUTPUT); //pinMode(EnablePin2, OUTPUT); //pinMode(EnablePin3, OUTPUT);//Enable the boards pinMode(PWMPinA1, OUTPUT); pinMode(PWMPinB1, OUTPUT); //pinMode(PWMPinA2, OUTPUT); //pinMode(PWMPinB2, OUTPUT); //pinMode(PWMPinA3, OUTPUT); //pinMode(PWMPinB3, OUTPUT);//Set motor outputs }//end setup void loop() { digitalWrite(EnablePin1, HIGH); //enable the board analogWrite(PWMPinB1, 0); //Set pinB to 0, when speed is written to pinA the motor will extend for (duty = 0; duty <= 255; duty += 5) // ramp up speed { analogWrite(PWMPinA1, duty); delay(5); } analogWrite(PWMPinA1, 255);//end at max speed CRaw1 = analogRead(CPin1); Serial.println("Feedback"); Serial.print(CRaw1); delay(2000);//hold speed for (duty = 255; duty >= 0; duty -= 5) // ramp down speed { analogWrite(PWMPinA1, duty); delay(20); } analogWrite(PWMPinA1, 0); //set to 0 speed delay(500); digitalWrite(EnablePin1, LOW);// Toggle enable to reset the power chips if we have had an overcurrent or overtemp fault delay(500); // Swap pins to make the motor change direction if (PWMPinA1 == 11) { PWMPinA1 = 3; PWMPinB1 = 11; } else { PWMPinA1 = 11; PWMPinB1 = 3; } /*if (PWMPinA2 == 9) { PWMPinA2 = 10; PWMPinB2 = 9; } else { PWMPinA2 = 9; PWMPinB2 = 10; }*/ /*if(PWMPinA3 == 6) { PWMPinA3 = 5; PWMPinB3 = 6; } else { PWMPinA3 = 6; PWMPinB3 = 5; }*/ }//end main loop