Turret Project

I need help setting up 5 positions and loop it 3 times, at every position the servo has to fire once.
I use 2 potentiometers and 2 DC 24V motors.

Kind regards.

I/O

volatile unsigned int potentiometer_value = 0;
volatile unsigned int potentiometer_value2 = 0;
#include <Servo.h>
Servo myservo;

const char TAB = '\t';
int pos3 = 0;
int pos4 = 180;

int MotorCW = 7;
int MotorCCW = 8;
int MotorUP = 10;
int MotorDOWN = 11;

int potentiometer_pin = A2;
int potentiometer_pin2 = A5;   
                  
int pulse_value = 1; 
int pulse_value2 = 0; 

int Start_button= 4;
int Stop_button= 5;
int Reset_button= 6;


void setup() {
Serial.begin (9600);

pinMode(MotorCW, OUTPUT);
pinMode(MotorCCW, OUTPUT);
pinMode(MotorUP, OUTPUT);
pinMode(MotorDOWN, OUTPUT);

pinMode(Start_button, INPUT_PULLUP);
pinMode(Reset_button, INPUT_PULLUP);

myservo.attach(9);
 
  }

Relays

void setPositionX(int pos1){
    if(pos1 <= potentiometer_value2){
        digitalWrite(MotorCW, HIGH);
       digitalWrite(MotorCCW, LOW);
       //Serial.println("Turn Motor Clockwise");
       
    } else if (pos1 >= potentiometer_value2){
        digitalWrite(MotorCCW, HIGH);
       digitalWrite(MotorCW, LOW);
        //Serial.println("Turn Motor Counter Clockwise");
    } else {
        return;
    }



   while (pos1 == potentiometer_value2){
     digitalWrite(MotorCW, LOW);
     digitalWrite(MotorCCW, LOW);
     delay(2000);

   }
   {
    return;
   }

 
}

  void setPositionY(int pos2){
    if(pos2 <= potentiometer_value){
        digitalWrite(MotorUP, HIGH); 
       digitalWrite(MotorDOWN, LOW);
       //Serial.println("Turn Motor Up");
       
    } else if (pos2 >= potentiometer_value){
        digitalWrite(MotorUP, LOW);
       digitalWrite(MotorDOWN, HIGH);
       //Serial.println("Turn Motor Down");
    } else {
        return;
    }



    while (pos2 == potentiometer_value){
     digitalWrite(MotorUP, LOW);
     digitalWrite(MotorDOWN, LOW);

    }
    {
      return;
    }
    
   
   
  }

Loop

void loop() 
    {

  {

  Serial.println (potentiometer_value2);
  Serial.print (TAB);
  Serial.print (TAB);
  Serial.print (TAB);
  Serial.print (TAB);
  Serial.println (potentiometer_value);
  }

  {
    potentiometer_value = analogRead(potentiometer_pin); 
  potentiometer_value2 = analogRead(potentiometer_pin2); 
  pulse_value = potentiometer_value ; 
  pulse_value2 = potentiometer_value2; 
  }
    
   else if (potentiometer_value2 == 632)
    {
 setPositionY(580);
  setPositionX(600);
  Serial.println ("Start");
  
  }
  else if(potentiometer_value2 == 580 && potentiometer_value == 600){
    setPositionX(400);
    setPositionY(550);
  //}


 

  
    }}

paintball.ino (736 Bytes)

Relay.ino (1.15 KB)

Void_Loop.ino (697 Bytes)

I need help setting up 5 positions and loop it 3 times

it? What the heck is it that you want to loop?

at every position the servo has to fire once.

At every position of what? Servos move. They don't "fire".

   while (pos1 == potentiometer_value2){
     digitalWrite(MotorCW, LOW);
     digitalWrite(MotorCCW, LOW);
     delay(2000);

   }

Setting pins LOW over and over accomplishes nothing more than setting them LOW once.
The delay() is useless.

So that should be:

     digitalWrite(MotorCW, LOW);
     digitalWrite(MotorCCW, LOW);

     while (pos1 == potentiometer_value2)
     {
     }

And now, it is real obvious that if pos1 ever does equal potentiometer_value2, you are f**ked.

If your servo goes from 0 - 180 and back then you have some code that you haven’t posted. There’s nothing in those odd code snippets that ever moves a servo in any way. Please post a COMPLETE program and details of what it does and what you need it to do.

It’s a lot easier to help if you provide something that we can help with.

Steve