Skip to content

Commit b66e7d2

Browse files
Merge pull request #1 from EdernOllivier/AduinoMotorShield
Add files via upload
2 parents ac25b53 + a813de8 commit b66e7d2

File tree

1 file changed

+96
-27
lines changed

1 file changed

+96
-27
lines changed

Main.ino

Lines changed: 96 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,28 @@
77
----> http://www.adafruit.com/products/1438
88
*/
99
//#include "I2CScanner.h"
10-
#include <Wire.h>
11-
#include <Adafruit_MotorShield.h>
10+
//#include <Wire.h>
11+
//#include <Adafruit_MotorShield.h>
1212
#define Wire Wire1
1313
//I2CScanner scanner;
1414
// Create the motor shield object with the default I2C address
15-
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
15+
//Adafruit_MotorShield AFMS = Adafruit_MotorShield();
1616
// Or, create it with a different I2C address (say for stacking)
1717
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);
1818

1919
// Select which 'port' M1, M2, M3 or M4. In this case, M1
20-
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
20+
//Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
2121
// You can also make another motor on port M2
2222
//Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);
2323
// You can also make another stepper motor on port M3 and M4
2424
//Adafruit_StepperMotor *myNextMotor = AFMS.getStepper(200, 2);
2525

26+
const int
27+
PWM_A = 3,
28+
DIR_A = 12,
29+
BRAKE_A = 9,
30+
SNS_A = A0;
31+
2632
int sensorPin1 = A1; // select the input pin for the gyrometer // gyrometer wiper (middle terminal) connected to analog pin 1
2733
// outside leads to ground and +3.3V
2834
int sensorValue1 = 0; // variable to store the value coming from the sensor
@@ -201,6 +207,14 @@ void displayCalStatus(void)
201207
}
202208
*/
203209
void setup() {
210+
// Configure the A output
211+
pinMode(BRAKE_A, OUTPUT); // Brake pin on channel A
212+
pinMode(DIR_A, OUTPUT); // Direction pin on channel A
213+
214+
// Open Serial communication
215+
Serial.begin(9600); // set up Serial library at 9600 bps
216+
Serial.println("Motor shield DC motor Test:\n");
217+
/*
204218
Serial.begin(9600); // set up Serial library at 9600 bps
205219
Serial.println("Adafruit Motorshield v2 - DC Motor test!");
206220
@@ -212,7 +226,7 @@ void setup() {
212226
myMotor->run(FORWARD);
213227
// turn on motor
214228
myMotor->run(RELEASE);
215-
/*
229+
216230
// Set the speed to start, from 0 (off) to 255 (max speed)
217231
myOtherMotor->setSpeed(150);
218232
myOtherMotor->run(FORWARD);
@@ -277,6 +291,46 @@ void setup() {
277291
unsigned long lastMilli = 0;
278292

279293
void loop() {
294+
// Set the outputs to run the motor forward
295+
296+
// digitalWrite(BRAKE_A, LOW); // setting brake LOW disable motor brake
297+
// digitalWrite(DIR_A, HIGH); // setting direction to HIGH the motor will spin forward
298+
299+
// analogWrite(PWM_A, 255); // Set the speed of the motor, 255 is the maximum value
300+
301+
// delay(5000); // hold the motor at full speed for 5 seconds
302+
// Serial.print("current consumption at full speed: ");
303+
// Serial.println(analogRead(SNS_A));
304+
305+
// Brake the motor
306+
307+
// Serial.println("Start braking\n");
308+
// raising the brake pin the motor will stop faster than the stop by inertia
309+
// digitalWrite(BRAKE_A, HIGH); // raise the brake
310+
// delay(5000);
311+
312+
// Set the outputs to run the motor backward
313+
314+
// Serial.println("Backward");
315+
// digitalWrite(BRAKE_A, LOW); // setting againg the brake LOW to disable motor brake
316+
// digitalWrite(DIR_A, LOW); // now change the direction to backward setting LOW the DIR_A pin
317+
318+
// analogWrite(PWM_A, 255); // Set the speed of the motor
319+
320+
// delay(5000);
321+
// Serial.print("current consumption backward: ");
322+
// Serial.println(analogRead(SNS_A));
323+
324+
// now stop the motor by inertia, the motor will stop slower than with the brake function
325+
// analogWrite(PWM_A, 0); // turn off power to the motor
326+
327+
// Serial.print("current brake: ");
328+
// Serial.println(analogRead(A0));
329+
// Serial.println("End of the motor shield test with DC motors. Thank you!");
330+
331+
332+
// while(1);
333+
280334
// read the value from the gyrometer sensor:
281335
sensorValue1 = analogRead(sensorPin1); // - 512;
282336
Serial.println("From the gyrometer : ");
@@ -414,53 +468,68 @@ void loop() {
414468
////////////////////////////////////////////////////////////////////////////////////////////
415469

416470
// Set the speed to start, from 0 (off) to 255 (max speed)
417-
myMotor->run(FORWARD);
418-
myMotor->setSpeed(191);
471+
// myMotor->run(FORWARD);
472+
// myMotor->setSpeed(191);
419473
// turn on motor
420474
// myMotor->run(RELEASE);
421-
/*
475+
422476
#ifdef TRUE
423477
// here the stab. has been done with the inclinometer
424478
if (event.orientation.y > 0)
425479
{
426-
myMotor->run(BACKWARD);
427-
myMotor->setSpeed(event.orientation.y *2);
428-
// myNextMotor->step(4, FORWARD, MICROSTEP);
480+
// myMotor->run(BACKWARD);
481+
Serial.println("Backward");
482+
digitalWrite(BRAKE_A, LOW); // setting againg the brake LOW to disable motor brake
483+
digitalWrite(DIR_A, LOW); // now change the direction to backward setting LOW the DIR_A pin
484+
// myMotor->setSpeed(event.orientation.y *2);
485+
analogWrite(PWM_A, event.orientation.y *2);
486+
// myNextMotor->step(4, FORWARD, MICROSTEP);
429487
}
430488
else
431489
{
432-
myMotor->run(FORWARD);
433-
myMotor->setSpeed(-1 *event.orientation.y *2);
490+
// myMotor->run(FORWARD);
491+
Serial.println("forward");
492+
digitalWrite(BRAKE_A, LOW); // setting brake LOW disable motor brake
493+
digitalWrite(DIR_A, HIGH); // setting direction to HIGH the motor will spin forward
494+
// myMotor->setSpeed(-1 *event.orientation.y *2);
495+
analogWrite(PWM_A, -1 *event.orientation.y *2);
434496
// myNextMotor->step(4, BACKWARD, MICROSTEP);
435497
}
436498

437499
// here the stab. has been done with the inclinometer
438-
if (event.orientation.z > 0)
439-
{
440-
myOtherMotor->run(BACKWARD);
441-
myOtherMotor->setSpeed(event.orientation.z *2);
500+
// if (event.orientation.z > 0)
501+
// {
502+
// myOtherMotor->run(BACKWARD);
503+
// myOtherMotor->setSpeed(event.orientation.z *2);
442504
// myNextMotor->step(4, FORWARD, MICROSTEP);
443-
}
444-
else
445-
{
446-
myOtherMotor->run(FORWARD);
447-
myOtherMotor->setSpeed(-1 *event.orientation.z *2);
505+
// }
506+
// else
507+
// {
508+
// myOtherMotor->run(FORWARD);
509+
// myOtherMotor->setSpeed(-1 *event.orientation.z *2);
448510
// myNextMotor->step(4, BACKWARD, MICROSTEP);
449511
}
450512
#else
451513
// here the stab. has been done with the gyrometer
452514
if (sensorValue1 > 0)
453515
{
454-
myMotor->run(FORWARD);
455-
myMotor->setSpeed(sensorValue1);
516+
// myMotor->run(FORWARD);
517+
Serial.println("forward");
518+
digitalWrite(BRAKE_A, LOW); // setting brake LOW disable motor brake
519+
digitalWrite(DIR_A, HIGH); // setting direction to HIGH the motor will spin forward
520+
// myMotor->setSpeed(sensorValue1);
521+
analogWrite(PWM_A, 255); //sensorValue1);
456522
}
457523
else
458524
{
459-
myMotor->run(BACKWARD);
460-
myMotor->setSpeed(-1 *sensorValue1);
525+
// myMotor->run(BACKWARD);
526+
Serial.println("Backward");
527+
digitalWrite(BRAKE_A, LOW); // setting againg the brake LOW to disable motor brake
528+
digitalWrite(DIR_A, LOW); // now change the direction to backward setting LOW the DIR_A pin
529+
// myMotor->setSpeed(-1 *sensorValue1);
530+
analogWrite(PWM_A, 255); //-1 *sensorValue1);
461531
}
462532
#endif
463-
*/
464533
//#ifdef TRUE
465534
/*
466535
Serial.println("Single coil steps");

0 commit comments

Comments
 (0)