Advertisement
SpidiAndi

Arduino uno + flysky fs-i6x + ssd1306

Nov 9th, 2024
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 2.49 KB | Source Code | 0 0
  1. #include <SPI.h>
  2. #include <Wire.h>
  3. #include <Adafruit_GFX.h>
  4. #include <Adafruit_SSD1306.h>
  5.  
  6. #define CH1 3
  7. #define CH2 5
  8. #define CH3 6
  9. #define CH4 9
  10. #define CH5 10
  11. #define CH6 11
  12. #define SCREEN_WIDTH 128
  13. #define SCREEN_HEIGHT 64
  14. #define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
  15. #define SCREEN_ADDRESS 0x3C
  16. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  17.  
  18. // Integers to represent values from sticks and pots
  19. int ch1Value;
  20. int ch2Value;
  21. int ch3Value;
  22. int ch4Value;
  23. int ch5Value;
  24.  
  25. // Boolean to represent switch value
  26. bool ch6Value;
  27.  
  28. int readChannel(int channelInput, int minLimit, int maxLimit, int defaultValue) {
  29.   int ch = pulseIn(channelInput, HIGH, 30000);
  30.   if (ch < 100) return defaultValue;
  31.   return map(ch, 1000, 2000, minLimit, maxLimit);
  32. }
  33.  
  34. bool readSwitch(byte channelInput, bool defaultValue) {
  35.   int intDefaultValue = (defaultValue) ? 100 : 0;
  36.   int ch = readChannel(channelInput, 0, 100, intDefaultValue);
  37.   return (ch > 50);
  38. }
  39.  
  40. void setup() {
  41.   Serial.begin(9600);
  42. pinMode(A0, INPUT);
  43.  
  44.   // Initialize SSD1306 display
  45.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  46.  
  47.  
  48.   display.clearDisplay();
  49.  
  50.   display.setTextColor(SSD1306_WHITE);
  51. }
  52.  
  53. void loop() {
  54.   ch1Value = readChannel(CH1, -100, 100, 0);
  55.   ch2Value = readChannel(CH2, -100, 100, 0);
  56.   ch3Value = readChannel(CH3, -100, 100, -100);
  57.   ch4Value = readChannel(CH4, -100, 100, 0);
  58.   ch5Value = readChannel(CH5, -100, 100, 0);
  59.  
  60.   ch6Value = readSwitch(CH6, false);
  61.  
  62.   display.clearDisplay();
  63.  
  64.  
  65.  
  66.   // Przykład: Wyświetlanie wartości w kształcie słupków
  67.   // Przekształć ch1Value i ch2Value do zakresu od 0 do 100
  68.   int ch1BarLength = map(ch1Value, -100, 100, 0, 100);
  69.   int ch2BarLength = map(ch2Value, -100, 100, 0, 100);
  70.   int ch3BarLength = map(ch3Value, -100, 100, 0, 100);
  71.   int ch4BarLength = map(ch4Value, -100, 100, 0, 100);
  72.   int ch5BarLength = map(ch5Value, -100, 100, 0, 100);
  73.  
  74.  
  75.   // Wyświetl paski
  76.   display.drawRect(0, 0, ch1BarLength, 4, SSD1306_WHITE);
  77.   display.drawRect(0, 10, ch2BarLength, 4, SSD1306_WHITE);
  78.   display.drawRect(0, 20, ch3BarLength, 4, SSD1306_WHITE);
  79.   display.drawRect(0, 30, ch4BarLength, 4, SSD1306_WHITE);
  80.   display.drawRect(0, 40, ch5BarLength, 4, SSD1306_WHITE);
  81.  
  82.  
  83.   // Przykład: Dodawanie tekstu
  84.   display.setCursor(0, 50);
  85.   display.setTextSize(1);
  86.   display.setTextColor(SSD1306_WHITE);
  87.   display.println("Switch: " + String(ch6Value));
  88.  
  89.   display.display();
  90.  
  91.   delay(500);
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement