I'm currently trying to write some code to control a toy tower crane.
I'm using a SainSmart Nano 3.0 (currently testing on a Uno v3) for the controller, and an ADAFRUIT Motor control shield.
The Crane has 3 x 6v DC motors (thankfully 3 caps on each motor already) 1 motor to rotate the crane, another to move the hook trolly back and forth, and the third to raise and lower the hook.
I have a bit of code written so far. Most the set up, though it seems quite a lot, to me at least (this is my first project outside of the "Getting Started with Arduino" by Massimo Banzi book).
I am trying to use the analog pins as digitalRead pins, I think I have it worked out correctly, but i'm not sure.
I have the "first" control completed. Using the example for turning a LED on and off from the "Getting Started" book page 49, and modifying it for two different "states"
Hopefully Some one can review the code and let me know if it will work, if I did something wrong, and maybe ways to improve upon it.
/*
My Attempt at controlling a crane with Arduino
Using analog pins 0 - 5 as input
pin 0 will rotate the crane CCW
pin 1 will rotate the crane CW
pin 2 will move the trolly out
pin 3 will move the trolly in
pin 4 will raise the hook
pin 5 will lower the hook
Motor Control is accomplished with the ADAFRUIT
Motor Control Shield
Motor 1 will control rotation
Motor 2 will control the Trolly
Motor 3 will control the hook
*/#include <AFMotor.h>
// Set up the motors - name and channel
AF_DCMotor rotate(1, MOTOR12_64KHZ); // create motor #1 named rotate, 64KHz pwm
AF_DCMotor trolly(2, MOTOR12_64KHZ); // create motor #2 named trolly, 64KHz pwm
AF_DCMotor hook(3, MOTOR12_1KHZ); // create motor #2 named hook, 1KHz pwm// Constants defining buttons (not sure if this is or will be needed)
// Variables to help debounce the buttons
// but# is the read value
// bou# is the debounce variable
// mov# is the movement of the motor
// 0 = stopped (Released)
// 1 = moveint but0 = 0;
int bou0 = 0;
int mov0 = 0;
int but1 = 0;
int bou1 = 0;
int mov1 = 0;
int but2 = 0;
int bou2 = 0;
int mov2 = 0;
int but3 = 0;
int bou3 = 0;
int mov3 = 0;
int but4 = 0;
int bou4 = 0;
int mov4 = 0;
int but5 = 0;
int bou5 = 0;
int mov5 = 0;void setup() {
Serial.begin(9600);
Serial.println("Crane test!");
rotate.setSpeed(255);
trolly.setSpeed(255); // Motor speed 0-255
hook.setSpeed(255);
}void loop() {
// Read the button inputs
but0 = digitalRead(A0);
but1 = digitalRead(A1);
but2 = digitalRead(A2);
but3 = digitalRead(A3);
but4 = digitalRead(A4);
but5 = digitalRead(A5);//Check if there was a transition on button 0 rotate CCW
if ((but0 == HIGH) && (bou0 == LOW) && (bou1 == LOW)) {
mov0 = 1 - mov0;
delay(10);
}
bou0 = but0;//Check if there was a transition on button 1 rotate CW
if ((but1 == HIGH) && (bou1 == LOW) && (bou0 == LOW)) {
mov0 = 1 - mov0;
delay(10);
}
bou1 = but1;if ((mov0 == 1) && (mov1 == 0)) {
rotate.run(FORWARD); // rotate the crane CCW
} if ((mov1 == 1) && (mov0 == 0)) {
rotate.run(BACKWARD); // rotate the crane CW
} else {
rotate.run(RELEASE); // Stop crane rotation
}}
Thank You in advance