Commit d4e84b43 by satsob

LCD Robot Movements

parent e2727e36
Showing with 251 additions and 0 deletions
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 16, 2); // Address found using lcd scanner, columns, rows
/*
Test.
There are 16x2 segments on the LCD display. Each segment is 8x5 (width x length).
The points in the segment starts from left to right and from top to bottom.
"1" (ONE) represents filled box and "0" (ZERO) unfilled box in the segment.
For example, 00010 means that in the first line only the forth box in segment is filled.
Other boxes in the line are unfilled (not active).
*/
//byte test[8] = {B00001, B00010, B00100, B01000, B10000, B00101, B00110, B00111};
// Intialize for robot driving
/* 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};
bool sameArray = false;
int sameCounter = 0;
/* 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
int rowCounter = 0; // Can be max 8
int colCounter = 0; // Can be max 5
/* To store the values for moving on LCD screen. To move in array. */
//int countera = 0 // Initialize countera as array[countera][]
int counterb = 7 // Initialize counterb as array[][counterb]. 7 to start from the bottom
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
/* To detect whether we have moved to another segment on display or not. */
//bool posChange = false; // To change positions or not to change
//int movex = 0; // Moving and detecting x-coordinates
//int movey = 0; // Moving and detecting y-coordinates
void setup()
{
lcd.begin();
Serial.begin(9600);
/* Ignore the commented out code! These are for testing purposes! */
// lcd.backlight();
// lcd.clear();
// lcd.setCursor(4, 0);
// lcd.print("Write Something!");
// delay(500);
// lcd.clear();
// lcd.scrollDisplayLeft(); //moves sentence to the left by one character. Right to right.
// lcd.cursor(); //show visible cursor below next char that will be printed
// lcd.noCursor(); //turns cursor off
// lcd.blink(); //similar to lcd.cursor but creates blinking block cursor
// lcd.noBlink(); //turn blinking cursor off
/* TEST */
// lcd.createChar(6, test);
// lcd.setCursor(0, 0);
// lcd.write(6);
/* Testing bit shifting and printing on serial monitor! */
// unsigned int ex = 0b00100; // Initialize unsigned int
// Serial.println(ex); // Prints decimal number
// ex = ex << 1; // Bitwise operation -> shifting to left one time
// Serial.println(ex, BIN); // Prints binary number
}
void loop()
{
if (otse) {
// Check if you can move forward. You CAN ONLY move forward eighty (80) times.
if (colCounter == 79) {
Serial.println("ERROR!");
exit(0);
}
// Check in which segment you are going to draw a point.
if (posx % 5 == 0) {
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[counterb]);
lcd.setCursor(pointx, pointy);
lcd.write(cursorCounter);
colCounter++; // Increment colCounter on keep count on which column we're at.
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.
}
else if (parem) {
// Check in which segment you are going to draw a point.
if (posy == 8) {
changeSegment(1); // One to move down (right)
} else {
counterb++; // Increment counterb to "move right" in array.
}
// Check if you can move right.
/*if (counterb == 7) {
Serial.println("ERROR!");
exit(0);
}*/
liigun[counterb] = liigun[counterb] | (1 << pos);
lcd.createChar(cursorCounter, liigun[counterb]);
lcd.setCursor(pointx, pointy);
lcd.write(cursorCounter);
colCounter++; // Increment colCounter on keep count on which column we're at.
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.
}
else if (vasak) {
// 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.
}
// Check if you can move left.
/*if (counterb == 0) {
Serial.println("ERROR!");
exit(0);
}*/
liigun[counterb] = liigun[counterb] | (1 << pos);
lcd.createChar(cursorCounter, liigun[counterb]);
lcd.setCursor(pointx, pointy);
lcd.write(cursorCounter);
colCounter++; // Increment colCounter on keep count on which column we're at.
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.
}
}
void changeSegment(int a) {
/*if (pos < 4)
byte temp[] = liigun; // Save for when you need the old array back*/
// Move down if a == 1, else move up if a == 0 or forward if a == 2
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 == 0 && 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 (pointy == 0) {
sameArray = true;
sameCounter++;
//temp = liigun;
pointy = 1; // Move down one segment
}
counterb = 0;
}
else if (a == 0) {
/* Fristly check if you're going to move one segment up and towards. */
if (posx % 5 == 0 && posy == 7) {
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) {
sameArray = true;
sameCounter++;
pointy = 0; // Move up one segment
}
counterb = 7;
}
// Increment cursorCounter for next segment.
//cursorCounter++;
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)); // OR sizeof(temp)
memset(temp,0,sizeof(temp)); // Empty temp array
cursorCounter--;
sameCounter = 0;
sameArray = false;
}
} else {
//resetArray();
// Increment cursorCounter for next segment.
cursorCounter++;
}
}
//void resetArray() {
// // Reinitizialize robotMoving 2D array.
// memset(robotMoving,0,sizeof(robotMoving));
//
// // OR
//
// for(int i=0;i<5;i++)
// for(int j=0;j<8;j++)
// robotMoving[i][j] = 0b00000; // OR = 0 OR = 0b0
//
// memset(liigun, 0, sizeof(liigun));
//
// //OR
//
// for (int i=0; i<8; i++)
// liigun[i] = B00000; // 0b00000
//
//}
//
//void forward() {
//
//}
//
//void rightTurn() {
//
//}
//
//void leftTurn() {
//
//}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment