This code set to move 2 servos at pin 1 & 2 randomly; and two motors at pin 9 & 10 each motor spins for 1 second, then stops for a random time of 0.5-3 second... please tell me
1- how to slow down the two motors at pin 9 n 10 make them move at half and quarter speed..
2- how do i make the 2 random servos move independently from each other? they seem to move randomly but together.....
Many thanx for reading.
#include <Servo.h>
Servo servo1, servo2;
int pos = 0; // variable to store the servo position
int MotPin1 = 9; // select pin 9 for Motor1
int MotPin2 = 10; // select pin 10 for Motor2
void setup()
{
pinMode(MotPin1, OUTPUT); // MotPin1 should be output
pinMode(MotPin2, OUTPUT); // MotPin2 should be output
}
void loop()
{
digitalWrite(MotPin1, 1); // Motor1 ON
delay(1000);
digitalWrite(MotPin1, 0); // Motor1 OFF
delay(random(500,3000)); // delay for random time 0.5 - 3 sec
digitalWrite(MotPin2, 1); // Motor2 ON
delay(1000);
digitalWrite(MotPin2, 0); // Motor2 OFF
delay(random(500,3000)); // delay for random time 0.5 - 3 sec
int time = random(0, 5000);
delay(time);
int position1 = random(60, 120);
int position2 = random(30, 90);
servo1.attach(2); // attaches the servo on pin 2 to the servo object
servo2.attach(3); // attaches the servo on pin 3 to the servo object
The motor speed is controlled by an analogWrite rather than the digitalWrite you are using.
You didn't show your circuit: hopefully you're not driving the motors directly from the Arduino pins but have some transistors in there or an h-bridge driver chip? And don't forget the fly-back diode.
Regarding the servos, don't forget they take a while to get to their destination, so the way you coded it, the second servo gets its command to move before the first one is finished. So you could put a delay between the two servo writes.
Thanks for commenting guys...
for the analogwrite..not sure how yet..if anyone can please give a line or something ?
on the servo: if I add delay 50ms as shown below would that work?
servo1.attach(2); // attaches the servo on pin 2 to the servo object
servo2.attach(3); // attaches the servo on pin 3 to the servo object
Goldenshuttle:
if I add delay 50ms as shown below would that work?
It will work in that the code will compile and execute and do what you have told it to do. You have not said what you want the code to do so it is not possible to determine if the code will do what you want it to. The best way to get help is to show all of your code and say exactly what it is doing different from what you intended. Then someone can point out where your code doesn't match your intentions.
Cross-posting means posting the same question more than once in different parts of the forum, in the hope of getting answers more quickly.
This wastes time of other forum members.
Don't do it.
Goldenshuttle:
.i do not fully yet understand how this forum works.
That is why we places a "How to use this forum" sticky post at the start of each section so you can't miss it. Did you think it did not apply to you? Well the news is that it does.
Please read it.
yea sure..also if u have seen this question elswhere on the forum..please tell me and i will delete my post...it is also good not to clog the forum with unnecessary repeated topics.
Goldenshuttle:
yea sure..also if u have seen this question elswhere on the forum..please tell me and i will delete my post...it is also good not to clog the forum with unnecessary repeated topics.
It's already in the recycle bin, to which you have no access, so you can't delete it.
OK; here we are, to reply to one of the coleags comment: this is a haloween prop, all servos and motors move the ears, nose, and eyeballs in a random way..
now I read analog write and modified the sketch...this way...any comments I would be grateful...
#include <Servo.h>
Servo servo1, servo2;
int pos = 0; // variable to store the servo position
int MotPin1 = 9; // select pin 9 for Motor1
int MotPin2 = 10; // select pin 10 for Motor2
void setup()
{
pinMode(MotPin1, OUTPUT); // MotPin1 should be output
pinMode(MotPin2, OUTPUT); // MotPin2 should be output
}
void loop()
{
analogWrite(MotPin1, 150); // Motor1 Run in half speed
delay(1000);
analogWrite(MotPin1, 0); // Motor1 OFF
delay(random(500,3000)); // delay for random time 0.5 - 3 sec
analogWrite(MotPin2, 150); // Motor2 run half speed
delay(1000);
analogWrite(MotPin2, 0); // Motor2 OFF
delay(random(500,3000)); // delay for random time 0.5 - 3 sec
int time = random(0, 5000);
delay(time);
int position1 = random(60, 120);
int position2 = random(30, 90);
servo1.attach(2); // attaches the servo on pin 2 to the servo object
servo2.attach(3); // attaches the servo on pin 3 to the servo object
delay(15);
servo1.write(position1);
delay(50);
servo2.write(position2);
delay(50);
}
click the MODIFY button in the upper right of the post window.
Highlight all you code.
click the "#" CODE TAGS button on the toolbar above just to the left of the QUOTE button.
click SAVE (at the bottom).
When you post code on the forum, please make a habit of using the code tags "#" button.
Goldenshuttle:
OK; here we are, to reply to one of the coleags comment: this is a haloween prop, all servos and motors move the ears, nose, and eyeballs in a random way..
now I read analog write and modified the sketch...this way...any comments I would be grateful... #include <Servo.h>
Servo servo1, servo2;
int pos = 0; // variable to store the servo position
int MotPin1 = 9; // select pin 9 for Motor1
int MotPin2 = 10; // select pin 10 for Motor2
void setup()
{
pinMode(MotPin1, OUTPUT); // MotPin1 should be output
pinMode(MotPin2, OUTPUT); // MotPin2 should be output
}
void loop()
{
analogWrite(MotPin1, 150); // Motor1 Run in half speed
delay(1000);
analogWrite(MotPin1, 0); // Motor1 OFF
delay(random(500,3000)); // delay for random time 0.5 - 3 sec
analogWrite(MotPin2, 150); // Motor2 run half speed
delay(1000);
analogWrite(MotPin2, 0); // Motor2 OFF
delay(random(500,3000)); // delay for random time 0.5 - 3 sec
int time = random(0, 5000);
delay(time);
int position1 = random(60, 120);
int position2 = random(30, 90);
servo1.attach(2); // attaches the servo on pin 2 to the servo object
servo2.attach(3); // attaches the servo on pin 3 to the servo object
#include <Servo.h>
Servo servo1, servo2;
int MotPin1 = 9; // select pin 9 for Motor1
int MotPin2 = 10; // select pin 10 for Motor2
void setup()
{
pinMode(MotPin1, OUTPUT);
pinMode(MotPin2, OUTPUT);
servo1.attach(2); // attaches the servo on pin 2 to the servo object
servo2.attach(3); // attaches the servo on pin 3 to the servo object
}
void loop()
{
// Run Motor 1 for one second at half speed
analogWrite(MotPin1, 150);
delay(1000);
analogWrite(MotPin1, 0);
// delay for random time 0.5 - 3 sec
delay(random(500,3000));
// Run Motor 2 for one second
analogWrite(MotPin2, 150); // Motor2 run half speed
delay(1000);
analogWrite(MotPin2, 0); // Motor2 OFF
// delay for random time 0.5 - 8 sec
delay(random(500,3000));
delay(random(0, 5000));
// Move the servos to new random positions
servo1.write(random(60, 120));
servo2.write(random(30, 90));
}
If your sketch is not doing exactly what you want it to do, please describe, in detail, the differences between what it is doing and what you want it to do.
Grumpy_Mike:
You know I am starting to think that Goldenshuttle dosn't want any help at all and is just jerking us about.
Sorry that u got this impression...but i wud not waste a chance to learn from you all the experienced members here...the thing is, that all these buttons are confusing, but i m sure will get there...
I loaded the sketch, servo1 moved as expected, but the motors did not...so I have to check transistors here is the schematic
johnwasser:
If your sketch is not doing exactly what you want it to do, please describe, in detail, the differences between what it is doing and what you want it to do.