This question is an extension of this question.
Accessing Values From Array Within a Library
The answer provided by Mark showed me how to read and print a single value from a particular array by using the index number to print to the serial console.
The second part of this question is how to get these values from the array to map to 0-180 for values that are useful to the servo.
The below code is my attempt that is not working. I used the servo sweep example from the Arduino examples which worked fine with a potentiometer but not with the ppm input. What am I doing wrong here? This won't compile. (I am using TinyServo.h library since this is for an attiny85. Works with with the pot mapping just fine.)
#include <TinyServo.h>
#include <TinyPinChange.h>
#include <TinyPpmReader.h>
#include <Rcul.h>
int PPM_INPUT_PIN = TinyPpmReader.width_us(3); //<------- Doesn't work
//int potpin0 = 3; <--------- // This worked
int val0; // integer to store value
const byte SERVOS = 1; // how many servos do you have? up to 5 on ATTiny85 and 8 on ATtiny84/2313
const byte servoPin[SERVOS] = { 0 }; // what pins are your servos on?
// you have the option to give your servos nice names. 0 refers to the first servo pin above, 1 to the second, etc
#define PANSERVO 0
#define PPM_INPUT_PIN 2 <--------- //define pin for ppm input
void setup()
{
setupServos();
}
void loop()
{
val0 = analogRead(PPM_INPUT_PIN); <--------- //Doesnt work// reads the value of the potentiometer (value between 0 and 1023) // reads the value of the 2nd potentiometer (value between 0 and 1023)
val0= map(val0, 1000, 2000, 0, 180);
// scale it to use it with the servo (value between 0 and 180)
// scale it to use it with the servo (value between 0 and 180)
moveServo(PANSERVO, val0);
delay(5); // waits for the servo to get there
}
For some background, the ppm library outputs like this to the serial monitor:
Ch4=1560 us
These values move from ~1000 - 2000 depending on the ppm signal input. (The input is from a RC remote control receiver)
This example below prints only channel 4 (Ch4=1560 us)from a total of 8 channels. Now, I want to map this to the values of 0-180:
#include <TinyPinChange.h>
#include <TinyPpmReader.h>
#include <Rcul.h>
#define PPM_INPUT_PIN 2
void setup()
{
Serial.begin(38400);//attiny tx - rx uno does not work at 9600 baud. Set to 38400 @ attiny 1mhz and uno serial console to 38400 baud
TinyPpmReader.attach(PPM_INPUT_PIN); /* Attach TinyPpmReader to PPM_INPUT_PIN pin */
}
void loop()
{
Serial.print(F("Ch"));Serial.print(3);Serial.print(F("="));Serial.print(TinyPpmReader.width_us(3));Serial.println(F(" us"));
display results */
delay(500);
}
What am I doing wrong?
1 Answer 1
int PPM_INPUT_PIN = TinyPpmReader.width_us(3);
will save the result into variable PPM_INPUT_PIN
, while
val0 = analogRead(PPM_INPUT_PIN);
will save an analog reading results from PPM_INPUT_PIN
channel into variable val0
. This is (one of) your problem.
To map the PPM value, you can do:
#include <TinyServo.h>
#include <TinyPinChange.h>
#include <TinyPpmReader.h>
#include <Rcul.h>
int val0, // integer to store value
mappedval0; // integer to store mapped value;
const byte SERVOS = 1; // how many servos do you have? up to 5 on ATTiny85 and 8 on ATtiny84/2313
const byte servoPin[SERVOS] = { 0 }; // what pins are your servos on?
// you have the option to give your servos nice names. 0 refers to the first servo pin above, 1 to the second, etc
#define PANSERVO 0
#define PPM_INPUT_PIN 2 <--------- //define pin for ppm input
const int PPM_max_value = xxxxx; //put the max value of PPM reading result
const int PPM_min_value = yyyyy; //put the min value of PPM reading result
void setup()
{
setupServos();
}
void loop()
{
val0 = TinyPpmReader.width_us(4); //get PPM reading result from index 4
mappedval0= map(val0, PPM_min_value, PPM_max_value, 0, 180);
// scale it to use it with the servo (value between 0 and 180)
moveServo(PANSERVO, mappedval0);
delay(5); // waits for the servo to get there
}
-
I think I see what I was doing.Thanks. Why is int left out before mappedval0; ? EDIT. I see the comma after val0. Just extending the variable int type to the next line.Brad– Brad2017年01月11日 15:43:57 +00:00Commented Jan 11, 2017 at 15:43
-
This does in fact compile but does not work.Brad– Brad2017年01月11日 16:12:49 +00:00Commented Jan 11, 2017 at 16:12
-
sorry, there's a mistake in my code. It should be
moveServo(PANSERVO,mappedval0);
. I've edit the answerduck– duck2017年01月11日 16:14:11 +00:00Commented Jan 11, 2017 at 16:14 -
Yes, I saw that before compiling and replaced it with the new variable. It does compile but does nothing.Brad– Brad2017年01月11日 16:28:45 +00:00Commented Jan 11, 2017 at 16:28
-
have you tried to see the result of PPM reading? And have you successfully make the servo rotate (without using PPM result as input)?duck– duck2017年01月11日 16:41:35 +00:00Commented Jan 11, 2017 at 16:41
int PPM_INPUT_PIN = TinyPpmReader.width_us(3);
contain a reading result, when followed byval0 = analogRead(PPM_INPUT_PIN);
it's clearly absurd.