Good evening all,
I'm working on a 1sheeld controlled robot using my phone accelerometers to control throttle and steering and the gamepad to control the servos. I can get the servos to run using the coloured gamepad buttons but when I connect the servos to the Uno outputs assigned to the gamepad arrows pin 5 makes the servo go straight up and 6 makes it go straight down without pressing any buttons. I'm sure it's an error in the code but I'm not sure where. Please can you assist with this. If there is anything else that can be improved in the coding please let me know.
> #define CUSTOM_SETTINGS
> #define INCLUDE_GAMEPAD_SHIELD
> #define INCLUDE_LED_SHIELD
> #define INCLUDE_ACCELEROMETER_SENSOR_SHIELD
> #define INCLUDE_SLIDER_SHIELD
>
>
> float x, y, z;
>
>
> /* Include 1Sheeld library. */
> #include <OneSheeld.h>
> #include <AFMotor.h>
> /* include Servo library */
> #include <Servo.h>
> /* A name for the LED on pin 7. */
> const int ledPin = 13;
>
>
>
> /* Pin configuration of the Seeedstudio's motor shield. */
> int motorAPin1 = A0;
> int motorAPin2 = A1;
> int motorBPin1 = A2;
> int motorBPin2 = A3;
> //int motorASpeedPin = 9;
> //int motorBSpeedPin = 10;
> // Making servo objects
>
> Servo shoulder;
>
> Servo hand;
>
> Servo grab;
>
> Servo spare;
>
> // Global variables for initializing the servo motors positions
>
> int shoulderPos=90;
>
> int handPos=90;
>
> int grabPos=90;
>
> int sparePos=(90);
>
> /* Define a variable to hold the value of the slider. */
> int value;
> Servo servo;
>
> void setup()
> {
> /* Start communication. */
> OneSheeld.begin();
>
> // Attach servo motors
> // Shoulder on 9, hand on 10, grab on 11 spare on 6, slider on 5.
>
> shoulder.attach(9);
>
> hand.attach(10);
>
> grab.attach(11);
>
> spare.attach(6);
>
> servo.attach(5); //servo pin
>
> // The LED just works as an indicator that the 1Sheeld is working fine
> pinMode(ledPin, OUTPUT);
> /* Set the LED pin as output. */
>
>
> /* Seeedstudio's motor shield initialization. */
> pinMode(motorAPin1, OUTPUT); // IN1 of motor A
> pinMode(motorAPin2, OUTPUT); // IN2 of motor A
> pinMode(motorBPin1, OUTPUT); // IN3 of motor B
> pinMode(motorBPin2, OUTPUT); // IN4 of motor B
> // pinMode(motorASpeedPin, OUTPUT); // Speed of motor A
> //pinMode(motorBSpeedPin, OUTPUT); // Speed of Motor B
> //motorA.setSpeed(250);
> //motorB.setSpeed(250);
> }
>
> void loop()
>
> {/* Always get the value of the slider and output it as PWM to pin 5. */
> value = Slider.getValue();
> servo.write(value);
>
> // Controlling shoulder
> // Orange button moves it forward and green moves it backward
> if(GamePad.isGreenPressed()) {
> shoulderPos+=3;
> } else if(GamePad.isOrangePressed()) {
> shoulderPos-=3;
> }
> // Controlling hand
> // Red closes the hand and blue opens it
> if(GamePad.isRedPressed()) {
> handPos-=3;
> } else if(GamePad.isBluePressed()) {
> handPos+=3;
> }
>
> //Controlling grab
> // Up closes the Grab and down opens it
> if(GamePad.isUpPressed()) {
> grabPos-=3;
> } else if(GamePad.isDownPressed()) {
> grabPos+=3;
> }
> // Controlling spare
> // Left closes the hand and right opens it
> if(GamePad.isLeftPressed()) {
> sparePos-=3;
> } else if(GamePad.isRightPressed()) {
> sparePos+=3;
> }
> // Move the servo motors to it's new positions
>
> shoulder.write(shoulderPos);
>
> hand.write(handPos);
>
> grab.write(grabPos);
>
> spare.write(sparePos);
> // Delay before run again
> delay(20);
>
>
>
> x=AccelerometerSensor.getX();
> y=AccelerometerSensor.getY();
> z=AccelerometerSensor.getZ();
>
> /* If up is pressed, move the car forward. */
> if (y < -5)
> {
> // analogWrite(motorASpeedPin, 255);
> //analogWrite(motorBSpeedPin, 255);
> digitalWrite(motorAPin1, LOW);
> digitalWrite(motorAPin2, HIGH);
> digitalWrite(motorBPin1, LOW);
> digitalWrite(motorBPin2, HIGH);
> digitalWrite(ledPin, HIGH);
>
> }
> /* If down is pressed, move the car backwards. */
> else if (y > 6)
> {
> //analogWrite(motorASpeedPin, 255);
> //analogWrite(motorBSpeedPin, 255);
> digitalWrite(motorAPin1, HIGH);
> digitalWrite(motorAPin2, LOW);
> digitalWrite(motorBPin1, HIGH);
> digitalWrite(motorBPin2, LOW);
> digitalWrite(ledPin, HIGH);
>
>
> }
> /* If right is pressed, turn the car to the right. */
> else if (x < -6)
> {
> //analogWrite(motorASpeedPin, 255);
> //analogWrite(motorBSpeedPin, 255);
> digitalWrite(motorAPin1, LOW);
> digitalWrite(motorAPin2, HIGH);
> digitalWrite(motorBPin1, HIGH);
> digitalWrite(motorBPin2, LOW);
> digitalWrite(ledPin, HIGH);
>
> }
> /* If left is pressed, turn the car to the left. */
> else if (x > 6)
> {
> //analogWrite(motorASpeedPin, 255);
> //analogWrite(motorBSpeedPin, 255);
> digitalWrite(motorAPin1, HIGH);
> digitalWrite(motorAPin2, LOW);
> digitalWrite(motorBPin1, LOW);
> digitalWrite(motorBPin2, HIGH);
> digitalWrite(ledPin, HIGH);
>
> }
> /* If nothing is pressed stop all motors. */
> else
> {
> //analogWrite(motorASpeedPin, 0);
> //analogWrite(motorBSpeedPin, 0);
> digitalWrite(motorAPin1, LOW);
> digitalWrite(motorAPin2, LOW);
> digitalWrite(motorBPin1, LOW);
> digitalWrite(motorBPin2, LOW);
> digitalWrite(ledPin, LOW);
> }
>
> }