Pages: [1]   Go Down
Author Topic: problem with array code for servo  (Read 344 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:


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

Austin, TX
Offline Offline
Faraday Member
**
Karma: 64
Posts: 6055
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Is there a question in your post?
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Newcastle, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 179
Posts: 8070
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Pages: [1]   Go Up
Jump to: