#include <Stepper.h>
Stepper myStepper(200, 1, 2, 3, 4);
const int stepsPerRevolution = 180;
const int UpButton = 9;
const int DownButton = 8;
const int LeftButton = 7;
const int RightButton = 6;
const int StopButton = 5;
const int fieldSize = 15;
char field[fieldSize][fieldSize];
// Начальные координаты робота
int x = fieldSize / 2;
int y = fieldSize / 2;
void drawField() {
for (int i = 0; i < fieldSize; i++) {
for (int j = 0; j < fieldSize; j++) {
Serial.print(field[i][j]);
Serial.print(' ');
}
Serial.println();
}
Serial.println();
}
void control() {
if (digitalRead(UpButton) == LOW)
{
Serial.println("Robot moves forward");
field[y][x] = '|';
y = max(0, y - 1);
myStepper.step(stepsPerRevolution / 2);
field[y][x] = 'R';
drawField();
}
else if (digitalRead(DownButton) == LOW)
{
Serial.println("Robot moves backward");
field[y][x] = '|';
y = min(fieldSize - 1, y + 1);
myStepper.step(-stepsPerRevolution / 2);
field[y][x] = 'R';
drawField();
}
else if (digitalRead(LeftButton) == LOW)
{
Serial.println("Robot moves to the left");
field[y][x] = '-';
x = max(0, x - 1);
myStepper.step(stepsPerRevolution / 4);
field[y][x] = 'R';
drawField();
}
else if (digitalRead(RightButton) == LOW)
{
Serial.println("Robot moves to the right");
field[y][x] = '-';
x = min(fieldSize - 1, x + 1);
myStepper.step(stepsPerRevolution / 4);
field[y][x] = 'R';
drawField();
}
else if (digitalRead(StopButton) == LOW)
{
Serial.println("Robot doesn't move");
}
}
void setup() {
pinMode(UpButton, INPUT_PULLUP);
pinMode(DownButton, INPUT_PULLUP);
pinMode(LeftButton, INPUT_PULLUP);
pinMode(RightButton, INPUT_PULLUP);
pinMode(StopButton, INPUT_PULLUP);
Serial.begin(9600);
Serial.println("Robot is inactive.");
for (int i = 0; i < fieldSize; i++) {
for (int j = 0; j < fieldSize; j++) {
field[i][j] = '.';
}
}
field[y][x] = 'R';
drawField();
}
void loop() {
control();
delay(500);
}