Skip to content
This repository was archived by the owner on Sep 6, 2023. It is now read-only.

Commit 2791fd7

Browse files
committed
added code for gamepad controller
1 parent 4e19618 commit 2791fd7

File tree

4 files changed

+203
-2
lines changed

4 files changed

+203
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ https://github.com/adafruit/Adafruit_Microbit and learn more about it here: htt
1919
3) Install the arduino-BLEPeripheral library by following instructions here:
2020
https://github.com/sandeepmistry/arduino-BLEPeripheral
2121
4) Install the NewPing Library: https://github.com/eliteio/Arduino_New_Ping
22+
5) Install the NRF51 Libary: https://github.com/tipih/NRF51_Radio_library
2223

2324
Experimental, but useful:
2425
Sparkfun's Mag3110 compass library - https://github.com/sparkfun/SparkFun_MAG3110_Breakout_Board_Arduino_Library
@@ -34,7 +35,7 @@ The Maqueen robot has 4 Neopixels on PIN 15, currently this is not supported in
3435

3536
The IR remote sensor works, but not as well as it could. I am using a very old method to read the IR data, using pulseIn. This works, but is limited.
3637

37-
The biggest issue I've run across so far is no peer-to-peer support in the Arduino IDE. I am not even sure how it works. Again this is a non-issue with Makecode.
38+
---The biggest issue I've run across so far is no peer-to-peer support in the Arduino IDE. I am not even sure how it works. Again this is a non-issue with Makecode.---
3839
BLE works, and Adafruit does provide an app for Android phones that support BLE. I was able to setup remote control via that app, and using one of Adafruits base sketches.
3940
https://play.google.com/store/apps/details?id=com.adafruit.bluefruit.le.connect
4041
There is probably an app for IOS as well, but I don't own a IOS device.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 your soft device to S110
28+
29+
#include <Maqueen.h>
30+
#include "NRF51_Radio_library.h" //https://github.com/tipih/NRF51_Radio_library
31+
32+
Maqueen bot;
33+
Adafruit_Microbit_Matrix microbit;
34+
NRF51_Radio MicrobitRadio = NRF51_Radio();
35+
36+
static long currentMillis;
37+
const long interval = 5000;
38+
FrameBuffer *myDataSendData; //FrameBuffer for sending data to another device
39+
FrameBuffer* myData;
40+
41+
void setup() {
42+
Serial.begin(115200);
43+
bot.begin();
44+
Serial.println("Gamepad Control demo ready!");
45+
microbit.begin();
46+
MicrobitRadio.enable();
47+
MicrobitRadio.setGroup(10);
48+
MicrobitRadio.setFrequencyBand(50);
49+
Serial.println("Radio running");
50+
microbit.clear();
51+
microbit.show(smile_bmp);
52+
}
53+
54+
void loop() {
55+
56+
//Check if there is any data in the buffer
57+
FrameBuffer* myData = MicrobitRadio.recv();
58+
if (myData != NULL) {
59+
Serial.print(myData->length);
60+
Serial.print(" ");
61+
Serial.print(myData->version);
62+
Serial.print(" ");
63+
Serial.print(myData->group);
64+
Serial.print(" ");
65+
Serial.println(myData->protocol);
66+
//Serial.print(" ");
67+
//Serial.print(myData->payload[10]);
68+
//Serial.println(" ");
69+
//Serial.println(MicrobitRadio.dataReady());
70+
71+
if (myData->protocol == 0) {bot.stop();}
72+
if (myData->protocol == 1) {bot.setSpeed(50); bot.forward(); }
73+
if (myData->protocol == 2) {bot.setSpeed(50); bot.backward(); }
74+
if (myData->protocol == 3) {bot.setSpeed(50); bot.spinLeft(); }
75+
if (myData->protocol == 4) {bot.setSpeed(50); bot.spinRight(); }
76+
if (myData->protocol == 5) {bot.stop(); /*Button X is pushed*/}
77+
if (myData->protocol == 6) {bot.stop(); /*Button Y is pushed*/}
78+
if (myData->protocol == 7) {bot.stop(); /*Button A is pushed*/}
79+
if (myData->protocol == 8) {bot.stop(); /*Button B is pushed*/}
80+
delete myData; //Remember to delete it
81+
}
82+
83+
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=micro Maqueen
2-
version=1.0.1
2+
version=1.1.0
33
author=LeRoy Miller
44
maintainer=LeRoy Miller <[email protected]>
55
sentence=Simple library for the DF Robot micro:Maqueen BBC Micro:Bit Robot

0 commit comments

Comments
 (0)