Joystick/4 switch please help me!

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 turns 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...


here is my circuit. if you need any other information I will answer within a minute. Huge thanks to anyone who can help me.

What have you tried ?

I'm trying to create a joystick with 4 switches to controls two motors to move a vehicle and I can't create the program

Are you saying that you have not even tried to write a sketch do do what you want ?

Why ?

Are you using the Blocks in Tinkercad or code text?

I tried to create this program. I searched for information on the forum but there is nothing that helps me on this program
I just managed to turn a motor

Can you please copy and paste the code in formatted?
There is a little symbol you can press </>

on this code there is nothing on the switches because I can't do it
code:

int counter = 0; 
int aState;
int aLastState;  


void setup() {
  Serial.begin(9600);
  
  pinMode(outputA, INPUT_PULLUP);
  pinMode(outputB, INPUT_PULLUP);
  
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  
  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;
  }
}

because I'm not very good at programming

Hello
shall we do your job ?

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

I need help..

There are major parts missing to this code. Where are outputA, outputB, LEFT, RIGHT defined? Is this the full code you got?

the code is not at all complete and that's why I need help

Please do us all a favour and edit post #10 and add code tags to make it easier to deal with

like this ?

No. Like this

int counter = 0;
int aState;
int aLastState;

void setup()
{
  Serial.begin(9600);
  pinMode(outputA, INPUT_PULLUP);
  pinMode(outputB, INPUT_PULLUP);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  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;
  }
}

Note that I have also Auto Formatted the code in the IDE

See how much easier it is to read and see the code blocks ?

Hello
The sketch is incomplete over all.
At least the following functions are missing:

  • controlMovement (forward,backward,leftturn,rightturn, stopp)
  • joyStickRead(????)
  • and other

Have a nice day and enjoy coding in C++.

p.s. You may moved your request to Jobs and Paid Consultancy - Arduino Forum