I am currently attempting to drive 2 motors in both directions with variable speed using a Megamoto and a joystick.
I am using a borrowed code that I have modified slightly and it seems to work for the most part.
The problem is with my code, sometimes it will PWM both the high and low side at the same time. I'm sure it's easy to fix but I am lost.
I have some deadspace built in my code for the joystick and have verified that it has plenty of space with Analog read.
#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 ;
const int YMPin = 9 ;
const int YRPin = 10 ;
// pointometers/joystick
int joyx = A4 ;
int joyy = A2 ;
// variables
int joyxval = 0 ;
int joyyval = 0 ;
int RTval = 0 ;
int LTval = 0 ;
int LDval = 0 ;
int RDval = 0 ;
int RYval = 0 ;
int LYval = 0 ;
int YLDval = 0 ;
int YRDval = 0 ;
/////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600);
pinMode (RTMPin, OUTPUT);
pinMode (RTRPin, OUTPUT);
pinMode (YMPin, OUTPUT);
pinMode (YRPin, OUTPUT);
pinMode (joyx, INPUT);
pinMode (joyy, INPUT);
}
/////////////////////////////////////////////////////////////////////////////////////
void loop()
{
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(0);
{
YLDval = analogRead(joyy);
YRDval = analogRead(joyy);
YLDval = map (YLDval, 530, 1023, 0, 255);
YRDval = map (YRDval, 500, 0, 0, 255);
if (analogRead(joyy)<500)
{
LYval = analogRead(joyy);
LYval = map(LYval, 0, 500, 0, 255);
RYval = analogRead(joyy);
RYval = map(RYval, 0, 500, 0, 255);
LYval = (LYval - constrain (YRDval, 0, 255));
RYval = (RYval - constrain (YLDval, 0, 255));
analogWrite (YMPin, constrain (LYval, 0, 255));
digitalWrite (YRPin, HIGH);
}
if (analogRead(joyy)>530)
{
LYval = analogRead(joyy);
LYval = map(LYval, 530, 1023, 0, 255);
RYval = analogRead(joyy);
RYval = map(RYval, 530, 1023, 0, 255);
LYval = (LYval - constrain (YRDval, 0, 255));
RYval = (RYval - constrain (YLDval, 0, 255));
analogWrite (YMPin, constrain (LYval, 0, 255));
digitalWrite (YRPin, LOW);
}
}
}
Any help is appreciated.