Examples - ExampleMotor - DC Motor

DC Motor

This Example shows how to setup a DC-Motor successfully. To run it, you'll need a H-Brige with 2 amperes





// Const
static int digitalButtonPin[] = {24, 25};
static int analogPotiPin = 0;
static int analogOutPin[] = {4, 5};
static boolean enableSerial = true;




// Setup

void setup()
{

        // LED (2 * blink)
        pinMode(48, OUTPUT);
        digitalWrite(48, HIGH);
        delay(200);
        digitalWrite(48, LOW);
        delay(200);
        digitalWrite(48, HIGH);


        // Serial (Print to serial if enableSerial = true)
        if (enableSerial == true) Serial.begin(9600);


        // Pin Mode
        pinMode(digitalButtonPin[0], INPUT);
        pinMode(digitalButtonPin[1], INPUT);

}




// Loop

void loop()
{

        // Read Poti and Buttons
        int analogVal = analogRead(analogPotiPin);
        int val[] = {0, 0};
        if (digitalRead(digitalButtonPin[0]) == LOW)
        val[0] = analogVal;
        if (digitalRead(digitalButtonPin[1]) == LOW)
        val[1] = analogVal;


        // Move
        analogWrite(analogOutPin[0], val[0]);
        analogWrite(analogOutPin[1], val[1]);


        // Debug
        if (enableSerial == true) {

                Serial.print("     Poti: ");
                Serial.print(analogVal);
                Serial.print("     Button 1: ");
                Serial.print(val[0]);
                Serial.print("     Button 2: ");
                Serial.print(val[1]);
                Serial.println("");

        }

}