Hello! I am looking for a little guidance on code for a 3 motor laser show. I have roughed it together and wired it up without the arduino and it is cool! I would like a little more control over the motors than the jumpiness I get with pots varying the motor rpms. Since the three motors are powered from the same p/s (robbed from a nokia cell charger), each motor affects the speed of the others.
I have a adafruit motor shield too and am at a loss as to initial setup with the code.
I would also like to consider varying the speed of the motors via ir remote, and have no idea how to implement that either.
Can anyone suggest a sample code or starting point with a pot for each motor or even the ir route?
Thanks for the reply!!
The motor shield running sample code allows the motors to run MUCH smoother than just using a breadboard and pots.
I imagine the caps and such on the motorshield is a sufficient buffer.
I am sorry but have a very limited understanding of coding. I have more familiarity with electronics and hardware than software. So could you help me understand the code you suggested?
I am trying to learn in a “trial by fire” method.
Do you know from experience if
const int motorPin [N_MOTORS] = [ 3, 5, 6};
corresponds to the motorshield outputs for M1, M2, and M3?
I found this info about motor shield.
Digital pin 11: DC Motor #1 / Stepper #1 (activation/speed control)
Digital pin 3: DC Motor #2 / Stepper #1 (activation/speed control)
Digital pin 5: DC Motor #3 / Stepper #2 (activation/speed control)
Digital pin 6: DC Motor #4 / Stepper #2 (activation/speed control)
would then
const int motorPin [N_MOTORS] = [ 11, 3, 5};
make your code work for motor 1, 2, and 3??
Also how would I implement a random speed in lieu of a manual control?
Does that require if statement and/ or a button or state of the three pots?
Thanks so much for teaching me without making me feel like a total noob!
-e
I thank you for helping all! I HAVE MADE SOLID PROGRESS.
#include <AFMotor.h>
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
int potpin1 = 8;
int potpin2 = 9;
int potpin3 = 10; // analog pin used to connect the potentiometer
int val1;
int val2;
int val3; // variable to read the value from the analog pin
void setup()
{ Serial.begin(9600);
}
void loop()
{
val1 = analogRead(potpin1); // reads the value of the potentiometer (value between 0 and 1023)
val1 = map(val1, 0, 1023, 0, 255);
motor1.setSpeed(val1); // sets the motor speed according to the scaled value
motor1.run(FORWARD);
delay(15);
val2 = analogRead(potpin2); // reads the value of the potentiometer (value between 0 and 1023)
val2 = map(val2, 0, 1023, 0, 255);
motor2.setSpeed(val2); // sets the motor speed according to the scaled value
motor2.run(FORWARD);
delay(15);
val3 = analogRead(potpin3); // reads the value of the potentiometer (value between 0 and 1023)
val3 = map(val3, 0, 1023, 0, 255);
motor3.setSpeed(val3); // sets the motor speed according to the scaled value
motor3.run(FORWARD);
delay(15);
}
It works. Now for random control with “snapshot” function…
I mean to not move, to cease to rotate, "sleep mode"...
I initially thought it would make sense to initiate a random mode for all three motors by looking for a value of zero at each of the three pots. After a little thought, it makes more sense in this case for it to initiate the random mode if all pots are set to max.
It is almost pointless in this case to have all three motors spinning at max (not very exciting to look at), but having all three motors stop spinning is useful for me.
When the pots are at max resistance the motors should NOT SPIN. While, as I said before, all three at full on does not need to ever happen for this project. So I now think having all three values at 255 would be a viable cue to initiate a random mode.
If I could accomplish that, and temporally keep each motor spinning at their random speed while all three pots are at max, that would rock!
It seems that if that works, then moving any of the three away from and then back to max, would generate a new random "scene".
I can simply turn all three pots to zero and I get "sleep mode" (no rotation for quiet, saving battery life...). That said I don't need an if statement or anything to initiate 0,0,0. I can figure that out. What I am currently trying to figure out is a way to manually select a "scene" of three random speeds for the motors. (possibly one random number for each val1, val2, and val3)
The fact that all three motors/ pots at max is not useful to me right now, just seemed like a way to cue my code to insert random values.
Initially I was thinking the 0,0,0 state would be a good cue but after a little thought 255, 255, 255 is the way to go.
I have learned a lot from your posts so thank you!
So I wanted to share my “progress”. I have at this point 90% functionality but have just one question. I now have a “random scene mode” (when all 3 pots are at max) that changes the image every 7 seconds. And I am able to go back to “manual mode” by moving any pot away from max.
The 7 second delay that holds the random image, however, means I don’t have manual control until 7 seconds after moving pot away from max.
Is there an easy fix to ensure the device returns to manual mode sooner while leaving the 7 second “hold” of a random scene?
Here is my code:
#include <AFMotor.h>
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
int potpin1 = 8;
int potpin2 = 9;
int potpin3 = 10; // analog pin used to connect the potentiometer
int i;
int val1;
int val2;
int val3; // variable to read the value from the analog pin
void setup()
{
//Serial.begin(9600);
}
void loop()
{
{
val1 = analogRead(potpin1); // reads the value of the potentiometer (value between 0 and 1023)
val1 = map(val1, 0, 1023, 0, 255);
motor1.setSpeed(val1); // sets the motor speed according to the scaled value
motor1.run(FORWARD);
val2 = analogRead(potpin2); // reads the value of the potentiometer (value between 0 and 1023)
val2 = map(val2, 0, 1023, 0, 255);
motor2.setSpeed(val2); // sets the motor speed according to the scaled value
motor2.run(FORWARD);
val3 = analogRead(potpin3); // reads the value of the potentiometer (value between 0 and 1023)
val3 = map(val3, 0, 1023, 0, 255);
motor3.setSpeed(val3); // sets the motor speed according to the scaled value
motor3.run(FORWARD);
if (val1 == 255 && val2 == 255 && val3 == 255)
{
motor1.setSpeed(random(0, 255));
motor1.run(FORWARD);
motor2.setSpeed(random(0, 255));
motor2.run(FORWARD);
motor3.setSpeed(random(0, 255));
motor3.run(FORWARD);
delay(7000);
}
delay(15);
//Serial.println (val1);
//Serial.println (val2);
//Serial.println (val3);
}}
E3po:
Is there an easy fix to ensure the device returns to manual mode sooner while leaving the 7 second "hold" of a random scene?
I am always unsure whether to gve the quick answer, or just to tell about the approach as it (usually) is fu working it out yourself.
So the "hint" this time is - the much famed and abused "blink with out delay" - because you must not use delay if you want to do two things at once. (in this case, (1)monitor the pot value and (2)count to 7 seconds before doing another random)