Go Down

Topic: Joystick not working after switching from UNO (Read 304 times) previous topic - next topic

HandigeHarry

Hey guys,

Today I switched from my UNO to DUE because I needed more I/O. (only larger board on my school)
So when I plugged it all in I noticed my motor leds are on and cant use the joystick anymore.
Also my motor is turning.

When I delete the joystick code, and upload without it, the leds are off and my motor is not turning.

Is there something different with coding between these two boards?

My code is a bit of a mess, sorry.

Code: [Select]
const int vert_motor1   = 2;     //pin 2 of arduino to pin 7 of l293d
const int vert_motor2   = 3;     //pin 3 of arduino to pin 2 of l293d
const int horz_motor1   = 7;      //pin 7 of arduino to pin 10 of l293d
const int horz_motor2   = 8;      //pin 8 of arduino to pin 15 of l293d

const int mvert = 9;
const int mhorz = 10;

int joyvert = A0;
int joyhorz = A1;

int vert_motor_led = 6;
int horz_motor_led = 4;

int modeSwitchPin = 13;         // switch is connected to pin 13
int ledAutoPin    = 11;
int ledManPin     = 12;
int val;                        // variable for reading the pin status
int val2;                       // variable for reading the delayed status
int modeButtonState;            // variable to hold the button state
int Mode = 0;

int start_stop_auto = 5;
int val3;
int val4;
int startButtonState;
int state = 0;

const unsigned int onTime = 1000;
const unsigned int offTime = 500;

unsigned long previousMillis = 0;
int interval = onTime;
boolean motorstate1 = true;
boolean motorstate2 = false;

int speed_vert = 0;
int speed_horz = 0;

int joyposvert = 512;
int joyposhorz = 512;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(vert_motor1, OUTPUT);
  pinMode(vert_motor2, OUTPUT);
  pinMode(horz_motor1, OUTPUT);
  pinMode(horz_motor2, OUTPUT);
  pinMode(mvert, OUTPUT);
  pinMode(mhorz, OUTPUT);
  pinMode(vert_motor_led, OUTPUT);
  pinMode(horz_motor_led, OUTPUT);
  pinMode(modeSwitchPin, INPUT);    // Set the switch pin as input
  pinMode(start_stop_auto, INPUT_PULLUP);
  pinMode(ledAutoPin, OUTPUT);
  pinMode(ledManPin, OUTPUT);
  previousMillis = millis();
  modeButtonState = digitalRead(modeSwitchPin);   // read the initial state
}

void loop() {

  //***AUTO/MANUAL SWITCH LOOP***//
  unsigned long currentMillis = millis();

  val = digitalRead(modeSwitchPin);      // read input value and store it in val
  delay(10);                             // 10 milliseconds is a good amount of time
  val2 = digitalRead(modeSwitchPin);     // read the input again to check for bounces
  if (val == val2) {                     // make sure we got 2 consistant readings!
    if (val != modeButtonState) {        // the button state has changed!
      if (val == LOW) {                  // check if the button is pressed
        if (Mode == 0) {
          Mode = 1;
        } else {
          if (Mode == 1) {
            Mode = 0;
          }
        }
      }
    }
  }
  modeButtonState = val;                 // save the new state in our variable

  joyposvert = analogRead(joyvert);
  joyposhorz = analogRead(joyhorz);

  if (Mode == 0)
  {
    digitalWrite(ledAutoPin, HIGH);
    digitalWrite(ledManPin, LOW);
    Serial.println("Automatic Mode");

    val3 = digitalRead(start_stop_auto);      // read input value and store it in val
    delay(10);                          // 10 milliseconds is a good amount of time
    val4 = digitalRead(start_stop_auto);      // read the input again to check for bounces
    if (val3 == val4) {                 // make sure we got 2 consistant readings!
      if (val3 != startButtonState) {   // the button state has changed!
        if (val3 == LOW) {              // check if the button is pressed
          if (state == 0) {
            state = 1;
          } else {
            if (state == 1) {
              state = 0;
            }
          }
        }
      }
    }
    startButtonState = val3;             // save the new state in our variable

    if (state == 0) { // all-off
      Serial.println("Stop automatisch programma");
      digitalWrite(vert_motor_led, LOW);
      digitalWrite(horz_motor_led, LOW);
      digitalWrite(vert_motor1, LOW);
      digitalWrite(vert_motor2, LOW);
      digitalWrite(horz_motor1, LOW);
      digitalWrite(horz_motor2, LOW);
      analogWrite(mvert, 0);
      analogWrite(mhorz, 0);
    }

    if (state == 1) {
      Serial.println("Start automatisch programma");

        previousMillis = currentMillis;
      }
    }
  }

  if (Mode == 1)
  {
    digitalWrite(ledAutoPin, LOW);
    digitalWrite(ledManPin, HIGH);
    digitalWrite(vert_motor_led, LOW);
    digitalWrite(horz_motor_led, LOW);
    Serial.println("MANUAL MODE");


    if (joyposvert < 470)
    {
      digitalWrite(vert_motor1, LOW);
      digitalWrite(vert_motor2, HIGH);
      digitalWrite(vert_motor_led, HIGH);

      speed_vert = map(joyposvert, 470, 0, 0, 255);
    }
    else if (joyposvert > 550)
    {
      digitalWrite(vert_motor1, HIGH);
      digitalWrite(vert_motor2, LOW);
      digitalWrite(vert_motor_led, HIGH);

      speed_vert = map(joyposvert, 550, 1023, 0, 255);
    }
    else
    {
      speed_vert = 0;
      speed_horz = 0;
      digitalWrite(vert_motor_led, LOW);
      digitalWrite(horz_motor_led, LOW);
    }

    if (joyposhorz < 470)
    {
      digitalWrite(horz_motor1, LOW);
      digitalWrite(horz_motor2, HIGH);
      digitalWrite(horz_motor_led, HIGH);

      speed_horz = map(joyposhorz, 470, 0, 0, 255);
    }
    else if (joyposhorz > 550)
    {
      digitalWrite(horz_motor1, HIGH);
      digitalWrite(horz_motor2, LOW);
      digitalWrite(horz_motor_led, HIGH);

      speed_horz = map(joyposhorz, 550, 1023, 0, 255);
    }

    if (speed_vert < 8)speed_vert = 0;
    if (speed_horz < 8)speed_horz = 0;

    analogWrite(mvert, speed_vert);
    analogWrite(mhorz, speed_horz);
  }
}


