|
| 1 | +/* |
| 2 | +Copyright (c) 2020 LeRoy Miller |
| 3 | +
|
| 4 | + This program is free software: you can redistribute it and/or modify |
| 5 | + it under the terms of the GNU General Public License as published by |
| 6 | + the Free Software Foundation, either version 3 of the License, or |
| 7 | + (at your option) any later version. |
| 8 | +
|
| 9 | + This program is distributed in the hope that it will be useful, |
| 10 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + GNU General Public License for more details. |
| 13 | +
|
| 14 | + You should have received a copy of the GNU General Public License |
| 15 | + along with this program. If not, see <http://www.gnu.org/licenses> |
| 16 | +
|
| 17 | +If you find this or any of my projects useful or enjoyable please support me. |
| 18 | +Anything I do get goes to buy more parts and make more/better projects. |
| 19 | +https://www.patreon.com/kd8bxp |
| 20 | +https://ko-fi.com/lfmiller |
| 21 | +
|
| 22 | +https://github.com/kd8bxp |
| 23 | +https://www.youtube.com/channel/UCP6Vh4hfyJF288MTaRAF36w |
| 24 | +https://kd8bxp.blogspot.com/ |
| 25 | +*/ |
| 26 | + |
| 27 | +//Remember to set the soft device to S110 |
| 28 | + |
| 29 | +#include "NRF51_Radio_library.h" //https://github.com/tipih/NRF51_Radio_library |
| 30 | +#include <Adafruit_Microbit.h> |
| 31 | + |
| 32 | +#define DOWN 13 |
| 33 | +#define LEFT 14 |
| 34 | +#define UP 8 |
| 35 | +#define RIGHT 15 |
| 36 | +#define XBTN 1 |
| 37 | +#define YBTN 2 |
| 38 | +#define LED 16 |
| 39 | +#define BUZZER 0 |
| 40 | +#define VIBRATE 12 |
| 41 | +#define ABTN 5 |
| 42 | +#define BBTN 11 |
| 43 | + |
| 44 | +Adafruit_Microbit_Matrix microbit; //We only use this library to get easy access to the MATRIX LED, to sad you need to set the soft device to S110, even though we do not use it |
| 45 | + //Should be easy to fix, by removing the ble class of the in the library |
| 46 | +NRF51_Radio MicrobitRadio = NRF51_Radio(); //Let's get a new instance of the Radio |
| 47 | + |
| 48 | + |
| 49 | +FrameBuffer *myDataSendData; //FrameBuffer for sending data to another device |
| 50 | +FrameBuffer* myData; |
| 51 | + |
| 52 | +static long currentMillis; //Var to store the time gone since last time |
| 53 | +const long interval = 5000; //Wait time before sending |
| 54 | +const long send_interval = 200; //In state send, after start we send out at 1 sec interval, ontil we start receiving something |
| 55 | + |
| 56 | +void setup() { |
| 57 | + Serial.begin(115200); |
| 58 | + microbit.begin(); |
| 59 | + MicrobitRadio.enable(); |
| 60 | + MicrobitRadio.setGroup(10); |
| 61 | + MicrobitRadio.setFrequencyBand(50); |
| 62 | +pinMode (UP, INPUT); |
| 63 | +pinMode (DOWN, INPUT); |
| 64 | +pinMode (LEFT, INPUT); |
| 65 | +pinMode (RIGHT,INPUT); |
| 66 | +pinMode (ABTN, INPUT); |
| 67 | +pinMode (BBTN, INPUT); |
| 68 | +pinMode (XBTN, INPUT); |
| 69 | +pinMode (YBTN, INPUT); |
| 70 | +pinMode (LED, OUTPUT); |
| 71 | +pinMode (BUZZER, OUTPUT); |
| 72 | +pinMode (VIBRATE, OUTPUT); |
| 73 | +//digitalWrite(IR, HIGH); |
| 74 | +analogWrite(BUZZER, 2055); |
| 75 | +digitalWrite(VIBRATE, HIGH); |
| 76 | +delay(100); |
| 77 | +analogWrite(BUZZER, 0); |
| 78 | +digitalWrite(VIBRATE, LOW); |
| 79 | + |
| 80 | + |
| 81 | + myDataSendData = new FrameBuffer(); |
| 82 | + currentMillis = millis(); |
| 83 | +} |
| 84 | + |
| 85 | +void loop() { |
| 86 | + |
| 87 | +digitalWrite(LED,HIGH); |
| 88 | +if (digitalRead(UP) == 0) {microbit.print("U"); myDataSendData->protocol=1; radio(); } else |
| 89 | +if (digitalRead(DOWN) == 0) {microbit.print("D"); myDataSendData->protocol=2; radio();} else |
| 90 | +if (digitalRead(LEFT) == 0) {microbit.print("L"); myDataSendData->protocol=3; radio();} else |
| 91 | +if (digitalRead(RIGHT) == 0) {microbit.print("R"); myDataSendData->protocol=4; radio();} else |
| 92 | +if (digitalRead(XBTN) == 0) {microbit.print("X"); myDataSendData->protocol=5; radio(); } else |
| 93 | +if (digitalRead(YBTN) == 0) {microbit.print("Y"); myDataSendData->protocol=6; radio(); } else |
| 94 | +if (digitalRead(ABTN) == 0) {microbit.print("A"); myDataSendData->protocol=7; radio();} else |
| 95 | +if (digitalRead(BBTN) == 0) {microbit.print("B"); myDataSendData->protocol=8; radio();} else |
| 96 | + { myDataSendData->protocol=0; radio(); } |
| 97 | +delay(250); |
| 98 | +digitalWrite(LED, LOW); |
| 99 | +delay(250); |
| 100 | +microbit.print(" "); |
| 101 | + |
| 102 | +} |
| 103 | + |
| 104 | +void radio() { |
| 105 | + |
| 106 | + if (millis() - currentMillis >= send_interval) { |
| 107 | + |
| 108 | + currentMillis = millis(); //so we start to send, and then wait for a ack. |
| 109 | + |
| 110 | + myDataSendData->length = 3; |
| 111 | + myDataSendData->group = 2; //(1=ACK 2=SEND) |
| 112 | + myDataSendData->version = 10; |
| 113 | + |
| 114 | + MicrobitRadio.send(myDataSendData); |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments