Pages: [1] 2   Go Down
Author Topic: 3 motor laser spirograph thingy  (Read 1467 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 92
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
-e
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 92
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Also I wanted to mention, I am using a Mega 2560 so there are plenty of available pins not used or obscured by motor shield.

Thanks!!
-e
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 239
Posts: 24373
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Since the three motors are powered from the same p/s (robbed from a nokia cell charger),
A charger provides maybe 500mA - what do the motors draw?
I'd beef-up the supply first.
How are you buffering the motors?

Quote
Can anyone suggest a sample code or starting point with a pot for each motor

Code:
const int N_MOTORS = 3;
const int motorPin [N_MOTORS] = [ 3, 5, 6};
const int analogPin [N_MOTORS] = { 0, 1, 2};

void setup (){}

void loop ()
{
  for (int i = 0; i < N_MOTORS; ++i) {
    analogWrite (motorPin [i], analogRead (analogPin [i] / 4));
  }
}
« Last Edit: June 19, 2011, 02:20:25 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 92
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 239
Posts: 24373
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry - I missed the bit about using a shield for the buffering (I was concerned you were driving the motors directly from output pins on the Arduino)

Quote
would then
const int motorPin [N_MOTORS] = [ 11, 3, 5};
make your code work for motor 1, 2, and 3??
From what you've posted about the shield, yes, I think it should (I don't have a motor shield)

Quote
Also how would I implement a random speed in lieu of a manual control?
Start here:
http://arduino.cc/en/Reference/Random

Quote
Does that require if statement and/ or a button or state of the three pots?
Your choice.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 92
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I thank you for helping all! I HAVE MADE SOLID PROGRESS.

Quote
    #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...
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 92
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Could anyone suggest a way to use a button connected to another pin to enter a random state

or maybe...

if val1, val2, val3  all equal 0
then { motor1.setSpeed(random 0, 255)
motor2.setSpeed(random 0, 255)
motor3.setSpeed(random 0, 255)}

how would I write that into the code?

Am I even close?

Thanks
-e

Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 92
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried to include at the end of the loop

 if (val1, val2, val3 = 0);
      motor1.setSpeed(random(0, 255));
      motor2.setSpeed(random(0, 255));
      motor3.setSpeed(random(0, 255));

but then I cannot ever stop all motors.

i also tried to change the if statement to if (val1 = 255, val2 = 255, val3 = 255)
and it is even more unpredictable. 

I have the http://arduino.cc/en/Reference/If  and guess I do not understand it fully

Please help.
Thanks
-e
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 239
Posts: 24373
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
if (val1, val2, val3 = 0);
So many things wrong.
1) lose the semicolon.
2) comparison is "==" not "="
3)You need to combine conditions with logical AND "&&"

Code:
if (val1 == 0 && val2 == 0 &&  val3 == 0)

Quote
also tried to change the if statement to if (val1 = 255, val2 = 255, val3 = 255)
and it is even more unpredictable. 
I'd say that's about as hard-and-fast as it is possible to get
« Last Edit: June 20, 2011, 02:28:42 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 92
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried this

     if (val1 == 255 && val2 == 255 && val3 == 255) {
      motor1.setSpeed(random(0, 255));
      motor2.setSpeed(random(0, 255));
      motor2.setSpeed(random(0, 255));}

so if all were at zero all motors would stop

but now no random actions happen

BTW much thanks for helping me!
-e
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 239
Posts: 24373
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I tried this

     if (val1 == 255 && val2 == 255 && val3 == 255) {
      motor1.setSpeed(random(0, 255));
      motor2.setSpeed(random(0, 255));
      motor2.setSpeed(random(0, 255));}

so if all were at zero all motors would stop

Define "stop".
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 92
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


[/quote]

Define "stop".
[/quote]

Sure

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".

If that makes sense, that is my goal.

Thanks
-e
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 239
Posts: 24373
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I mean to not move, to cease to rotate, "sleep mode"...
So why are you setting the speed to some random value and not, say, zero?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 92
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks AWOL!

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! 

Thanks
-e
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 92
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
   
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);
      }}

You guys have been really helpful!
Thanks!!
-e
Logged

Pages: [1] 2   Go Up
Jump to: