Joystick/ 4switch please help me! v2

hello help me please I have a project but I block a lot. the project consists of the creation of a joystick with 4 switches to activate two motors as for a tank if the switch to move forward is to activate the two motors turn in the same direction, to move back the two motors turn in the other direction and go to the sides the motors are reversed between the two
and I can't create this code...

code created so far:

int counter = 0; 
int aState;
int aLastState;  
int switchPin1 = 4;
int switchPin2 = 3;
int switchPin3 = 2;
int switchPIN4 = 1;

void setup() {
  Serial.begin(9600);
  
  pinMode(outputA, INPUT_PULLUP);
  pinMode(outputB, INPUT_PULLUP);
  
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  
  digitalRead(switchPin1);
  digitalRead(switchPin2);
  digitalRead(switchPin3);
  digitalRead(switchPin4);
  
  aLastState = digitalRead(outputA);   
  
  rotate(LEFT);

  Serial.begin(9600);
  
  pinMode(outputA, INPUT_PULLUP);
  pinMode(outputB, INPUT_PULLUP);
  
  pinMode(13, OUTPUT);
  pinMode(8, OUTPUT);
  
  aLastState = digitalRead(outputA);   
  
  rotate(LEFT);
}

void loop() {
  aState = digitalRead(outputA);
   if (aState != aLastState){     
     if (digitalRead(outputB) != aState) { 
       counter ++;
       if(counter > 130) {
         rotate(RIGHT);
       }
     } else {
       counter --;
        if(counter < 0) {
         rotate(LEFT);
       }
     }
     Serial.print("Position: ");
     Serial.println(counter);
   } 
   aLastState = aState;
  delay(10);

  aState = digitalRead(outputA);
   if (aState != aLastState){     
     if (digitalRead(outputB) != aState) { 
       counter ++;
       if(counter > 130) {
         rotate(RIGHT);
       }
     } else {
       counter --;
        if(counter < 0) {
         rotate(LEFT);
       }
     }
     Serial.print("Position: ");
     Serial.println(counter);
   } 
   aLastState = aState;
  delay(12);
}


void rotate(int d) {
  switch(d) {
    case LEFT:
    digitalWrite(5, HIGH);
    digitalWrite(6, LOW);
    break;
	case RIGHT:
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    break;
  }
}

the code is far from finished and that's why I need help. there is no help anywhere to create this
tanks :slight_smile:

See Joystick/4 switch please help me! for background information

thank you so do you have an answer? to complete my code

Does the code even compile? I can't see where you declare LEFT, RIGHT, outputA and outputB.

It seems illogical to have an input called outputA, but that's just me.

I can write the code for you, at a cost. If you OK with that please PM me.

sent

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.