There are many great examples of how to access information from an array on the net. I ran into a specific case that has me confused. I am using a library for attiny 85 TinyPpmReader.h. This is a simple library that will take PPM signals in from a RC receiver running in pulse position mode and allows you to attach to a pin to process input. Easy enough.
In the code below, I ran into some confusion when trying to access the data in the array. I am trying to filter out a single channel in the ppm stream and print the contents of only 1 channel (Original example prints 8 channels) and eventually map that value to a servo. (I pasted the serial monitor ouput at the end of the code)
I tried accessing the variable stored in the array like this:
void loop()
{
for(uint8_t Idx = 1; Idx <= TinyPpmReader.detectedChannelNb(); Idx++) /* From Channel 1 to Max detected */
if(TinyPpmReader.detectedChannelNb == [5]) //Is this channel 6 in index 0-7?
{
Serial.println(TinyPpmReader.width_us[5); // if so, print only index 5 containing channel 6 values
}
delay(500);
}
Example unedited sketch from library
#include <TinyPinChange.h>
#include <TinyPpmReader.h>
#include <Rcul.h>
#define PPM_INPUT_PIN 2
void setup()
{
Serial.begin(38400)
TinyPpmReader.attach(PPM_INPUT_PIN);
}
void loop()
{
Serial.print(F("* Period="));Serial.print((int)TinyPpmReader.ppmPeriod_us());Serial.println(F(" us *"));
Serial.print(F("ChNb="));Serial.println((int)TinyPpmReader.detectedChannelNb());
for(uint8_t Idx = 1; Idx <= TinyPpmReader.detectedChannelNb(); Idx++) /* From Channel 1 to Max detected */
{
Serial.print(F("Ch"));Serial.print(Idx);Serial.print(F("="));Serial.print(TinyPpmReader.width_us(Idx));Serial.println(F(" us"));
}
delay(500);
}
The serial monitor prints this:
* Period=17928 us *
ChNb=8
Ch=1496 us
Ch=1744 us
Ch=896 us
Ch=1560 us
Ch=1640 us
Ch=1496 us
Ch=1536 us
Ch=1688 us
Any suggestions? It is probably something simple but I dont think I am googling the correct question to get the correct answer.
EDIT: That worked Mark!
New Code Mapping attempt (Compiles,does not work)
#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); <--------- // 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
}
1 Answer 1
This code (from the example) is what prints a single index - index Idx
:
Serial.print(F("Ch"));Serial.print(Idx);Serial.print(F("="));Serial.print(TinyPpmReader.width_us(Idx));Serial.println(F(" us")); }
To print, say, index 3 you'd use
Serial.print(F("Ch"));Serial.print(3);Serial.print(F("="));Serial.print(TinyPpmReader.width_us(3));Serial.println(F(" us")); }
To simply get the value from index 3, without printing it:
int a = TinyPpmReader.width_us(3);
(assuming it's an int
)
-
That worked! Thanks! I was able to only print channel 3 info to the serial monitor. The ultimate goal was to map channel 3 using: int a = TinyPpmReader.width_us(3); But if you check the new code I just added above (below original post) I am not able to map this as an integer for some reason. I added arrows where I changed the code to try to map channel 3. Why does it not compile or map like this?Brad– Brad2017年01月07日 20:40:30 +00:00Commented Jan 7, 2017 at 20:40
-
Glad to hear it. This is a new question really - we should stick to one question per, erm, question. If you post a new question I (or others) will try to help.Mark Smith– Mark Smith2017年01月07日 20:51:34 +00:00Commented Jan 7, 2017 at 20:51
-
That said, perhaps you're not clear what
PPM_INPUT_PIN
is. It's just the number of the pin - not the reading taken from that pin or anything - simply a way to identify the pin. With that in mind,int PPM_INPUT_PIN = TinyPpmReader.width_us(3);
looks nonsensical.Mark Smith– Mark Smith2017年01月07日 21:01:47 +00:00Commented Jan 7, 2017 at 21:01 -
OK, I will post another question and try to clarify. This question plus the new one hopefully will help people see how to access the array and map it .Thanks again Mark. Very appreciated!Brad– Brad2017年01月07日 23:16:43 +00:00Commented Jan 7, 2017 at 23:16
-
posted new question about mapping the input here: arduino.stackexchange.com/questions/33224/…Brad– Brad2017年01月10日 16:08:34 +00:00Commented Jan 10, 2017 at 16:08
ArrayName[5]
(I can't be more specific than that without seeing the library). I have no idea howTinyPpmReader.detectedChannelNb == [5]
evaluates but[5]
by itself is unusual, if it even has any meaning, in C++. Did it even compile?