/*
ANALOG JOYSTICK CONTROLLED 32X LEDs
030120106
To begin click the middle button.
Başlamak için ortadaki butona tıklayın.
If it seems stuck on the bottom right corner then un-click the middle button.
Sağ alt köşede takılı kalmış gibi görünüyorsa, ortadaki düğmeye tıklamayı kaldırın.
*/
/*
Total pins needed:
Analog joystick:
* 2x Analog pins
* 1x Digital pin
Shift registers:
* 3x Digital pins
2x Analog pins
4x Digital pins
*/
///////// DEFINING PINS
#define VERT_PIN A0
#define HORZ_PIN A1
#define SEL_PIN 2
#define DS 8
#define SHCP 10
#define STCP 9
/////////
// Other constant values
#define LEFT 0
#define RIGHT 1
#define DOWN 0
#define UP 1
#define DELAY 100 // Wait time before next detection, prevents it from moving rapidly
int d[4] = {0, 0, 0, 0}; // Arrays for each line of LEDs.
void setup() {
pinMode(VERT_PIN, INPUT);
pinMode(HORZ_PIN, INPUT);
pinMode(SEL_PIN, INPUT_PULLUP);
pinMode(DS, OUTPUT);
pinMode(SHCP, OUTPUT);
pinMode(STCP, OUTPUT);
}
void loop() {
// Reads from the analog joystick
int vert = analogRead(VERT_PIN);
int horz = analogRead(HORZ_PIN);
bool selPressed = digitalRead(SEL_PIN) == LOW;
if(selPressed)
{
d[0] = 0; d[1] = 0; d[2] = 0; d[3] = 1;
shift();
}
if(horz == 1023) // Value for maximum left
{
horzMove(LEFT);
delay(DELAY);
}
if(horz == 0) // Value for maximum right
{
horzMove(RIGHT);
delay(DELAY);
}
if (vert == 1023) // Value for maximum up
{
vertMove(UP);
delay(DELAY);
}
if(vert == 0) // Value for maximum down
{
vertMove(DOWN);
delay(DELAY);
}
}
void horzMove(int direction)
{
for(int i = 0; i < 4; i++)
{
if(d[i] == 0)
continue;
else
{
if(direction == LEFT)
{
if(d[i] == 128)
d[i] = 1;
else
d[i] = d[i] << 1;
}
if(direction == RIGHT)
{
if(d[i] == 1)
d[i] = 128;
else
d[i] = d[i] >> 1;
}
}
}
shift();
}
void vertMove(int direction)
{
int temp;
for(int i = 0; i < 4; i++)
{
if(d[i] == 0)
continue;
else
{
temp = d[i];
if(direction == UP)
{
if(i == 0)
d[3] = temp;
else
d[i-1] = temp;
}
if(direction == DOWN)
{
if(i == 3)
d[0] = temp;
else
d[i+1] = temp;
}
d[i] = 0;
break;
}
}
shift();
}
void shift()
{
for(int i = 0; i < 4; i++)
{
shiftOut(DS, SHCP, LSBFIRST, d[i]);
}
// Send all the data in one go to avoid all vertical lights activating together
digitalWrite(STCP, LOW);
digitalWrite(STCP, HIGH);
}