This is my first project working with Arduino, what a learning experience this has been.
I am using a joystick and a Megamoto shield driving a 12v DC motor.
Forward and reverse is controlled by PWM on either pin 3 and 11.
I have borrowed a code but have a deadspace issuse I cant figure out.
Other that that the code works great with variable speed & direction!
//
//
// Tank bot work in progress 30/12/10 version 0.5:
#include <Servo.h>
//turret and servo(s)
Servo scanservo ;
Servo Larmservo ;
Servo Harmservo ;
//motor pins and reverse
const int RTMPin = 11 ;
const int RTRPin = 3 ;
// pointometers/joystick
int potpin = A3 ;
int joyx = A4 ;
// variables
int potval = 0 ;
int joyxval = 0 ;
int RTval = 0 ;
int LTval = 0 ;
int LDval = 0 ;
int RDval = 0 ;
/////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600);
pinMode (RTMPin, OUTPUT);
pinMode (RTRPin, OUTPUT);
pinMode (joyx, INPUT);
}
/////////////////////////////////////////////////////////////////////////////////////
void loop()
{
potval = analogRead(potpin);
LDval = analogRead(joyx);
RDval = analogRead(joyx);
LDval = map (LDval, 530, 1023, 0, 255);
RDval = map (RDval, 500, 0, 0, 255);
if (analogRead(joyx)<500)
{
LTval = analogRead(joyx);
LTval = map(LTval, 0, 500, 0, 255);
RTval = analogRead(joyx);
RTval = map(RTval, 0, 500, 0, 255);
LTval = (LTval - constrain (RDval, 0, 255));
RTval = (RTval - constrain (LDval, 0, 255));
analogWrite (RTMPin, constrain (LTval, 0, 255));
digitalWrite (RTRPin, HIGH);
}
if (analogRead(joyx)>530)
{
LTval = analogRead(joyx);
LTval = map(LTval, 530, 1023, 0, 255);
RTval = analogRead(joyx);
RTval = map(RTval, 530, 1023, 0, 255);
LTval = (LTval - constrain (RDval, 0, 255));
RTval = (RTval - constrain (LDval, 0, 255));
analogWrite (RTMPin, constrain (LTval, 0, 255));
digitalWrite (RTRPin, LOW);
}
delay(150);
}
Thanks!