Go Down

Topic: -output value to +output value? (Read 986 times) previous topic - next topic

smrp1984

I am using a joystick, when centered my analog value is 521. I have mapped my sensor value  

outputValue = map(sensorValue, 521, 1023, 0, 255);

this works great as long as I am moving my joystick to the right, however when I move it left I get a - outputValue. With PWM the - value is either HIGH or LOW and nothing in between.

I would like to have something like this...

if outputValue >0 analogWrite (pin9, outputValue);
if outputValue <0 analogWrite (pin10, +outputValue);

where if the value is - it will output to a different pin and convert the output to a + int.


Any help?

Thanks
SMRP1984

jraskell

What you have is almost what you need.  Just needs two tweaks...

change +output to -output in your second analogWrite.

One of your if statements should handle the case where outputValue = 0.  Either use >= in the first line, or <= in the second line.

PaulS

You want to map the values between 0 to 521 to a different range, too.

retrolefty


Not sure what the full raw range of your joy stick is, but have
You tried something like this for your mapping function? :

Code: [Select]
outputValue = map(sensorValue, 0, 1023, -255, 255);


smrp1984

#4
Jan 21, 2011, 12:16 am Last Edit: Jan 21, 2011, 12:36 am by smrp1984 Reason: 1
All are great ideas and would prob work...
I got it fixed, works great  :)
here is the code:

Code: [Select]
/* PWM controle of motors for pan tilt controler using a joystick as an input device.
* By: SMRP1984
* Date: Jan 20 2011
*/

const int jsticklr = A0;   // L-R joystick input
const int loutpin = 9;     // Left out pin
const int routpin = 10;    // Right out pin
const int jstickud = A1;   // U-D joystick in
const int uoutpin = 6;     // Up out pin
const int doutpin = 5;     // Down out pin

int lrinValue = 0;         // Set all values to 0
int lroutValue = 0;
int udinValue = 0;
int udoutValue = 0;

void setup()
{
 Serial.begin(9600);     // Begin serial conection @ 9600baud
}

void loop()
{
 lrinValue = analogRead(jsticklr);                          // Set LR value to LR joystick input
 lroutValue = map(lrinValue, 525, 1023, 0, 255);            // Remap input to PWM output
 if (lroutValue >= 0) analogWrite(loutpin, lroutValue);     // + = right
 if (lroutValue <= 0) analogWrite(routpin, -lroutValue);    // - = left
 
 udinValue = analogRead(jstickud);                          // Set UD value to UD joystick input
 udoutValue = map(udinValue, 521, 1023, 0, 255);            // Remap input to PWM output
 if (udoutValue <= 0) analogWrite(uoutpin, -udoutValue);    // - = up
 if (udoutValue >= 0) analogWrite(doutpin, udoutValue);     // + = down

 
 Serial.print("Jstick L - R = " );  // Print LRin label                    
 Serial.print(lrinValue);           // Print LRin value
 Serial.print("\t Output = ");      // Print LRout label
 Serial.println(lroutValue);        // Print LRout value w/ return
 Serial.print("jstick U - D = " );  // Print UDin label
 Serial.print(udinValue);           // Print UDin value
 Serial.print("\t Output = ");      // Print UDout label
 Serial.println(udoutValue);        // Print UDout Value w/ return

 delay(10);                         // Give everything a bit of time
}

Go Up