#include <Arduino.h>
#define SBUS_PIN 2 // Replace with the actual pin you're using for SBUS input
#define NUM_SWITCHES 8
uint16_t channels[NUM_SWITCHES] = {1500}; // Initialize with neutral positions
bool switchStates[NUM_SWITCHES] = {false}; // Stores the state of each switch
void setup() {
// Serial.begin(9600);
attachInterrupt((SBUS_PIN), sbusInterrupt, FALLING);
}
void loop() {
// Your main loop code here
// Check switch states and perform actions based on their positions
for (int i = 0; i < NUM_SWITCHES; i++) {
if (switchStates[i]) {
// Switch is ON
// Perform corresponding action
digitalWrite(i, HIGH);
} else {
// Switch is OFF
// Perform corresponding action
// Example: digitalWrite(outputPin, LOW);
}
}
}
void sbusInterrupt() {
static uint8_t sbusData[25];
static uint8_t dataIndex = 0;
uint8_t sbusByte = digitalRead(SBUS_PIN);
if (dataIndex == 0 && sbusByte != 0x0F) {
return; // Invalid SBUS start byte
}
sbusData[dataIndex++] = sbusByte;
if (dataIndex == 25) {
// Process SBUS data
processSbusData(sbusData);
dataIndex = 0;
}
}
void processSbusData(uint8_t* data) {
// Extract channel values and switch positions from SBUS data
for (int i = 0; i < NUM_SWITCHES; i++) {
uint16_t value = (data[2 * i + 1] << 8 | data[2 * i + 2]) & 0x07FF;
channels[i] = map(value, 172, 1811, 1000, 2000); // Map to 1000-2000 range
switchStates[i] = (value > 1500); // Adjust the threshold based on your switch characteristics
}
}