Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
satsob
/
Robotite_ylesannete_projekt
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
a89f92d0
authored
Jan 13, 2021
by
satsob
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Uuendused
parent
9072634c
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
389 additions
and
0 deletions
Lõputöö ülesanded/1. BLE ühenduse loomine RF24 ja nutitelefoni vahel/nRF_Stuff/nRF_RobotControl.ino
Lõputöö ülesanded/5. Raja kaardistamine ekraanile/LCD_16x2_b3/LCD_16x2_b3.ino
Lõputöö ülesanded/1. BLE ühenduse loomine RF24 ja nutitelefoni vahel/nRF_Stuff/nRF_RobotControl.ino
0 → 100644
View file @
a89f92d0
/* Include libraries */
/* Include libraries */
#include <SPI.h>
#include <RF24.h>
#include <BTLE.h>
/* Include servo */
#include <Servo.h>
//#include <nRF24L01.h> // Maybe needed, commented out right now!
/* Define */
#define rightServo 5 // Define Servo right pin
#define leftServo 6 // Define Servo left pin
/* Global variables */
Servo
rightWheel
;
Servo
leftWheel
;
RF24
radio
(
9
,
10
);
// Create RF24 object to work with the RF24 library.
BTLE
btle
(
&
radio
);
// Create BTLE object to work with the BTLE library.
/* Replace! "CustomName" to the name of your device. Password - maximum length (14)! characters.
It's for recognizing the correct data packets. */
#define PASSWORD "Galaxy A70"
/* Define functions used for the robot to control */
/* Stop the robot */
void
setWheelsStop
()
{
rightWheel
.
writeMicroseconds
(
1500
);
leftWheel
.
writeMicroseconds
(
1500
);
}
/* Drive forward */
void
setWheelsForward
(){
rightWheel
.
writeMicroseconds
(
1300
);
leftWheel
.
writeMicroseconds
(
1700
);
}
/* Drive right */
void
setWheelsRight
(){
rightWheel
.
writeMicroseconds
(
1700
);
leftWheel
.
writeMicroseconds
(
1700
);
}
/* Drive left */
void
setWheelsLeft
(){
rightWheel
.
writeMicroseconds
(
1300
);
leftWheel
.
writeMicroseconds
(
1300
);
}
/* Drive backward */
void
setWheelsBackward
(){
rightWheel
.
writeMicroseconds
(
1700
);
leftWheel
.
writeMicroseconds
(
1300
);
}
void
setup
()
{
/* Start the Serial and btle. */
Serial
.
begin
(
9600
);
btle
.
begin
(
""
);
/* Attach servos to digital pins defined earlier */
rightWheel
.
attach
(
rightServo
);
leftWheel
.
attach
(
leftServo
);
/* Put servos to standstill */
//setWheelsStop();
}
void
loop
()
{
String
str
=
""
;
// For collecting a string of values from different types of variables
/* Start listening. If data is received it is stored in the "buffer".
Then the received data is decoded and checked (CRC). */
if
(
btle
.
listen
())
{
// Serial.print("Got payload: "); // If data is received, print "Got payload: "
/* Store the received data into the buffer */
for
(
uint8_t
i
=
2
;
i
<
(
btle
.
buffer
.
pl_size
)
-
6
;
i
++
)
{
str
+=
(
char
(
btle
.
buffer
.
payload
[
i
]));
// Serial.print(btle.buffer.payload[i],HEX); Serial.print(" "); // To receive data in HEX format
// Serial.print(char (btle.buffer.payload[i])); Serial.print(" "); // To receive data in char format
}
/* (Spam protection) only if string starts with "CustomName" */
if
(
str
.
startsWith
(
PASSWORD
))
{
str
.
replace
(
PASSWORD
,
""
);
// (remove / replace) PASSWORD
/* According to what data was sent, print the direction */
if
((
char
(
str
.
charAt
(
2
)))
==
7
)
{
Serial
.
println
(
"RIGHT"
);
setWheelsRight
();
delay
(
100
);
}
else
if
((
char
(
str
.
charAt
(
2
)))
==
6
)
{
Serial
.
println
(
"LEFT"
);
setWheelsLeft
();
delay
(
100
);
}
else
if
((
char
(
str
.
charAt
(
2
)))
==
5
)
{
Serial
.
println
(
"BACKWARD"
);
setWheelsBackward
();
delay
(
100
);
}
else
if
((
char
(
str
.
charAt
(
2
)))
==
4
)
{
Serial
.
println
(
"FORWARD"
);
setWheelsForward
();
delay
(
100
);
}
}
else
{
Serial
.
println
(
"STOP"
);
setWheelsStop
();
delay
(
100
);
}
}
/* Hop channels through 37-39 to scan for data transmissions */
btle
.
hopChannel
();
}
Lõputöö ülesanded/5. Raja kaardistamine ekraanile/LCD_16x2_b3/LCD_16x2_b3.ino
0 → 100644
View file @
a89f92d0
#include <LiquidCrystal_I2C.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
LiquidCrystal_I2C
lcd
(
0x3F
,
16
,
2
);
// Address found using lcd scanner, columns, rows
/*
Intialize for robot driving pathway.
Byte array to store the values for LCD. B or 0b. Equals one segment.
*/
byte
liigun
[
8
]
=
{
B00000
,
B00000
,
B00000
,
B00000
,
B00000
,
B00000
,
B00000
,
B00000
};
byte
temp
[
8
]
=
{
0
};
// To temporarily store liigun array
bool
sameArray
=
false
;
// To check whether to save temp array or not
int
sameCounter
=
0
;
// To check whether to save temp array or not. 1 - Store array
// 2 - Store it back to liigun array and reset the counter
/* LCD coordinates. To create points on LCD screen segments. */
int
pointx
=
0
;
// Can only be 0 or 1; sets x position on lcd
int
pointy
=
0
;
// Can be max 7; sets y postion on lcd
/* To store the values for moving on LCD screen. To move in array. */
int
counterb
=
7
;
// Initialize counterb as array[counterb]. 7 to start from the bottom, 0 from top.
int
cursorCounter
=
0
;
// Initialize cursorCounter that sets cursor point on LCD
int
pos
=
4
;
// Initialize pos (position) to calculate the correct postion
int
posx
=
0
;
// Starting x position and the position of x-axis segment, max 79
int
posy
=
8
;
// Starting y position and the position of y-axis segment, max 15
/*
Buttons initialization
*/
// constants won't change. They're used here to set pin numbers:
const
int
buttonPin1
=
5
;
// the number of the pushbutton pin
const
int
buttonPin2
=
6
;
// the number of the pushbutton pin
const
int
buttonPin3
=
7
;
// the number of the pushbutton pin
// variables will change:
int
buttonState1
=
0
;
// variable for reading the pushbutton status
int
buttonState2
=
0
;
// variable for reading the pushbutton status
int
buttonState3
=
0
;
// variable for reading the pushbutton status
/*
Robot movement stuff!
*/
/* Defines */
#define rightServo 5 // Define Servo right pin
#define leftServo 6 // Define Servo left pin
/* Global variables */
Servo
rightWheel
;
Servo
leftWheel
;
/* Define functions used to control the robot. */
/* Stop the robot */
void
setWheelsStop
()
{
rightWheel
.
writeMicroseconds
(
1500
);
leftWheel
.
writeMicroseconds
(
1500
);
}
/* Drive forward */
void
setWheelsForward
()
{
rightWheel
.
writeMicroseconds
(
1300
);
leftWheel
.
writeMicroseconds
(
1700
);
}
/* Drive right */
void
setWheelsRight
()
{
rightWheel
.
writeMicroseconds
(
1700
);
leftWheel
.
writeMicroseconds
(
1700
);
}
/* Drive left */
void
setWheelsLeft
()
{
rightWheel
.
writeMicroseconds
(
1300
);
leftWheel
.
writeMicroseconds
(
1300
);
}
void
changeSegment
(
int
a
)
{
/*
Here it is determined whether to increment or decrement the position on the LCD.
For moving forward a is 2, right a is 1 and left a is 0.
According to the int value it's determined whether to increment pointx or pointy.
Boolean sameArray and int sameCounter is used when we change the segment, but later
we need the old array back if we return to the previous segment so that
the path is not lost. If the counter is two then we restore the previous array.
cursorCounter is used to know in which segment to create the pathway.
*/
if
(
a
==
2
)
{
pointx
++
;
pos
=
4
;
// Reinitialize pos to 4.
sameArray
=
false
;
// Just in case sameArray is true but pointx and pointy have changed.
}
else
if
(
a
==
1
)
{
/* Fristly check if you're going to move one segment down and towards. */
if
(
posx
==
5
&&
posy
==
8
)
{
pointx
++
;
pointy
++
;
pos
=
4
;
// Reinitialize pos to 4.
sameArray
=
false
;
// Just in case sameArray is true but pointx and pointy have changed.
}
else
if
(
posx
==
5
)
{
cursorCounter
++
;
counterb
++
;
pointx
++
;
pos
=
4
;
posy
--
;
sameArray
=
false
;
}
else
if
(
pointy
==
0
)
{
sameArray
=
true
;
sameCounter
++
;
//temp = liigun;
pointy
=
1
;
// Move down one segment, y == 1 means lower segment
}
counterb
=
0
;
}
else
if
(
a
==
0
)
{
/* Fristly check if you're going to move one segment up and towards. */
if
(
posx
==
5
&&
posy
==
7
)
{
// Check if at the end of segment
pointx
++
;
pointy
--
;
pos
=
4
;
// Reinitialize pos to 4.
sameArray
=
false
;
// Just in case sameArray is true but pointx and pointy have changed.
}
else
if
(
pointy
==
1
)
{
// To check if upper or lower segment
sameArray
=
true
;
sameCounter
++
;
pointy
=
0
;
// Move up one segment
}
counterb
=
7
;
}
if
(
sameArray
)
{
if
(
sameCounter
==
1
)
{
/* Copy the contents of array liigun to array temp for later use. */
memcpy
(
temp
,
liigun
,
sizeof
(
liigun
));
// OR sizeof(temp)
resetArray
();
/* Increment cursorCounter for next segment. */
cursorCounter
++
;
}
else
if
(
sameCounter
==
2
)
{
// If you need to copy the contents of temp back to array
memcpy
(
liigun
,
temp
,
sizeof
(
temp
));
// Copy the contents of array temp to liigun
memset
(
temp
,
0
,
sizeof
(
temp
));
// Empty temp array
cursorCounter
--
;
sameCounter
=
0
;
sameArray
=
false
;
}
}
else
{
resetArray
();
// Reset the byte array
cursorCounter
++
;
// Increment cursorCounter for next segment
}
}
void
resetArray
()
{
// Reinitizialize robotMoving 2D array.
memset
(
liigun
,
0
,
sizeof
(
liigun
));
}
void
setup
()
{
Serial
.
begin
(
9600
);
// Begin Serial
lcd
.
begin
();
// Begin LCD
lcd
.
clear
();
// Clear LCD
// initialize the pushbutton pin as an input:
pinMode
(
buttonPin1
,
INPUT
);
pinMode
(
buttonPin2
,
INPUT
);
pinMode
(
buttonPin3
,
INPUT
);
/* Attach servos to digital pins defined earlier */
//rightWheel.attach(rightServo);
//leftWheel.attach(leftServo);
/* Put servos to standstill */
//setWheelsStop();
}
void
loop
()
{
/* read the state of the pushbutton value. */
buttonState1
=
digitalRead
(
buttonPin1
);
buttonState2
=
digitalRead
(
buttonPin2
);
buttonState3
=
digitalRead
(
buttonPin3
);
/* Check if you can move forward. YOU CAN ONLY MOVE FORWARD EIGHTY (80) TIMES! */
if
(
posx
>=
79
)
{
Serial
.
println
(
"ERROR!"
);
exit
(
0
);
}
if
(
buttonState1
==
HIGH
)
{
Serial
.
println
(
"Otse"
);
/* Check in which segment you are going to draw a point. */
if
(
posx
==
5
)
{
changeSegment
(
2
);
// Two to move forward
}
/* DO NOT CHANGE counterb HERE! Change only when moving right or left! */
liigun
[
counterb
]
=
liigun
[
counterb
]
|
(
1
<<
pos
);
lcd
.
createChar
(
cursorCounter
,
liigun
);
lcd
.
setCursor
(
pointx
,
pointy
);
lcd
.
write
(
cursorCounter
);
posx
++
;
// Increment posx to move on the segment to know were we at.
pos
--
;
// Decrement pos to get the correct power of 2 to add to counterb.
delay
(
500
);
}
else
if
(
buttonState2
==
HIGH
)
{
Serial
.
println
(
"Parem"
);
/* Check if you can move right. */
if
(
pointy
==
1
&&
posy
==
0
)
{
Serial
.
println
(
"ERROR! CANNOT MOVE RIGHT NO MORE!"
);
exit
(
0
);
}
/* Check in which segment you are going to draw a point. */
if
(
posy
==
8
||
posx
==
5
)
{
changeSegment
(
1
);
// One to move down (right)
}
else
{
counterb
++
;
// Increment counterb to "move right" in array.
}
liigun
[
counterb
]
=
liigun
[
counterb
]
|
(
1
<<
pos
);
lcd
.
createChar
(
cursorCounter
,
liigun
);
lcd
.
setCursor
(
pointx
,
pointy
);
lcd
.
write
(
cursorCounter
);
posx
++
;
// Increment posx to move forward one step.
posy
--
;
// Decrement posy to move down one step.
pos
--
;
// Decrement pos to get the correct power of 2 to add to counterb.
delay
(
500
);
}
else
if
(
buttonState3
==
HIGH
)
{
Serial
.
println
(
"Vasak"
);
/* Check if you can move left. */
if
(
pointy
==
0
&&
posy
==
15
)
{
Serial
.
println
(
"ERROR! CANNOT MOVE LEFT NO MORE!"
);
exit
(
0
);
}
/* Check in which segment you are going to draw a point. */
if
(
posy
==
7
)
{
changeSegment
(
0
);
// Zero to move up (left)
}
else
{
counterb
--
;
// Decrement counterb to "move left" in array.
}
liigun
[
counterb
]
=
liigun
[
counterb
]
|
(
1
<<
pos
);
lcd
.
createChar
(
cursorCounter
,
liigun
);
lcd
.
setCursor
(
pointx
,
pointy
);
lcd
.
write
(
cursorCounter
);
posx
++
;
// Increment posx to move forward one step.
posy
++
;
// Increment posy to move up one step.
pos
--
;
// Decrement pos to get the correct power of 2 to add to counterb.
delay
(
500
);
}
if
(
posx
==
5
)
{
posx
==
0
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment