Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eca3f8e
commit a8da89d
Showing
2 changed files
with
234 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include <SPI.h> | ||
#include <Mirf.h> | ||
#include <nRF24L01.h> | ||
#include <MirfHardwareSpiDriver.h> | ||
|
||
unsigned long rfdata; | ||
String name = ""; | ||
boolean nameEntered = false; | ||
|
||
short int ENGINE_1 = 1; | ||
short int ENGINE_2 = 2; | ||
short int FORWARD = 1; | ||
short int BACKWARD = 2; | ||
|
||
|
||
void setup(){ | ||
Serial.begin(9600); | ||
Mirf.spi = &MirfHardwareSpi; | ||
Mirf.init(); | ||
Mirf.setRADDR((byte *)"clie1"); | ||
Mirf.payload = sizeof(rfdata); | ||
Mirf.config(); | ||
Serial.println("Beginning ... "); | ||
} | ||
|
||
void loop(){ | ||
// getting a value from the Serial | ||
while (Serial.available()) { | ||
char readChar = (char)Serial.read(); | ||
// If the next character is a linefeed (enter) | ||
// the name is complete. Exit the while() loop. | ||
if (readChar == '\n') { | ||
nameEntered = true; | ||
continue; | ||
} | ||
// If the character wasn't enter, add it to the name string. | ||
name += readChar; | ||
} | ||
|
||
// if we got a value, send it through NRF | ||
if (name != "") { | ||
unsigned long time = millis(); | ||
|
||
// going forward on engine 1 with 124 power | ||
rfdata = ENGINE_1 << 32 | FORWARD << 16 | name.toInt(); | ||
|
||
Mirf.setTADDR((byte *)"serv1"); | ||
|
||
Mirf.send((byte *)&rfdata); | ||
while(Mirf.isSending()){ | ||
} | ||
delay(10); | ||
while(!Mirf.dataReady()){ | ||
//Serial.println("Waiting"); | ||
if ( ( millis() - time ) > 1000 ) { | ||
Serial.println("Timeout on response from server!"); | ||
return; | ||
} | ||
} | ||
|
||
Mirf.getData((byte *) &rfdata); | ||
Serial.println(rfdata); | ||
} | ||
name = ""; | ||
delay(1000); | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
/** | ||
* An Mirf example which copies back the data it recives. | ||
* | ||
* Pins: | ||
* Hardware SPI: | ||
* MISO -> 12 | ||
* MOSI -> 11 | ||
* SCK -> 13 | ||
* | ||
* Configurable: | ||
* CE -> 8 | ||
* CSN -> 7 | ||
* | ||
*/ | ||
|
||
#include <SPI.h> | ||
#include <Mirf.h> | ||
#include <nRF24L01.h> | ||
#include <MirfHardwareSpiDriver.h> | ||
|
||
|
||
|
||
int IN1 = 4; | ||
int IN2 = 5; | ||
int IN3 = 6; | ||
int IN4 = 9; | ||
int ENA = 3; | ||
int ENB = 1; | ||
|
||
unsigned long rfdata; | ||
|
||
|
||
// qty 0-255 | ||
void engine1(boolean forward, int speed) { | ||
if (forward) { | ||
digitalWrite(IN1, HIGH); | ||
digitalWrite(IN2, LOW); | ||
} | ||
else { | ||
digitalWrite(IN1, LOW); | ||
digitalWrite(IN2, HIGH); | ||
} | ||
analogWrite(ENA, speed); | ||
} | ||
|
||
void engine2(boolean forward, int speed) { | ||
if (forward) { | ||
digitalWrite(IN3, HIGH); | ||
digitalWrite(IN4, LOW); | ||
} | ||
else { | ||
digitalWrite(IN3, LOW); | ||
digitalWrite(IN4, HIGH); | ||
} | ||
analogWrite(ENB, speed); | ||
} | ||
|
||
|
||
void setup(){ | ||
Serial.begin(9600); | ||
|
||
// motorshield pins | ||
pinMode(ENB, OUTPUT); // sets the pin as output | ||
pinMode(IN3, OUTPUT); // sets the pin as output | ||
pinMode(IN4, OUTPUT); // sets the pin as output | ||
pinMode(IN1, OUTPUT); // sets the pin as output | ||
pinMode(IN2, OUTPUT); // sets the pin as output | ||
pinMode(ENA, OUTPUT); // sets the pin as output | ||
|
||
/* | ||
* Set the SPI Driver. | ||
*/ | ||
|
||
Mirf.spi = &MirfHardwareSpi; | ||
|
||
/* | ||
* Setup pins / SPI. | ||
*/ | ||
|
||
Mirf.init(); | ||
|
||
/* | ||
* Configure reciving address. | ||
*/ | ||
|
||
Mirf.setRADDR((byte *)"serv1"); | ||
|
||
/* | ||
* Set the payload length to sizeof(unsigned long) the | ||
* return type of millis(). | ||
* | ||
* NB: payload on client and server must be the same. | ||
*/ | ||
|
||
Mirf.payload = sizeof(rfdata); | ||
|
||
/* | ||
* Write channel and payload config then power up reciver. | ||
*/ | ||
|
||
Mirf.config(); | ||
|
||
Serial.println("Listening..."); | ||
} | ||
|
||
void loop(){ | ||
/* | ||
* A buffer to store the data. | ||
*/ | ||
|
||
byte data[Mirf.payload]; | ||
|
||
/* | ||
* If a packet has been recived. | ||
* | ||
* isSending also restores listening mode when it | ||
* transitions from true to false. | ||
*/ | ||
|
||
if(!Mirf.isSending() && Mirf.dataReady()){ | ||
Serial.println("Got packet"); | ||
|
||
/* | ||
* Get load the packet into the buffer. | ||
*/ | ||
|
||
Mirf.getData(data); | ||
|
||
rfdata = data[0]; | ||
short int engine = rfdata >> 32; | ||
short int direction = rfdata >> 16 & 0xFFFFF; | ||
short int speed = rfdata & 0xFFFFF; | ||
boolean forward = direction == 1; | ||
|
||
|
||
if (engine == 1) { | ||
engine1(forward, speed); | ||
} | ||
else { | ||
engine2(forward, speed); | ||
|
||
} | ||
|
||
/* | ||
* Set the send address. | ||
*/ | ||
|
||
|
||
Mirf.setTADDR((byte *)"clie1"); | ||
|
||
/* | ||
* Send the data back to the client | ||
*/ | ||
|
||
Mirf.send(data); | ||
|
||
/* | ||
* Wait untill sending has finished | ||
* | ||
* NB: isSending returns the chip to receving after returning true. | ||
*/ | ||
|
||
Serial.println("Reply sent."); | ||
} | ||
} |