Pages: [1]   Go Down
Author Topic: -output value to +output value?  (Read 768 times)
0 Members and 1 Guest are viewing this topic.
Charlotte NC
Offline Offline
Newbie
*
Karma: 0
Posts: 2
I make things!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

New Hampshire
Offline Offline
God Member
*****
Karma: 13
Posts: 779
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16548
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

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

Logged

Charlotte NC
Offline Offline
Newbie
*
Karma: 0
Posts: 2
I make things!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

All are great ideas and would prob work...
I got it fixed, works great  smiley
here is the code:

Code:
/* 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
}
« Last Edit: January 20, 2011, 06:36:48 pm by smrp1984 » Logged

Pages: [1]   Go Up
Jump to: