Advertisement
igendel

Arduino<-Micro:bit Receiver and Tilt Display

Jan 20th, 2018
2,675
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. // Share and enjoy!
  2.  
  3. #include <SPI.h>
  4. #include "nRF24L01.h"
  5. #include "RF24.h"
  6. #include <Adafruit_NeoPixel.h>
  7.  
  8. #define PIXEL_PIN 6U
  9. #define NUMPIXELS 16U
  10.  
  11. Adafruit_NeoPixel pixels =
  12.   Adafruit_NeoPixel(NUMPIXELS, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
  13.  
  14. // The Micro:bit's transmission channel
  15. #define CH 2U
  16.  
  17. // Set up nRF24L01 radio on SPI bus plus pins 7 & 8
  18. RF24 radio(7,8);
  19.  
  20. uint8_t n, num = 0;
  21. uint8_t currCarr, lastCarr = 0;
  22. uint8_t sampleCount = 0;
  23. uint8_t currBit = 0;
  24. uint8_t incomingByte = 0;
  25.  
  26. //=========================================================
  27. void setup(void) {
  28.  
  29.   Serial.begin(115200);
  30.   radio.begin();
  31.   radio.setAutoAck(false);  
  32.   //radio.setPALevel(RF24_PA_HIGH);
  33.   radio.setChannel(CH);
  34.  
  35.   pixels.begin();
  36.  
  37. }
  38.  
  39. //=========================================================
  40. bool carrierPresent(void) {
  41.  
  42.   radio.startListening();
  43.   delayMicroseconds(100);
  44.   radio.stopListening();
  45.   return radio.testCarrier();
  46.  
  47. }
  48.  
  49. //=========================================================
  50. void displayAngle(const uint8_t raw) {
  51.  
  52.   uint8_t pStart, pStop, i;
  53.   uint8_t rawPerPixel = 256 / (NUMPIXELS / 2);
  54.   uint8_t midVal;
  55.  
  56.   for (i = 0; i <= NUMPIXELS; ++i) {
  57.     pixels.setPixelColor(i, 0, 0, 32);
  58.   }
  59.  
  60.   pStart = raw / rawPerPixel + 1;
  61.   pStop = pStart + (NUMPIXELS / 2) - 2;
  62.  
  63.   for (i = pStart; i <= pStop; ++i) {
  64.     pixels.setPixelColor(i, 32, 0, 0);
  65.   }
  66.  
  67.   midVal = pStart * rawPerPixel - raw;
  68.   pixels.setPixelColor(pStart - 1, midVal, 0, 32 - midVal);
  69.   pixels.setPixelColor(pStop + 1, 32 - midVal, 0, midVal);
  70.  
  71.   pixels.show();
  72.  
  73. }
  74.  
  75. //=========================================================
  76. void loop(void) {
  77.      
  78.       currCarr = carrierPresent();
  79.  
  80.       // Did we spot a change?
  81.       if (currCarr != lastCarr) {
  82.  
  83.         // Is it the end of a data bit?
  84.         if (currCarr) {
  85.  
  86.           // We ignore the first "bit" because it was just the "Start" signal
  87.           if (!currBit) {
  88.             currBit = 1;          
  89.           } else {
  90.  
  91.               // A Legit Bit!
  92.               incomingByte <<= 1;
  93.               if (sampleCount >= 10) {
  94.                   incomingByte += 1;
  95.               }
  96.  
  97.               ++currBit;
  98.               if (currBit == 9) {
  99.              
  100.                   currBit = 0;
  101.                   Serial.println(incomingByte);
  102.                   displayAngle(incomingByte);  
  103.                   incomingByte = 0;
  104.              
  105.               }
  106.              
  107.             } // else
  108.          
  109.         }
  110.        
  111.         sampleCount = 0;
  112.         lastCarr = currCarr;
  113.  
  114.       } else {
  115.          
  116.           ++sampleCount;
  117.           if (sampleCount > 50) {
  118.             currBit = 0;
  119.             incomingByte = 0;
  120.           }
  121.          
  122.         }
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement