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:
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
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
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
Last edited: