Jevois and stepper motors

I am using an Arduino UNO to detect an object using the jevois camera and based on the object’s position, I am moving stepper motors to desired coordinates. How can I move the motors to the coordinates? Attached is the code I used, and it would not move my motors in any direction. I think it is because the coordinates fluctuate too much for the motors to move, but I am not sure.

#include <avr/io.h>
#include <util/delay.h>

#define LEDPIN 13

#define SERIAL Serial

#define INLEN 256
char instr[INLEN + 1];
int smDirectionPin = 2; //Direction pin
int smStepPin = 3; //Stepper pin
int dir;
int centerx = -211;
int centery = 475;
int targx;
int targy;
int movement;

void setup()
{
delay(8000);
digitalWrite(LEDPIN, HIGH); // turn LED on
delay(500);
digitalWrite(LEDPIN, LOW); // turn LED off
delay(200);
digitalWrite(LEDPIN, HIGH); // turn LED on
delay(500);
digitalWrite(LEDPIN, LOW); // turn LED off

SERIAL.begin(115200);
SERIAL.setTimeout(1000); //* changed

pinMode(LEDPIN, OUTPUT);
// digitalWrite(LEDPIN, HIGH);

// set JeVois to detect objects with tensorflow easy: YUYV 320 308 30.0 YUYV 320 240 30.0 JeVois TensorFlowEasy
SERIAL.println(“setpar serout Hard”);
SERIAL.println(“setmapping2 YUYV 320 254 60.0 JeVois ObjectTracker”);
SERIAL.println(“streamon”);

pinMode(smDirectionPin, OUTPUT);
pinMode(smStepPin, OUTPUT);
}

