SBUS library - not reading analog pin

Status
Not open for further replies.

Geekness

Well-known member
SBUS library - channel value not changing with analog input

I Have this code working on my teensy3.2, but my analog input does nothing.
When I view the serial monitor I only get these values bouncing back and forth:
CH01: 1807
CH01: 172
CH01: 1807
CH01: 172
CH01: 1808


Here's my code, I know its not the way Brian (brtaylor) has suggested to use analog inputs to generate the sbus signal (Link here), but I also need to use digital inputs mixed in with my analog inputs, and im not sure how to keep all my channels in order, so im trying it this way.

Please note that the void loop "heartbeat" is just there so I can see that the teensy is actually operating

Code:
#include <TimerOne.h>
#include "SBUS.h"

// output SBus on serial port 1
SBUS Transmit(Serial1);

//CH01: Roll (pin [A13])
uint16_t CH01 = A13;
int CH01Value = 0;

const int ledPin = 13; //just a heartbeat to show that the code is going

void setup() {
    pinMode(ledPin, OUTPUT);

  // serial to display the channel commands for debugging
  Serial.begin(115200);

  // begin the SBUS communication
  Transmit.begin();

  // setup the analog read resolution to 16 bits
  analogReadResolution(16);

  // setup an interrupt to send packets every 9 ms
  Timer1.initialize(9000);
  Timer1.attachInterrupt(sendSBUS);
}

void loop() {
 //heartbeat timing
  digitalWrite(ledPin, HIGH);  // set the LED on
  delay(200);                  // wait for a 0.2 seconds
  digitalWrite(ledPin, LOW);   // set the LED on
  delay(100);                  // wait for a 0.1 seconds
  digitalWrite(ledPin, HIGH);  // set the LED on
  delay(200);                  // wait for a 0.2 seconds
  digitalWrite(ledPin, LOW);   // set the LED off
  delay(1000);                 // wait for a second
}

// reads analog and digital inputs and sends an SBUS packet
void sendSBUS() {
  float scaleFactor = 1639.0f / 65535.0f;
  float bias = 172.0f;
  uint16_t channels[16];
  
	CH01 = analogRead(CH01); // read the analog input
    CH01Value = (uint16_t)(((float)CH01) * scaleFactor + bias); // linearly map the analog measurements (0-65535) to the SBUS commands (172-1811)
    Serial.print("CH01: ");
    Serial.print(CH01Value); // print the channel command (172-1811)
    Serial.print("\t");

    Serial.println();

  // write the SBUS packet to an SBUS compatible servo
    Transmit.write(&channels[0]);
}

My end goal is to output 15 sbus channels, made up of 10 analog inputs and 10 digital inputs.
The order of those inputs is not all analog then digital, they are all mixed in together.
Within that 10 digital inputs I have 6 momentary switches that are mapped to a single channel (Channel 5). I want to be able to output a predefined value dependent on which switch is pressed, and maintain that value until another switch is pressed.


Im still a noob at coding anything (as my code shows), but I started this project a long time ago, and decided to pick it back up myself over this holiday period.
There's obviously a number of ways to read the inputs and generate the sbus output, and I'd like to do it the most efficient way so any suggestions will be appreciated.

Here's a pic of what im basically trying to do
Concept layout.jpg
 
Last edited:
I should also note that when I get my inputs working correctly, I then need to loop the sbus output into the teensy3.2 Rx, and light a number of LEDs dependent on the value of one of the channels.
I know it would be easier to just read the input switch or pot that created the channel value in the first place, but I want to make sure that the actual output value is correct for the actual switch I pressed.
 
I believe you got more than one issue with your CODE here.
Correct me if i'm wrong but unless something changed in the newer Teensyduino software add on for the Arduino, the Teensy 3.X and Teensy LC pins default to a tri-state logic (high impedance, disabled).
It looks like youre missing pinMode(A13,INPUT) in setup().

Now is the CH01 variable to store the value read or pin declaration ?
CH01 = analogRead(CH01);//read the analog input

Perhaps this should be change to:
CH01 = analogRead(A13);//read the analog input
 
I believe you got more than one issue with your CODE here.
Correct me if i'm wrong but unless something changed in the newer Teensyduino software add on for the Arduino, the Teensy 3.X and Teensy LC pins default to a tri-state logic (high impedance, disabled).
It looks like youre missing pinMode(A13,INPUT) in setup().
I didn't realise I needed to do that for inputs. Will add it in.

Now is the CH01 variable to store the value read or pin declaration ?
CH01 = analogRead(CH01);//read the analog input

Perhaps this should be change to:
CH01 = analogRead(A13);//read the analog input

Then what's the point in declaring what CH01 is before the setup()
Code:
uint16_t CH01 = A13;
int CH01Value = 0;
 
When you
uint16_t CH01 = A13;
you are storing a value that internally points to the port and pin for A13 as defined by various lookup tables
if you then
CH01 = analogRead(CH01);//read the analog input
the value in CH01 that points to a pin gets overwritten with another unknown value from the input, which you can use the first time ok
The next time you get there though the analogRead will now read some other pin, based on what the analog input value was last time.
 
When you
uint16_t CH01 = A13;
you are storing a value that internally points to the port and pin for A13 as defined by various lookup tables
if you then
CH01 = analogRead(CH01);//read the analog input
the value in CH01 that points to a pin gets overwritten with another unknown value from the input, which you can use the first time ok
The next time you get there though the analogRead will now read some other pin, based on what the analog input value was last time.

Understood, thanks heaps.
Im at work right now so I can't rework the code just yet, but will try it tonight.
Thankyou.
 
Status
Not open for further replies.
Back
Top