Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Share and enjoy!
- #include <SPI.h>
- #include "nRF24L01.h"
- #include "RF24.h"
- #include <Adafruit_NeoPixel.h>
- #define PIXEL_PIN 6U
- #define NUMPIXELS 16U
- Adafruit_NeoPixel pixels =
- Adafruit_NeoPixel(NUMPIXELS, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
- // The Micro:bit's transmission channel
- #define CH 2U
- // Set up nRF24L01 radio on SPI bus plus pins 7 & 8
- RF24 radio(7,8);
- uint8_t n, num = 0;
- uint8_t currCarr, lastCarr = 0;
- uint8_t sampleCount = 0;
- uint8_t currBit = 0;
- uint8_t incomingByte = 0;
- //=========================================================
- void setup(void) {
- Serial.begin(115200);
- radio.begin();
- radio.setAutoAck(false);
- //radio.setPALevel(RF24_PA_HIGH);
- radio.setChannel(CH);
- pixels.begin();
- }
- //=========================================================
- bool carrierPresent(void) {
- radio.startListening();
- delayMicroseconds(100);
- radio.stopListening();
- return radio.testCarrier();
- }
- //=========================================================
- void displayAngle(const uint8_t raw) {
- uint8_t pStart, pStop, i;
- uint8_t rawPerPixel = 256 / (NUMPIXELS / 2);
- uint8_t midVal;
- for (i = 0; i <= NUMPIXELS; ++i) {
- pixels.setPixelColor(i, 0, 0, 32);
- }
- pStart = raw / rawPerPixel + 1;
- pStop = pStart + (NUMPIXELS / 2) - 2;
- for (i = pStart; i <= pStop; ++i) {
- pixels.setPixelColor(i, 32, 0, 0);
- }
- midVal = pStart * rawPerPixel - raw;
- pixels.setPixelColor(pStart - 1, midVal, 0, 32 - midVal);
- pixels.setPixelColor(pStop + 1, 32 - midVal, 0, midVal);
- pixels.show();
- }
- //=========================================================
- void loop(void) {
- currCarr = carrierPresent();
- // Did we spot a change?
- if (currCarr != lastCarr) {
- // Is it the end of a data bit?
- if (currCarr) {
- // We ignore the first "bit" because it was just the "Start" signal
- if (!currBit) {
- currBit = 1;
- } else {
- // A Legit Bit!
- incomingByte <<= 1;
- if (sampleCount >= 10) {
- incomingByte += 1;
- }
- ++currBit;
- if (currBit == 9) {
- currBit = 0;
- Serial.println(incomingByte);
- displayAngle(incomingByte);
- incomingByte = 0;
- }
- } // else
- }
- sampleCount = 0;
- lastCarr = currCarr;
- } else {
- ++sampleCount;
- if (sampleCount > 50) {
- currBit = 0;
- incomingByte = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement