needs a code to convert analogue input into a PWM output please help

Hi i need a code which will take a analogue input and convert it into digital PWM output for the arduino mega.

would appreciate some help in how to write it as i have little programming experience.

robert

analogWrite(9, analogRead(0) >> 2);

Why shift 4 Mike? Why not just 2, for 10 bits down to 8?
Or mask off upper 6, then shift 2 to get down to 8.

Why shift 4 Mike? Why not just 2

Yes, in too much of a rush. Corrected now. Thanks.

Here another :

analog_value=analogRead(any_analog_pin);
pwm_value=map(analog_value,0,1023,0,255);
analogWrite(any_pwm_pin, pwm_value);

Here what the code do : read analog pin : from 0 to 1023 <-- analogRead()

map()
Convert in a value between 0 to 255 using this methode : a/b = x/y analog_value / 1023 = pwm_value / 255

analogWrite()
Provide a PWM pulse from any PWM pin of the Arduino.

I am need this to be able to work with a analogue joystick so that when it is in the middle it will either make the motor controller http://www.robotmarketplace.com/products/0-SABER2X12-RC.html go backwards or forwards as i move the joystick

Let me understand this. At the middle ( around 512 ) , the motor is at rest, when you move foward the joystick, the motor going foward , speed is joystick a bit foward, motor slow, joystick at top, going fast. OK and reverse is the same principle...

I am correct ?

yes that is what i am trying to achive

yes that is what i am trying to achive

What is it that YOU have tried?

int pinVal = analogRead(pinNum);
if(pinVal > 520)
{
   // pinVal = map of pinVal from 520 to 1023 to 0 to 255
   // dir = forward
}
else if(pinVal < 504)
{
   // pinVal = map of pinVal from 520 to 0 to 0 to 255
   // dir = backward
}

@electronicscrazy1996

I check this "module", try to find a datasheet for it, <--- no luck so far. So you are trying to "hack" that module using an arduino ? I feel it is possible, if I have more info about what type of signals goes into which pins of the module, what make the motor 1 move / stop/ reverse and the same for motor 2. If you know what type of signals do what and theres... a code could be design using that info. The example provided by PaulS, Grumpy_Mike and me are for a H Bridge type of circuit using bi-polar and mosfet transistors. And beside, I don't know if the PWM signal is compatible with the module you give a link to.

The linked controller looks like an R/C ESC, so simply drive it with the Servo library

I am not really trying to hack the device i'm trying to use the PWM output to go into the controller, the signals going to the controller are 5v, GND and signal, but i don't know what type of signals i guess PWM.

The ESC generally accepts a nominal 50 Hz PWM servo input signal

sp. "PPM servo input signal"
Like the signal generated by the Servo library.

I am not really trying to hack the device i'm trying to use the PWM output to go into the controller, the signals going to the controller are 5v, GND and signal, but i don't know what type of signals i guess PWM.

The PWM of a analogWrite() is about 450 Hz. And a servo is about 50 Hz. Why not try the module, using a small code to see what happen.

Here a start :

digitalWrite(any_digital_pin, HIGH);
delay(t_on);
digitalWrite(any_digital_pin, LOW);
delay(t_off);

The code is a simple "blink". The delay use miliseconds A TTL signal at 50% duty cycle is T=ton +toff. And T= 1/Frequency. So to change the duty cycle, you just change t_on value and t_off value.

Like I sid : Experiment and see what happen and learn.

In my definition of "hacking", is trying to access, experiments , reverse engineer a device to do something with it by a way it was not design to do.

Or, take the simple approach, and use the Servo library, which was written for the purpose.