0

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?

asked Jan 10, 2017 at 16:07
2
  • int PPM_INPUT_PIN = TinyPpmReader.width_us(3); contain a reading result, when followed by val0 = analogRead(PPM_INPUT_PIN); it's clearly absurd. Commented Jan 11, 2017 at 4:25
  • Yes, this is where my problem is. I am trying to understand this. Commented Jan 11, 2017 at 14:58

1 Answer 1

2
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 
} 
answered Jan 11, 2017 at 15:28
9
  • 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. Commented Jan 11, 2017 at 15:43
  • This does in fact compile but does not work. Commented Jan 11, 2017 at 16:12
  • sorry, there's a mistake in my code. It should be moveServo(PANSERVO,mappedval0);. I've edit the answer Commented 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. Commented 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)? Commented Jan 11, 2017 at 16:41

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.