void loop()
{
for(int j = 0; j < 5; j++){
if (j == 1){
byte len = SERIAL.readBytesUntil(’\n’, instr, INLEN);
instr[len–] = ‘\0’;

while (len >= 0 && instr[len] == ’ ’ || instr[len] == ‘\r’ || instr[len] == ‘\n’) instr[len–] = ‘\0’;

char * tok = strtok(instr, " \r\n");
int state = 0; targx = 0, targy = 0;
while (tok)
{
switch (state)
{
case 0:
if (strcmp(tok, “T2”) == 0) state = 1;
break;
case 1: targx = atoi(tok); state = 2; break;
case 2:
targy = atoi(tok);
if(strcmp(tok, “OK”) == 0) state = 3;
else state = 1000;
break;
default: break; // Skip any additional tokens
}
tok = strtok(0, " \r\n");
}
if (targx != 0 && targy != 0){
int x = targx;
int y = targy;
SERIAL.print("# “);
SERIAL.print(x);
SERIAL.print(” “);
SERIAL.println(y);
}
digitalWrite(LEDPIN, HIGH); // turn LED on
SERIAL.print(”# ");
SERIAL.println(instr);
//Move stricker based on y coordinate
if (centery > targy){
//move clockwise
movement = abs(targy - centery)*13;
digitalWrite(smDirectionPin, HIGH);
for (int i = 0; i < movement; i++){
digitalWrite(smStepPin, HIGH);
delayMicroseconds(70);
digitalWrite(smStepPin, LOW);
delayMicroseconds(70);
}
//set dir to 0
dir = 0;
}
else if (centery < targy){
//move counterclockwise
movement = abs(targy - centery)*13;
digitalWrite(smDirectionPin, LOW);
for (int i = 0; i < movement; i++){
digitalWrite(smStepPin, HIGH);
delayMicroseconds(70);
digitalWrite(smStepPin, LOW);
delayMicroseconds(70);
}
//set dir to 1
dir = 1;
}
}
}
// write stricker coord and move to center
if( dir == 0){
//move counterclockwise
digitalWrite(smDirectionPin, HIGH);
for (int i = 0; i < movement; i++){
digitalWrite(smStepPin, HIGH);
delayMicroseconds(70);
digitalWrite(smStepPin, LOW);
delayMicroseconds(70);
}
}
else if(dir == 1){
//move clockwise
digitalWrite(smDirectionPin, HIGH);
for (int i = 0; i < movement; i++){
digitalWrite(smStepPin, HIGH);
delayMicroseconds(70);
digitalWrite(smStepPin, LOW);
delayMicroseconds(70);
}
}
}

To make it easy for people to help you please modify your post and use the code button </>
codeButton.png

so your code 
looks like this

and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to my text editor.

Have you written a short program to make your motors move, and does that work properly?

If you need more help please post a link to the datasheet for your stepper motors and tell us what stepper motor driver you are using.

...R
Stepper Motor Basics
Simple Stepper Code

I am able to move the motor individually, but I am having trouble moving the motor to the desired coordinates. Below is a picture of the connections between motor, camera, and Arduino, I am using an EasyDriver as my motor driver.

#include <avr/io.h>
#include <util/delay.h>

#define LEDPIN 13
 

#define SERIAL Serial
 
#define INLEN 256
char instr[INLEN + 1];
int smDirectionPin = 2; //Direction pin
int smStepPin = 3; //Stepper pin
int dir;
int centerx = -195;
int centery = 475; 
int targx;
int targy;
int movement; 
int x;
int y; 
void setup()
{
delay(8000);
digitalWrite(LEDPIN, HIGH); // turn LED on
delay(500);
digitalWrite(LEDPIN, LOW); // turn LED off
delay(200);
digitalWrite(LEDPIN, HIGH); // turn LED on
delay(500);
digitalWrite(LEDPIN, LOW); // turn LED off
 
SERIAL.begin(115200);
SERIAL.setTimeout(1500); //* changed
 
pinMode(LEDPIN, OUTPUT);
// digitalWrite(LEDPIN, HIGH);
 
// set JeVois to detect objects with tensorflow easy: YUYV 320 308 30.0 YUYV 320 240 30.0 JeVois TensorFlowEasy
SERIAL.println("setpar serout Hard");
SERIAL.println("setmapping2 YUYV 320 254 60.0 JeVois ObjectTracker");
SERIAL.println("streamon");


pinMode(smDirectionPin, OUTPUT);
pinMode(smStepPin, OUTPUT);
}
 
 
void loop()
{
  for(int j = 0; j < 5; j++){  
    if (j == 0){
      byte len = SERIAL.readBytesUntil('\n', instr, INLEN);
      instr[len--] = '\0';
      while (len >= 0 && instr[len] == ' ' || instr[len] == '\r' || instr[len] == '\n') instr[len--] = '\0';
      char * tok = strtok(instr, " \r\n");
      int state = 0; targx = 0, targy = 0;
      while (tok)
      {
        switch (state)
        {
          case 0:
            if (strcmp(tok, "T2") == 0) state = 1;
            break;
          case 1: targx = atoi(tok); state = 2; break;
          case 2: 
            targy = atoi(tok);
            //if(strcmp(tok, "OK") == 0) state = 3;
            //state = 1000;
            break;
          default: break; // Skip any additional tokens
        }
      tok = strtok(0, " \r\n");
    }
    if (targx != 0 && targy != 0){
    x = targx;
    y = targy;
    SERIAL.print("# ");
    SERIAL.print(x);
    SERIAL.print(" ");
    SERIAL.println(y);
    
    digitalWrite(LEDPIN, HIGH); // turn LED on
    //Move stricker based on x coordinate
    if (x > centerx){
      //move clockwise
      if(x > 270){
        x = 270;
      }
       movement = abs(x - centerx)*13;
       if(movement > 5500){
          movement = 5500;
       }
       if( y > 150){
       digitalWrite(smDirectionPin, HIGH);
       for (int i = 0; i < movement; i++){
        digitalWrite(smStepPin, HIGH);
        delayMicroseconds(70);
        digitalWrite(smStepPin, LOW);
        delayMicroseconds(70);
       }
        //set dir to 0
        dir = 0;
      }
    }
    else if (x < centerx){
      //move counterclockwise
      if(x < -700){
        x = -700;
      }
       movement = abs(x - centerx)*13;
       if(movement > 5500){
          movement = 5500;
       }
       if (y > 150){
       digitalWrite(smDirectionPin, LOW);
       for (int i = 0; i < movement; i++){
        digitalWrite(smStepPin, HIGH);
        delayMicroseconds(70);
        digitalWrite(smStepPin, LOW);
        delayMicroseconds(70);
        }
      //set dir to 1
      dir = 1; 
      }
     }
    }  
   } 
  
  // write stricker coord and move to center
  if(j == 1){
    if(dir == 0){
    //move counterclockwise
      digitalWrite(smDirectionPin, HIGH);
      for (int i = 0; i < movement; i++){
        digitalWrite(smStepPin, HIGH);
        delayMicroseconds(70);
        digitalWrite(smStepPin, LOW);
        delayMicroseconds(70);
      }
    }
    else if(dir == 1){
    //move clockwise
      digitalWrite(smDirectionPin, HIGH);
      for (int i = 0; i < movement; i++){
        digitalWrite(smStepPin, HIGH);
        delayMicroseconds(70);
        digitalWrite(smStepPin, LOW);
        delayMicroseconds(70);
        }
      }
    }  
  }
}

Image from Reply #2 so we don't have to download it. See this Simple Image Posting Guide

...R

I've had a look at your code but I'm afraid I can't follow it. You have all the code in loop(). For anything other than a trivial program it is much better to break the code into functions - for example your code in loop() might be something like

void loop() {
   checkCameraData();
   calculateMotorPosition();
   moveMotor();
}

Have a look at Planning and Implementing a Program

Also be sure to use the AutoFormat tool to indent your code consistently - that's especially necessary when you have a lot of code in a single function.

...R