Stap 7: Verkeer van Trolley Robot
Broncode voor het verkeer van Trolley robot
void runFunction(){ Wire.begin(); // join i2c bus (address optional for master) Serial.println("sent DC speed 100"); MotorDirectionSet(0b0101); //"0b1010" defines the output polarity, "10" means the M+ is "positive" while the M- is "negtive" // make sure M+ and M- is different polatity when driving DC motors. MotorSpeedSetAB(100,100);//defines the speed of motor 1 and motor 2; //0b0101 Rotating in the opposite direction }//Function to set the 2 DC motor speed //motorSpeedA : the DC motor A speed; should be 0~100; //motorSpeedB: the DC motor B speed; should be 0~100;void MotorSpeedSetAB(unsigned char MotorSpeedA , unsigned char MotorSpeedB) { MotorSpeedA=map(MotorSpeedA,0,100,0,255); MotorSpeedB=map(MotorSpeedB,0,100,0,255); Wire.beginTransmission(I2CMotorDriverAdd); // transmit to device I2CMotorDriverAdd Wire.write(MotorSpeedSet); // set pwm header Wire.write(MotorSpeedA); // send pwma Wire.write(MotorSpeedB); // send pwmb Wire.endTransmission(); // stop transmitting } //set the prescale frequency of PWM, 0x03 default; void MotorPWMFrequenceSet(unsigned char Frequence) { Wire.beginTransmission(I2CMotorDriverAdd); // transmit to device I2CMotorDriverAdd Wire.write(PWMFrequenceSet); // set frequence header Wire.write(Frequence); // send frequence Wire.write(Nothing); // need to send this byte as the third byte(no meaning) Wire.endTransmission(); // stop transmitting } //set the direction of DC motor. void MotorDirectionSet(unsigned char Direction) { // Adjust the direction of the motors 0b0000 I4 I3 I2 I1 Wire.beginTransmission(I2CMotorDriverAdd); // transmit to device I2CMotorDriverAdd Wire.write(DirectionSet); // Direction control header Wire.write(Direction); // send direction control information Wire.write(Nothing); // need to send this byte as the third byte(no meaning) Wire.endTransmission(); // stop transmitting }void MotorDriectionAndSpeedSet(unsigned char Direction,unsigned char MotorSpeedA,unsigned char MotorSpeedB) { //you can adjust the driection and speed together MotorDirectionSet(Direction); MotorSpeedSetAB(MotorSpeedA,MotorSpeedB); }