Вы находитесь на странице: 1из 8

//Push button int ledPin = 13; int inputPin = 3; void setup() { pinMode(ledPin, OUTPUT); pinMode(inputPin, INPUT); } void loop(){

val = digitalRead(inputPin); if (val == HIGH) { digitalWrite(ledPin, LOW); } else { digitalWrite(ledPin, HIGH); } } // choose the pin for the LED // Connect sensor to input pin 3

// declare LED as output // declare pushbutton as input

// read input value // check if the input is HIGH // turn LED OFF // turn LED ON

Motor driver

The Controller A motor controller is a device that serves to govern in some predetermined manner the performance of an electric motor. A motor controller might include a manual or automatic means for starting and stopping the motor, selecting forward or reverse rotation, regulating the speed and torque, and protecting against overloads and faults. This Dual Motor Controller is easy to use, thank's to the L298N motor driver chip. This chip allows for direct drive of two bi-directional DC motors, and incorporates high-speed short diodes for protection. Drive current up to 2A per motor output. The driver uses a broad-brush design to reduce wire resistance.

Below, an illustration of the DRI0002, an inexpensive board avalaible by DFRobot.

Each motor is controlled by two pins on the motor controller:

A pin M, (zoom the above picture, look at the motor logic) that affects the polarity on the output terminal of the corresponding motor.

A pin E (zoom the above picture, look at the motor logic) that affects speed using PWM (Pulse Width Modulation).

What is PWM ? Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of "on time" is called the pulse width. To get varying analog values, you change, or modulate, that pulse width. If you repeat this on-off pattern fast enough with an LED for example, the result is as if the signal is a steady voltage between 0 and 5v controlling the brightness of the LED.

PWM on your Arduino With the Arduino, you will have to use ''AnalogWrite()'' to Write an analog value (PWM wave) to a pin. Can be used to light a LED at varying brightnesses or drive a motor at various speeds. After a call to analogWrite(), the pin will generate a steady square wave of the specified duty cycle until the next call to analogWrite() (or a call to digitalRead() or digitalWrite() on the same pin). The frequency of the PWM signal is approximately 490 Hz. On most Arduino boards (those with the ATmega168 or ATmega328), this function works on pins 3, 5, 6, 9, 10, and 11. On the Arduino Mega, it works on pins 2 through 13. Older Arduino boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11. You do not need to call pinMode() to set the pin as an output before calling analogWrite(). The analogWrite function has nothing whatsoever to do with the analog pins or the analogRead function.

//======================================================================= // 2 wheels Robot Platform Template // www.e-shore.com.my //======================================================================= //motor control pin const int E1=5; //motor const int M1=4; //motor const int E2=6; //motor const int M2=7; //motor

1 1 2 2

(right) enable pin (right) direction pin (left) enable pin (left) direction pin

// setup function //======================================================================= void setup() { //configure all motor control pin as output pinMode(E1,OUTPUT); pinMode(M1,OUTPUT); pinMode(E2,OUTPUT); pinMode(M2,OUTPUT);

//disable both motor by default digitalWrite(E1,LOW); digitalWrite(E2,LOW); } // loop function //======================================================================= void loop() { //right wheel forward at 70 PWM speed digitalWrite(M1,LOW); analogWrite(E1,40); //left wheel forward at 70 PWM speed digitalWrite(M2,HIGH); analogWrite(E2,40); //delay for 300ms delay(300); //right wheel stop digitalWrite(E1,LOW); //left wheel stop digitalWrite(E2,LOW); //program halt while(1); }

// Initialize int PWM1 = 6; // PWM Pin Motor 1 int PoM1 = 7; // Polarity Pin Motor 1 int PWM2 = 5; // PWM Pin Motor 2 int PoM2 = 4; // Polarity Pin Motor 2

int ValM1 = 0; // Initial Value for PWM Motor 1 int ValM2 = 0; // Initial Value for PWM Motor 2

int i = 25;

// increment

boolean goUp = true ; // Used to detect acceleration or deceleration

void setup() { pinMode(PWM1, OUTPUT); pinMode(PoM1, OUTPUT); pinMode(PWM2, OUTPUT); pinMode(PoM2, OUTPUT); digitalWrite(PoM1, LOW) ; // Both motor with same polarity digitalWrite(PoM2, LOW) ; analogWrite(PWM1, ValM1); // Stop both motors => ValMx = 0 analogWrite(PWM2, ValM2); Serial.begin(9600); } // Used to check value

// Main program void loop() { delay (500) ; // give some time to the motor to adapt to new value

if ((ValM1 < 250) && goUp) // First phase of acceleration { ValM1 = ValM1 + i ; ValM2 = ValM2 + i ; } // increase PWM value => Acceleration

else { goUp = false ; ValM1 = ValM1 - i ; ValM2 = ValM2 - i ; if (ValM1 < 75) { ValM1 = 0 ; ValM2 = 0 ; goUp = true ; } } if ((ValM1 > 75) && (ValM1< 255)) // If PWM values are OK, I send to motor controller { analogWrite(PWM1, ValM1); analogWrite(PWM2, ValM2); } Serial.print(ValM1); Serial.print("\t"); Serial.println(ValM2); } // End. // Debug. Print Value Motor 1 // Print tab // Print Value Motor 2 to Serial // deceleration completed // My motor made fanzy noise below 70 // Acceleration completed // Decrease PWM => deceleration

// One below 75, I set to 0 = STOP

Вам также может понравиться