Galileo code for the DC motor shield

Now that the HW is in place, bring up the IDE, make sure that the proper port and device are selected, and enter the following code:

Galileo code for the DC motor shield

The code is straightforward. It consists of the following three blocks:

  1. The declaration of the six variables that connect to the proper Galileo pins:
    int pwmA = 3;
    int pwmB = 11;
    int brakeA = 9;
    int brakeB = 8;
    int directionA = 12;
    int directionB = 13;
  2. The setup() function, which sets the directionA, directionB, brakeA, and brakeB digital output pins:
    pinMode(directionA, OUTPUT);
    pinMode(brakeA, OUTPUT);
    pinMode(directionB, OUTPUT);
    pinMode(brakeB, OUTPUT);
  3. The loop() function. This is an example of how to make the wheeled robot go forward, then turn to the right. At each of these steps, you use the brake to stop the robot:
    // Move Forward
    digitalWrite(directionA, HIGH);
    digitalWrite(brakeA, LOW);
    analogWrite(pwmA, 255);
    digitalWrite(directionB, HIGH);
    digitalWrite(brakeB, LOW);
    analogWrite(pwmB, 255);
    delay(2000);
    digitalWrite(brakeA, HIGH);
    digitalWrite(brakeB, HIGH);
    delay(1000);
    //Turn Right
    digitalWrite(directionA, LOW); //Establishes backward direction of Channel A
    digitalWrite(brakeA, LOW); //Disengage the Brake for Channel A
    analogWrite(pwmA, 128); //Spins the motor on Channel A at half speed
    digitalWrite(directionB, HIGH); //Establishes forward direction of Channel B
    digitalWrite(brakeB, LOW); //Disengage the Brake for Channel B
    analogWrite(pwmB, 128); //Spins the motor on Channel B at full speed
    delay(2000);
    digitalWrite(brakeA, HIGH);
    digitalWrite(brakeB, HIGH);
    delay(1000);

Once you have uploaded the code, the program should run in a loop. If you want to run your robot without connecting to the computer, you'll need to add a battery to power the Galileo. The Galileo will need at least 2 Amps, but you might want to consider providing 3 Amps or more based on your project. To supply this from a battery, you can use one of several different choices. My personal favorite is to use an emergency cell phone charging battery, like this:

Galileo code for the DC motor shield

If you are going to use this, you'll need a USB-to-2.1 mm DC plug cable, available at most online stores. Once you have uploaded the code, you can disconnect the computer, then press the reset button. Your robot can move all by itself!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.200.242