| |
| Examples |
|
/* PWM Motor advanced * by DojoCorp <http://www.0j0.org> * * Demonstrates the use of analog output pins (PWM) * controlling a motor. * * Description of th pinout used by the motor control board: * - pwm pin 1 controls the speed on the A side of the H bridge * - pwm pin 2 controls the speed on the B side of the H bridge * - digital pin 8 controls the direction for motor B * - digital pin 9 controls the direction for motor A * * Created 19 March 2005 */ int value = 0; // variable to keep the actual value int inA = 38; // reads the pin 38 as a pushbutton (pushbutt 1) int inB = 39; // reads the pin 39 as a pushbutton (pushbutt 2) int pwmpinA = 1; // motor A connected to analog pin 1 int pwmpinB = 2; // motor B connected to analog pin 2 int dirpinA = 9; // motor A direction pin int dirpinB = 8; // motor B direction pin boolean directionA = true; // toggling direction for motor A boolean directionB = true; // toggling direction for motor B int ledpin = 48; // show the led on if dirA = HIGH, off otherwise void setup() {
pinMode(inA, INPUT);
pinMode(inB, INPUT);
pinMode(ledpin, OUTPUT);
} void loop() {
if (digitalRead(inA) == HIGH) {
digitalWrite(dirpinA,HIGH);
digitalWrite(ledpin,HIGH);
} else {
digitalWrite(dirpinA,LOW);
digitalWrite(ledpin,LOW);
}
if (directionB) {
digitalWrite(dirpinB,HIGH);
} else {
digitalWrite(dirpinB,LOW);
}
directionB = !directionB;
// fade in (from min to max)
for(value = 0 ; value <= 255; value+=5) {
analogWrite(pwmpinA, value); // sets the value (range from 0 to 255)
analogWrite(pwmpinB, value); // sets the value (range from 0 to 255)
delay(30); // waits for 30 milli seconds to see the dimming effect
}
// fade out (from max to min)
for(value = 255; value >=0; value-=5) {
analogWrite(pwmpinA, value);
analogWrite(pwmpinB, value);
delay(30);
}
}
|
|
|
| Description |
|
This program is a variation of Example 1. It shows that the very same principle can be used to speed up and slow down a motor through using the motor board.
This program assumes that there are two different motors attached to the board. The one at connector-set A spins all the time in the same direction, while the one plugged at the B-port toggles direction each time the loop is repeated.
The board counts with two pushbuttons, in this example we are monitoring the use of the one labeled as S1, when pressed it activates the on-board LED (pin 48 of the i/o board), showing that the motor at connector-set A will toggle direction. This example shows how the analog pins are mapped twice on the wiring board, this may be relevant for designers to know, in order not to count with certain pins in the board if they are used as analog outputs. |
 |
|
|
| Syntax |
|
Please refer to: analogWrite(pin, value) and digitalWrite(pin, value) at the reference
|
 |
|
|
| Parameters |
|
| pwmpinA |
|
int, for the i/o board is always analog pin nr. 1. We will select different values to assign to this pin, possible values go between 0 and 255
|
|
| |
|
| pwmpinB |
|
int, for the i/o board is always analog pin nr. 2. We will select different values to assign to this pin, possible values go between 0 and 255
|
|
| |
|
| dirpinA |
|
int, for the i/o board is always digital pin nr. 9. Determines whether the motor placed at connector-set A should spin in one or the other direction. Possible values are TRUE and FALSE
|
|
| |
|
| dirpinB |
|
int, for the i/o board is always digital pin nr. 8. Determines whether the motor placed at connector-set B should spin in one or the other direction. Possible values are TRUE and FALSE
|
|
| |
|
| inA |
|
int, attached to pin nr. 38 of the i/o board, this corresponds to S1, or pushbutton nr. 1 on the motor board
|
|
| |
|
| inB |
|
int, attached to pin nr. 39 of the i/o board, this corresponds to S2, or pushbutton nr. 2 on the motor board
|
|
 |
|
|
|