Speklap

#1
Apr 09, 2018, 05:03 pm Last Edit: Apr 09, 2018, 05:04 pm by Speklap
The UNO and DUE are different internal. The uno is based on ATmega328P, DUE on Atmel SAM3X8E ARM Cortex-M3.
So the due is an ARM board that has different instruction set than the UNO which is AVR iirc. The pinlayout is also different, so you'll have to port your uno pins to the due.

ard_newbie


A few thoughts:

- Parameters you are using in map() function seem weird, are you sure they are correct ?
- When you read digital pins, it's a good practice to add a pull down resistor (~ 4.7K ) because they can collect all sorts of EMI.


HandigeHarry

The UNO and DUE are different internal. The uno is based on ATmega328P, DUE on Atmel SAM3X8E ARM Cortex-M3.
So the due is an ARM board that has different instruction set than the UNO which is AVR iirc. The pinlayout is also different, so you'll have to port your uno pins to the due.
I did, still the same issue.

A few thoughts:

- Parameters you are using in map() function seem weird, are you sure they are correct ?
- When you read digital pins, it's a good practice to add a pull down resistor (~ 4.7K ) because they can collect all sorts of EMI.
It has been working for days on the UNO. When I rotate the joystick the leds do flicker a little bit. Where would I need to put the resistors you are mentioning?

ard_newbie

#4
Apr 09, 2018, 06:23 pm Last Edit: Apr 09, 2018, 06:26 pm by ard_newbie
Where would I need to put the resistors you are mentioning?

Between digital pins you are reading and ground.

http://www.resistorguide.com/pull-up-resistor_pull-down-resistor/

HandigeHarry

#5
Apr 09, 2018, 07:15 pm Last Edit: Apr 09, 2018, 07:16 pm by HandigeHarry
Okay so both buttons that its reading are now with pull up from the internal pull up resistor of the arduino.
Also I put serial.println in the joystick code, and what i get is this

Code: [Select]

MANUAL MODE
Verticaal Rechts
Horizontaal Rechts
MANUAL MODE
Verticaal Rechts
Horizontaal Rechts
MANUAL MODE
Verticaal Rechts
Horizontaal Rechts
MANUAL MODE
Verticaal Rechts
Horizontaal Rechts
MANUAL MODE
Verticaal Rechts
Horizontaal Rechts
MANUAL MODE
Verticaal Rechts
Horizontaal Rechts
MANUAL MODE
Verticaal Rechts
Horizontaal Rechts
MANUAL MODE
Verticaal Rechts
Horizontaal Rechts


Code: [Select]

  if (Mode == 1)
  {
    digitalWrite(ledAutoPin, LOW);
    digitalWrite(ledManPin, HIGH);
    Serial.println("MANUAL MODE");


    if (joyposvert < 470)
    {
      digitalWrite(vert_motor1, LOW);
      digitalWrite(vert_motor2, HIGH);
      digitalWrite(vert_motor_led, HIGH);
      speed_vert = map(joyposvert, 470, 0, 0, 255);
      Serial.println("Verticaal Links");
    }
    else if (joyposvert > 550)
    {
      digitalWrite(vert_motor1, HIGH);
      digitalWrite(vert_motor2, LOW);
      digitalWrite(vert_motor_led, HIGH);
      speed_vert = map(joyposvert, 550, 1023, 0, 255);
      Serial.println("Verticaal Rechts");
    }
    else
    {
      speed_vert = 0;
      speed_horz = 0;
      digitalWrite(vert_motor_led, LOW);
      digitalWrite(horz_motor_led, LOW);
    }

    if (joyposhorz < 470)
    {
      digitalWrite(horz_motor1, LOW);
      digitalWrite(horz_motor2, HIGH);
      digitalWrite(horz_motor_led, HIGH);
      speed_horz = map(joyposhorz, 470, 0, 0, 255);
      Serial.println("Horizontaal Links");
    }
    else if (joyposhorz > 550)
    {
      digitalWrite(horz_motor1, HIGH);
      digitalWrite(horz_motor2, LOW);
      digitalWrite(horz_motor_led, HIGH);
      speed_horz = map(joyposhorz, 550, 1023, 0, 255);
      Serial.println("Horizontaal Rechts");
    }

    if (speed_vert < 8)speed_vert = 0;
    if (speed_horz < 8)speed_horz = 0;

    analogWrite(mvert, speed_vert);
    analogWrite(mhorz, speed_horz);
  }


As you see, for some reason its running the else if codes...

Go Up