Hello, I have posted few questions about my code im working on and still I need help. I have this code where I want to controle two servo motors with array index. I want to be able to controle the time ther servo motor takes to go from one index nr to the other. I post the code here.
#include <Servo.h>
Servo servox;
Servo servoy;
int movex[] = {16, 48, 135, 180};
int movey[] = {10, 20, 30, 40};
int de[] = {10, 200, 4000, 80000};
int fmovex = 0;
void setup()
{
Serial.begin(9600);
servox.attach(10);
servoy.attach(9);
}
void loop() {
for(int i=0; i<4; i=i+1){
int (movex[i] = (fmovex));
if (movex[i]<movey[i+1]);fmovex++; //here I would like the fmovex to "count down" from array index 1 to 2, and then 2 to 3 and so on. But it begins //not on 16 but on 0 and then down to 180 or where the rotation comes to its end. Does this make any sence?
Serial.println (fmovex);
servox.write(fmovex);
delay (de[1]);
}
}
He has actually - within his code, under his comment !
Umm, I don't really understand what exactly do you want to do.. but:
fmovex gives 0 makes sense because you started with: int fmovex = 0; (and it didn't change then)
I don't understand this line: int (movex[i] = (fmovex)); - don't you mean fmovex = movex[i]; ??
Another slight problem I see is: (movex[i]<movey[i+1]) ...when it will reach the last index of movex, there is no i+1 index of movey because both arrays have '4' indices.
Please explain what do you 'want' to do... for example, you want fmovex to be 16, and then 17, and so on, until 48.. etc.
#include <Servo.h>
Servo servox;
Servo servoy;
int movex[] = {16, 48, 135, 180};
int movey[] = {10, 20, 30, 40};
int de[] = {10, 200, 4000, 80000};
int fmovex = 0;
int currentx = 0, currenty = 0;
void setup()
{
Serial.begin(9600);
servox.attach(10);
servoy.attach(9);
}
void loop()
{
if (forex < 4)
{
if (currentx == movex[forex] && currenty == movey[forex]) // reached destination
forex++; // going on to the next destination
else
{
// We're not at the current destintion yet so we take a step
if (currentx < movex[forex])
currentx++;
else
if (currentx > movex[forex])
currentx--;
if (currenty < movey[forex])
currenty++;
else
if (currenty > movey[forex])
currenty--;
servox.write(currentx);
servoy.write(currenty);
delay(de[forex]);
}
}
}