#include <LedControl.h>
#define D_PIN 11
#define CS_PIN 12
#define CLK_PIN 13
enum Direction {IN, OUT};
enum State {DOWN, LEFT, UP, RIGHT, ADJ_RIGHT, ADJ_LEFT, ADJ_TOP, ADJ_BOTTOM, TURN};
LedControl lc = LedControl(D_PIN, CLK_PIN, CS_PIN, 4);
int top = 0;
int bottom = 15;
int left = 15;
int right = 0;
/*
int top = 3;
int bottom = 4;
int left = 4;
int right = 3;
*/
int row = top, col = left;
enum Direction direction = IN;
enum State state = DOWN;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
lc.shutdown(0, false);
lc.setIntensity(0, 8);
lc.clearDisplay(0);
lc.shutdown(1, false);
lc.setIntensity(1, 8);
lc.clearDisplay(1);
lc.shutdown(2, false);
lc.setIntensity(2, 8);
lc.clearDisplay(2);
lc.shutdown(3, false);
lc.setIntensity(3, 8);
lc.clearDisplay(3);
/*
lc.setLed (0, 0, 0, true);
lc.setLed (0, 0, 7, true);
lc.setLed (1, 0, 0, true);
lc.setLed (1, 0, 7, true);
lc.setLed (2, 0, 0, true);
lc.setLed (2, 0, 7, true);
lc.setLed (3, 0, 0, true);
lc.setLed (3, 0, 7, true);
*/
}
// Изменить текущее состояние
void stateChange()
{
/*
IN:
DOWN -> ADJ_LEFT -> RIGHT -> ADJ_BOTTOM -> UP -> ADJ_RIGHT -> LEFT -> ADJ_TOP -> TURN -> RIGHT
OUT:
RIGHT -> ADJ_RIGHT -> DOWN -> ADJ_BOTTOM -> LEFT -> ADJ_LEFT -> UP -> ADJ_TOP -> TURN -> DOWN
Домашнее задание - сделать вот такое переключение OUT:
RIGHT -> ADJ_TOP -> DOWN -> ADJ_LEFT -> LEFT -> ADJ_BOTTOM -> UP -> ADJ_RIGHT -> ? TURN
*/
Serial.print("stateChange: ");
switch (state)
{
case DOWN:
Serial.println("DOWN");
if (row >= bottom)
{
state = (direction == IN) ? ADJ_LEFT : ADJ_LEFT;
}
break;
case ADJ_LEFT:
Serial.println("ADJ_LEFT");
state = (direction == IN) ? RIGHT : LEFT;
break;
case RIGHT:
Serial.println("RIGHT");
if (col <= right)
{
state = (direction == IN) ? ADJ_BOTTOM : ADJ_TOP;
}
break;
case ADJ_BOTTOM:
Serial.println("ADJ_BOTTOM");
state = (direction == IN) ? UP : UP;
break;
case UP:
Serial.println("UP");
if ((row <= top) || (row < 0))
{
state = (direction == IN) ? ADJ_RIGHT : ADJ_RIGHT;
}
break;
case ADJ_RIGHT:
Serial.println("ADJ_RIGHT");
if ((row <= 0) && (col == 15))
{
state = TURN;
}
else
{
state = (direction == IN) ? LEFT : RIGHT;
}
break;
case LEFT:
Serial.println("LEFT");
if (col >= left)
{
state = (direction == IN) ? ADJ_TOP : ADJ_BOTTOM;
}
break;
case ADJ_TOP:
Serial.println("ADJ_TOP");
if (((left <= right) || (top < 0)) && (direction == IN))
{
state = TURN;
}
else
{
state = (direction == IN) ? DOWN : DOWN;
}
break;
case TURN:
Serial.println("TURN");
if (direction == IN)
{
state = DOWN;
}
else
{
state = DOWN;
}
break;
}
}
void stateAction()
{
Serial.print("stateAction: ");
switch (state)
{
case DOWN:
Serial.println("DOWN");
row++;
break;
case RIGHT:
Serial.println("RIGHT");
col--;
break;
case UP:
Serial.println("UP");
row--;
break;
case LEFT:
Serial.println("LEFT");
col++;
break;
case ADJ_LEFT:
Serial.println("ADJ_LEFT");
left += (direction == IN) ? -1 : 1;
Serial.print("left = "); Serial.println(left);
break;
case ADJ_BOTTOM:
Serial.println("ADJ_BOTTOM");
bottom += (direction == IN) ? -1 : 1;
Serial.print("bottom = "); Serial.println(bottom);
break;
case ADJ_RIGHT:
Serial.println("ADJ_RIGHT");
right += (direction == IN) ? 1 : -1;
Serial.print("right = "); Serial.println(right);
break;
case ADJ_TOP:
Serial.println("ADJ_TOP");
top += (direction == IN) ? 1 : -1;
Serial.print("top = "); Serial.println(top);
break;
case TURN:
Serial.println("TURN");
// переключить
if (direction == IN) // было движение к центру
{
direction = OUT;
top = 6;
left = 7;
right = 7;
bottom = 8;
col = 7;
row = 7;
// state = EXPAND;
}
else // было движение из центра
{
direction = IN;
top = 0;
left = 23;
right = 0;
bottom = 7;
// state = SQUEEZE;
}
break;
}
}
void loop() {
//return;
// put your main code here, to run repeatedly:
/*
Serial.print("row = "); Serial.println(row);
Serial.print("col = "); Serial.println(col);
Serial.print("left = "); Serial.println(left);
Serial.print("top = "); Serial.println(top);
Serial.print("right = "); Serial.println(right);
Serial.print("bottom = "); Serial.println(bottom);
*/
/*
if (direction == IN)
{
lc.setLed(0, row, col, true);
}
else
{
lc.setLed(0, row, col, false);
}
*/
if ((row < 8) && (col < 8))
{
lc.setLed (0, row, col, (direction == IN));
}
else if ((row < 8) && (col < 16))
{
lc.setLed (1, row, col - 8, (direction == IN));
}
else if ((row < 16) && (col < 8))
{
lc.setLed (2, row - 8 , col, (direction == IN));
}
else
{
lc.setLed (3, row - 8, col - 8 , (direction == IN));
}
delay(50);
//lc.setLed(0, row, col, false);
stateChange();
stateAction();
}