hi,
i'm trying to write code for (now) 2 motors, but will be expanding it to 6. i'm only using 1 direction hence InBx and 1 PWM.
int InB1 = 1;
int PWM1 = 3;
int InB2 = 2;
int PWM2 = 5;
void setup() {
Serial.begin(9600);
pinMode(InB1, OUTPUT);
pinMode(PWM1, OUTPUT);
pinMode(InB2, OUTPUT);
pinMode(PWM2, OUTPUT);
}
void loop() {
for (int PWM1_val, PWM2_val = 0; PWM1_val, PWM2_val < 120; PWM1_val++, PWM2_val++){ // increases from 0 to max speed
digitalWrite(InB1, HIGH);
digitalWrite(InB2, HIGH);
analogWrite(PWM1, PWM1_val);
analogWrite(PWM2, PWM2_val);
delay (100); // increase this number for a slower ramp
}
for (int PWM1_val, PWM2_val = 120; PWM1_val, PWM2_val > 60; PWM1_val--, PWM2_val--){
digitalWrite(InB1, HIGH);
digitalWrite(InB2, HIGH);
analogWrite(PWM1, PWM1_val);
analogWrite(PWM2, PWM2_val);
delay (120); // increase this number for a slower ramp
}
for (int PWM1_val, PWM2_val = 60; PWM1_val, PWM2_val < 100; PWM1_val++, PWM2_val++){
digitalWrite(InB1, HIGH);
digitalWrite(InB2, HIGH);
analogWrite(PWM1, PWM1_val);
analogWrite(PWM2, PWM2_val);
delay (10); // increase this number for a slower ramp
}
for (int PWM1_val, PWM2_val = 100; PWM1_val, PWM2_val > 50; PWM1_val--, PWM2_val--){
digitalWrite(InB1, HIGH);
digitalWrite(InB2, HIGH);
analogWrite(PWM1, PWM1_val);
analogWrite(PWM2, PWM2_val);
delay (200); // increase this number for a slower ramp
}
}
-
This is what i have written so far. First how do I write the code for starting in sync for all the motors (now only 2 but later total of 6)?
-
Is there any better way of writing the above code?
-
I would need to at some point let the motor run at a certain speed. Do i write it this way? and then to drop down to say 80 (PWM) and stay at 80 for 5 sec?
for (int PWM1_val = 120;){
digitalWrite(InB1, HIGH);
analogWrite(PWM1, PWM1_val);
delay(2000);
for (int PWM1_val = 120; PWM1_val > 80; PWM1_val--, PWM2_val--){
digitalWrite(InB1, HIGH);
analogWrite(PWM1, PWM1_val);
delay(50);
for (int PWM1_val = 80;){
digitalWrite(InB1, HIGH);
analogWrite(PWM1, PWM1_val);
delay(5000);
is this right?
- How do i write the code for say Motor1, PWM from 60 to 100 in 50ms and staying at 100 for 2 secs and Motor2, PWM from 50 to 80 in 200ms and staying at 80 for 10 secs? Both motors starting at the same time?
thanks!
p.s. i can't check if the codes work fine, cause there no internet at where i'm working. So i'm writing and testing later.