If you mean Pulse Position Modulation, I think you're using the wrong term. Standard RC servos use Pulse Width Modulation
Lefty is absolutely right.
Digital R/C systems (used to be called "digiprop" for "digital proportional") have used PPM for at least the last 35 years.
The difference is subtle, and you could use PWM to generate a PPM-compatible waveform (though you're advised not to) using the Arduino's eight bit PWM, which is at too high a frequency by default (a servo or ESC may not work at all with too high a frame-rate, or may draw excessive current), and has too low resolution.
If you take a typical range of an R/C PPM pulse of 1 to 2ms, an eight bit resolution PWM generating the whole 20ms frame will give only about twelve graduations or positions.
I do understand the different between the PWM provided by the analog pins and the PWM used by ordinary RC servos, but I still don't understand why you refer to the servo signal as PPM. It is not Pulse Position Modulation as I understand the term. PPM is a synchronous signalling technique requiring a timing reference, and obviously (?) it's inherently impossible for the single wire servo signal to be synchronous. Perhaps PPM means something different to you - if so, could you explain what meaning you're giving the term?
I appreciate that you want to make a distinction between a servo control signal and an Arduino's ordinary analogue output signal, but using a term which is flat-out wrong is imo not a good way to achieve that.
I have commented the else for the second time but i am still Getting the error
const int apin =A0;
const int m1 = 9;
const int m2 = 11;
int aval = 0;
int fval = 0;
int cval;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop()
{
// read the analog in value:
aval = analogRead(apin);
// map it to the range of the analog out:
fval = map(aval,0,0,0,1023);
// change the analog out value:
}
void check_input() {
if ( Serial.available()) {
char ch = Serial.read();
switch(ch) {
case 'l':
Serial.println("wana go left");
if(fval <= 49){
Serial.println("i'm in left");}
else if(fval > 49)
{
analogWrite(m1,255);
analogWrite(m2,0);
delay(1000);
}
digitalWrite(13, HIGH);
break;
case 'c':
Serial.println("Center the Steering");
int a=constrain(fval,470,510);
if (a){
Serial.println("i am in the center");
}
else
{
analogWrite(m1,255);
}
// else
{
analogWrite(m2,255);
}
// //motor_run = false;
//motor_started = false;
//analogWrite(motor_cur_pin, 0);
//digitalWrite(13, LOW);
break;
case 'r':
//motor_cur_pin = MOTOR_LT_PIN;
if (fval>=975)
{
Serial.println("Direction = Right");
}
else
{
analogWrite(m1,255);
}
//else
{
analogWrite(m2,255);
}
break;
//case 'r':
//motor_cur_pin = MOTOR_RT_PIN;
//Serial.println("Direction = RIGHT");
//break;
}
}
}
ERROR
Discus_forum.cpp: In function 'void check_input()':
Discus_forum:56: error: jump to case label
Discus_forum:39: error: crosses initialization of 'int a'
i am trying to Make the steering of my Robot to move in three states Left 0-50,Right 975-1023 and center 470-510 for which i have developed the following code and i will welcome any more suggestions in the code
const int apin =A0;
const int m1 = 9;
const int m2 = 11;
int aval = 0;
int fval = 0;
int cval;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop()
{
// read the analog in value:
aval = analogRead(apin);
// map it to the range of the analog out:
fval = map(aval,0,0,0,1023);
// change the analog out value:
}
void check_input()
{
if ( Serial.available()) {
char ch = Serial.read();
switch(ch) {
case 'l':
Serial.println("wana go left");
if(fval <= 49){
Serial.println("i'm in left");}
else if(fval > 49)
{
analogWrite(m1,255);
analogWrite(m2,0);
delay(1000);
}
digitalWrite(13, HIGH);
break;
case 'c':
{
Serial.println("Center the Steering");
int a=constrain(fval,470,510); //if the POT is in between 470-510
if (a==a){
Serial.println("i am in the center");// then the Steering is in the center
}
else if(fval>470)//Steering is inthe Left
{
analogWrite(m1,255);
}
else //steering is on the right
{
analogWrite(m2,255);
}
//digitalWrite(13, LOW);
}
break;
case 'r':
//motor_cur_pin = MOTOR_LT_PIN;
if (fval>=975)
{
Serial.println("Direction = Right");
}
else
{
analogWrite(m1,0);
analogWrite(m2,255);
delay(1000);
}
break;
}
}
}
The map function converts an input range of values (fromLow to fromHigh) from a variable to an output range (toLow to toHigh). Your input range starts at zero and ends at zero.
const int apin =A0;
const int m1 = 9;
const int m2 = 11;
int aval = 0;
int fval = 0;
int cval;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop()
{
// read the analog in value:
aval = analogRead(apin);
// map it to the range of the analog out:
fval = map(aval,0,1023,0,255);
// change the analog out value:
}
void check_input()
{
if ( Serial.available()) {
char ch = Serial.read();
switch(ch) {
case 'l':
Serial.println("wana go left");
if(fval <= 49){
Serial.println("i'm in left");}
else if(fval > 49)
{
analogWrite(m1,255);
analogWrite(m2,0);
delay(1000);
}
digitalWrite(13, HIGH);
break;
case 'c':
{
Serial.println("Center the Steering");
int a=constrain(fval,470,510); //if the POT is in between 470-510
if (a==a){
Serial.println("i am in the center");// then the Steering is in the center
}
else if(fval>470)//Steering is inthe Left
{
analogWrite(m1,255);
}
else //steering is on the right
{
analogWrite(m2,255);
}
//digitalWrite(13, LOW);
}
break;
case 'r':
//motor_cur_pin = MOTOR_LT_PIN;
if (fval>=975)
{
Serial.println("Direction = Right");
}
else
{
analogWrite(m1,0);
analogWrite(m2,255);
delay(1000);
}
break;
}
}
}
is there any Mistake using Constrain i have call checl_input............
void loop()
{
// read the analog in value:
aval = analogRead(apin);
// map it to the range of the analog out:
fval = map(aval,0,1023,0,255);
// change the analog out value:
check_input();
}
fval varies from 0-255 depending on pot position, so constraining it to the range 470-510 means a will always be 470.
I'm a bit confused how this is meant to work. You have a pot that is supposed to be for steering, but then you also have some serial input as well. How are the two supposed to work together?