Go Down

Topic: problem with array code for servo (Read 465 times) previous topic - next topic

ingi

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.

Code: [Select]



#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]);
}
  }
 

James C4S

Is there a question in your post?
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Hasan999


Is there a question in your post?

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:
Code: [Select]
int (movex[i] = (fmovex)); - don't you mean
Code: [Select]
fmovex = movex[i]; ??

Another slight problem I see is:
Code: [Select]
(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.


johnwasser

This is my best guess of what you want to do.

Code: [Select]

#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]);
            }
        }
    }
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Go Up