PROJECT 1: Arduino 3D printed RC
Transmitter Code
/*
* Khoa Au
* RC -Transmitter
*/
#include <SPI.h>
#include "RF24.h"
#include <Adafruit_GFX.h>
#include <gfxfont.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
static const unsigned char PROGMEM logo16_glcd_bmp[] =
{ B00000000, B11000000,
B00000001, B11000000,
B00000001, B11000000,
B00000011, B11100000,
B11110011, B11100000,
B11111110, B11111000,
B01111110, B11111111,
B00110011, B10011111,
B00011111, B11111100,
B00001101, B01110000,
B00011011, B10100000,
B00111111, B11100000,
B00111111, B11110000,
B01111100, B11110000,
B01110000, B01110000,
B00000000, B00110000 };
#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
RF24 myRadio (7,8);
byte addresses[][6] = {"1Node"};
int package [5];
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
myRadio.begin(); // Start up the physical nRF24L01 Radio
myRadio.setChannel(108); // Above most Wifi Channels
// Set the PA Level low to prevent power supply related issues since this is a
// getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
// myRadio.setPALevel(RF24_PA_MIN);
myRadio.setPALevel(RF24_PA_MAX); // Uncomment for more power
myRadio.setDataRate(RF24_250KBPS);
myRadio.openWritingPipe( addresses[0]);
// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32)
// init done
// Show image buffer on the display hardware.
// Since the buffer is intialized with an Adafruit splashscreen
// internally, this will display the splashscreen.
display.display();
delay(2000);
// Clear the buffer.
display.clearDisplay();
// draw a single pixel
display.drawPixel(10, 10, WHITE);
// Show the display buffer on the hardware.
// NOTE: You _must_ call display after making any drawing commands
// to make them visible on the display hardware!
display.display();
delay(2000);
display.clearDisplay();
}
/*
* ***************************************************************************
* Hardware Initializations Section
*/
void ini(){
digitalWrite(6,HIGH);
package[0] = analogRead(A3);//scaling down variables
package[1] = analogRead(A0);
package[2] = digitalRead(6);
//a pin for LCD display
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(10,0);
//turning on OLED
//turnDisplay();
//backwards or forwards on OLED
//directionDisplay();
//batteryLevelDisplay();
//operationModeDisplay();
myRadio.write( &package, sizeof(package) );//Transmit
display.display();
display.clearDisplay();
}
void loop() {
// put your main code here, to run repeatedly:
ini();
Serial.print(package[0]);
Serial.print(", ");
Serial.print(package[1]);
Serial.print(", ");
Serial.println(package[2]);
//
}
/*
* *********************************************************************************
* OLED DISPLAY SECTION
*/
void turnDisplay(){
display.drawTriangle(5,6,15,2,15,10, WHITE);//Left Triangle
display.drawTriangle(25, 2,35, 6,25, 10, WHITE);//Right Triangle
display.drawRect(17,2,6,9,WHITE);//neutral position
if (package[0] <300){
display.fillTriangle(25, 2,35, 6,25, 10, WHITE);
}else if (package[0] >800){
display.fillTriangle(5,6,15,2,15,10, WHITE);
}else {
display.fillRect(17,2,6,9,WHITE);//neutral position)
}
display.print (" ");}
void directionDisplay(){
if (package[1] <70){
display.println("S");
}else if (package[1] <400){
display.println("D");
}else if (package[1] >800){
display.println("R");
}else {
display.println ("N");
}
}
void batteryLevelDisplay(){
display.println("");
display.print("Batt: ");
float batt = analogRead(A7); //reading battery level
float slope = 0.4878;// corresponding values of 4V and 6V analog
float yIntersect = 374.146;
float battPercentage = (batt*slope) - yIntersect;
display.print((int)battPercentage);
display.println("%");
//display.print(batt);
int x_coord =80;
int shouldFillRect = 30;
for (int i = 0; i<3;i++){
display.drawRect(x_coord,17,7,5,WHITE);
if(battPercentage < shouldFillRect){// don't fill this rectangle
}else {
display.fillRect(x_coord,17,7,5,WHITE);
}
shouldFillRect += 30;
x_coord+=6;
}
display.drawRect(98,18.5,3,3,WHITE);
}
void operationModeDisplay(){
display.print ("MODE :");
if(package[2] == HIGH){
display.println("ROBOT");
}else {
display.println ("DRIVE");
}
}
void testdrawtriangle(void) {
}
void testfilltriangle(void) {
}
The Transmitter code
/*
* Khoa Au
* RC-Reciever
*/
#include <Servo.h>
#include <SPI.h> // Comes with Arduino IDE
#include "RF24.h" // Download and Install (See above)
#define enA 5
Servo myServo;
Servo lServo;
Servo rServo;
RF24 myRadio (7, 8);
/*-----( Declare Variables )-----*/
byte addresses[][6] = {"1Node"};
int dataReceived [4];
void setup() {
// put your setup code here, to run once:
//myServo.write(80);
pinMode(6 ,OUTPUT);
myServo.attach(9);
lServo.attach (2);
rServo.attach (3);
// Use the serial Monitor (Symbol on far right). Set speed to 115200 (Bottom Right)
Serial.begin(115200);
Serial.println("hello");
myRadio.begin(); // Start up the physical nRF24L01 Radio
myRadio.setChannel(108); // Above most Wifi Channels
//myRadio.setPALevel(RF24_PA_MIN);
myRadio.setPALevel(RF24_PA_MAX); // Uncomment for more power
myRadio.setDataRate(RF24_250KBPS);
myRadio.openReadingPipe(1, addresses[0]); // Use the first entry in array 'addresses' (Only 1 right now)
myRadio.startListening();
}
void ServoControl(){
if(dataReceived[0]>600){
myServo.write(170);
}else if (dataReceived[0]<400){
myServo.write(10);
}else{
myServo.write(90);
}
}
void motorControl(){
int spd = dataReceived[1];
if(spd>600){//forward direction, HIGH HIGH
analogWrite(enA,80);
digitalWrite(6,HIGH);
}else if(spd<400){//backward direction HIGH LOW
analogWrite(enA,80);
digitalWrite(6,LOW);
}else{
analogWrite(enA,LOW);
digitalWrite(6,LOW);
}
Serial.print(spd);
Serial.print(",");
}
//control the motor speed
void robotcontrol(){
int l = dataReceived[0];
int r = dataReceived[1];
//-------------------leftHand
if(l>600){
lServo.write(170);
}else if (dataReceived[0]<400){
lServo.write(30);
}else{
lServo.write(90);
}
//-------------------right hand
if(r>600){
rServo.write(170);
}else if (dataReceived[0]<400){
rServo.write(30);
}else{
rServo.write(90);
}
}
void loop() {
// put your main code here, to run repeatedly:
if ( myRadio.available()) // Check for incoming data from transmitter
{
while (myRadio.available()) // While there is data ready
{
myRadio.read( &dataReceived, sizeof(dataReceived) ); // Get the data payload (You must have defined that already!)
}
// DO something with the data, like print it
Serial.print("Data received = ");
Serial.print(dataReceived[0]);
Serial.print("," );
Serial.println(dataReceived [1]);
int mode = dataReceived[2];
// Serial.println(mode);
if(mode ==1){//drive mode
ServoControl();
motorControl();
Serial.print(digitalRead(2));
Serial.print(" ");
Serial.println(analogRead(enA));
}else
robotcontrol();
}
}
* Khoa Au
* RC-Reciever
*/
#include <Servo.h>
#include <SPI.h> // Comes with Arduino IDE
#include "RF24.h" // Download and Install (See above)
#define enA 5
Servo myServo;
Servo lServo;
Servo rServo;
RF24 myRadio (7, 8);
/*-----( Declare Variables )-----*/
byte addresses[][6] = {"1Node"};
int dataReceived [4];
void setup() {
// put your setup code here, to run once:
//myServo.write(80);
pinMode(6 ,OUTPUT);
myServo.attach(9);
lServo.attach (2);
rServo.attach (3);
// Use the serial Monitor (Symbol on far right). Set speed to 115200 (Bottom Right)
Serial.begin(115200);
Serial.println("hello");
myRadio.begin(); // Start up the physical nRF24L01 Radio
myRadio.setChannel(108); // Above most Wifi Channels
//myRadio.setPALevel(RF24_PA_MIN);
myRadio.setPALevel(RF24_PA_MAX); // Uncomment for more power
myRadio.setDataRate(RF24_250KBPS);
myRadio.openReadingPipe(1, addresses[0]); // Use the first entry in array 'addresses' (Only 1 right now)
myRadio.startListening();
}
void ServoControl(){
if(dataReceived[0]>600){
myServo.write(170);
}else if (dataReceived[0]<400){
myServo.write(10);
}else{
myServo.write(90);
}
}
void motorControl(){
int spd = dataReceived[1];
if(spd>600){//forward direction, HIGH HIGH
analogWrite(enA,80);
digitalWrite(6,HIGH);
}else if(spd<400){//backward direction HIGH LOW
analogWrite(enA,80);
digitalWrite(6,LOW);
}else{
analogWrite(enA,LOW);
digitalWrite(6,LOW);
}
Serial.print(spd);
Serial.print(",");
}
//control the motor speed
void robotcontrol(){
int l = dataReceived[0];
int r = dataReceived[1];
//-------------------leftHand
if(l>600){
lServo.write(170);
}else if (dataReceived[0]<400){
lServo.write(30);
}else{
lServo.write(90);
}
//-------------------right hand
if(r>600){
rServo.write(170);
}else if (dataReceived[0]<400){
rServo.write(30);
}else{
rServo.write(90);
}
}
void loop() {
// put your main code here, to run repeatedly:
if ( myRadio.available()) // Check for incoming data from transmitter
{
while (myRadio.available()) // While there is data ready
{
myRadio.read( &dataReceived, sizeof(dataReceived) ); // Get the data payload (You must have defined that already!)
}
// DO something with the data, like print it
Serial.print("Data received = ");
Serial.print(dataReceived[0]);
Serial.print("," );
Serial.println(dataReceived [1]);
int mode = dataReceived[2];
// Serial.println(mode);
if(mode ==1){//drive mode
ServoControl();
motorControl();
Serial.print(digitalRead(2));
Serial.print(" ");
Serial.println(analogRead(enA));
}else
robotcontrol();
}
}
No comments:
Post a Comment