Arduino motor aansturing mbv joystick

 
 
 
basic code

// Arduino pin numbers
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output

void setup() {
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);
  Serial.begin(115200);
}void loop() {
  Serial.print("Switch:  ");
  Serial.print(digitalRead(SW_pin));
  Serial.print("\n");
  Serial.print("X-axis: ");
  Serial.print(analogRead(X_pin));
  Serial.print("\n");
  Serial.print("Y-axis: ");
  Serial.println(analogRead(Y_pin));
  Serial.print("\n\n");
  delay(500);}
 
basic code 2 :
int xPin = A1;
int yPin = A0;
int buttonPin = 2;
int xPosition = 0;
int yPosition = 0;
int buttonState = 0;

void setup() {  
Serial.begin(9600); 
pinMode(xPin, INPUT);  
pinMode(yPin, INPUT);  
pinMode(buttonPin, INPUT_PULLUP); 
pinMode(buttonPin, INPUT);  
digitalWrite(buttonPin, HIGH);  
}

void loop() {  
xPosition = analogRead(xPin);  
yPosition = analogRead(yPin);  
buttonState = digitalRead(buttonPin);  
Serial.print("X: ");  
Serial.print(xPosition);  
Serial.print(" | Y: ");  
Serial.print(yPosition);  
Serial.print(" | Button: ");  
Serial.println(buttonState);  
delay(100);



joystick in combinatie met 2 elektromotoren:
 /* 
Adafruit Motor shield library
copyright Adafruit Industries LLC, 2009
this code is public domain, enjoy!
----------------------------------------------
Code edited by Jasper Oomen & Gido de Man meesterproef JRL-Boxtel
*/

#include <AFMotor.h>
#include <Servo.h>

AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
int RPM = 200; //RPM Motoren
int S1 = 22;
int S2 = 23;
int S3 = 24;
int S4 = 25;
int buttonPin = 26;
int xPin = A14;
int yPin = A15;
int xPosition = 0;
int yPosition = 0;
int buttonState = 0;

void setup() {
  motor1.setSpeed(RPM);
  motor2.setSpeed(RPM);
  motor1.run(RELEASE);
  motor2.run(RELEASE);
  pinMode(S1, INPUT);
  pinMode(S2, INPUT);
  pinMode(S3, INPUT);
  pinMode(S4, INPUT);

  Serial.begin(9600);
 
  pinMode(xPin, INPUT);
  pinMode(yPin, INPUT);

  pinMode(buttonPin, INPUT_PULLUP);
}

// Test de DC motor, stappenmotor en servo tegelijk
void loop() {
  //motorNR.run(FORWARD); Vooruit
  //motorNR.run(BACKWARD); Achteruit
  //motorNR.run(RELEASE); Stoppen
  if(digitalRead(S1) == HIGH){
    int countS1A = 0;
    while(digitalRead(S1) == HIGH && countS1A <= 4){
      countS1A = countS1A + 1;
      delay(50);
    }
    if(countS1A >= 4){ //Good
      motor1.run(RELEASE);
    }
  }
  if(digitalRead(S1) == HIGH){
    int countS1B = 0;
    while(digitalRead(S1) == HIGH && countS1B <= 4){
      countS1B = countS1B + 1;
      delay(50);
    }
    if(countS1B >= 4){ //Good
      motor1.run(RELEASE);
    }
  }  
  if(digitalRead(S2) == HIGH){
    int countS2A = 0;
    while(digitalRead(S2) == HIGH && countS2A <= 4){
      countS2A = countS2A + 1;
      delay(50);
    }
    if(countS2A >= 4){ //Good
      motor2.run(RELEASE);
    }
  }
  if(digitalRead(S2) == HIGH){
    int countS2B = 0;
    while(digitalRead(S2) == HIGH && countS2B <= 4){
      countS2B = countS2B + 1;
      delay(50);
    }
    if(countS2B >= 4){ //Good
      motor2.run(RELEASE);
    }
  }
 
  xPosition = analogRead(xPin);
  yPosition = analogRead(yPin);
  buttonState = digitalRead(buttonPin);
  if(buttonState == 0){
    motor1.run(RELEASE);
    motor2.run(RELEASE);
  }
  else{
    if(xPosition > 450 && xPosition < 550){ //midden
      motor1.run(RELEASE); //stoppen
    }
    else if(xPosition >= 550){
      motor1.run(FORWARD); //M1 FORWARD
    }
    else if(xPosition <= 450){
      motor1.run(BACKWARD);
    }
 
    if(yPosition > 455 && yPosition < 555){ //midden
      motor2.run(RELEASE); //stoppen
    }
    else if(yPosition >= 555){
      motor2.run(FORWARD); //M1 FORWARD
    }
    else if(yPosition <= 550){
      motor2.run(BACKWARD);
    }
  }
}