I am trying to simply make a "cursor" LED on a 8x8 matrix move around without leaving a trail of LED's behind it.
Hardware:
- 1 2-axis joystick(parallax)
- 1 8x8 LED matrix (MAX72)
Library:
LedControl-MAX7219-MAX7221
Code:
#include "LedControl.h"
LedControl lc=LedControl(12,11,10,1);
unsigned long delaytime=200;
const int hPin = A0;
const int vPin = A1;
int hVal;
int vVal;
int xPos;
int yPos;
void setup() {
lc.shutdown(0,false);
lc.setIntensity(0,8);
lc.setLed(0,0,0,1);
delay(delaytime);
lc.clearDisplay(0);
pinMode(hPin,INPUT);
pinMode(vPin,INPUT);
Serial.begin(9600);
Serial.println("[setup]");
xPos = 0;
yPos = 0;
}
void loop() {
hVal = analogRead(hPin);
vVal = analogRead(vPin);
Serial.print("Horizontal: ");
Serial.print(hVal);
Serial.print(" Vertical: ");
Serial.println(vVal);
if(hVal >= 768){
if(hVal >= 1000){
xPos = xPos + 256;
}
else {xPos = xPos + 128;
}
}
else if(hVal <= 256){
if(hVal <=24){
xPos = xPos - 256;
}
else {xPos = xPos - 128;
}
}
else{
}
Serial.print(" X: ");
Serial.print(xPos);
Serial.print(" ");
if(vVal >= 768){
if(vVal >= 1000){
yPos = yPos + 256;
}
else {yPos = yPos + 128;
}
}
else if(vVal <= 256){
if(vVal <=24){
yPos = yPos - 256;
}
else {yPos = yPos - 128;
}
}
else{
}
Serial.print(" Y: ");
Serial.print(yPos);
Serial.print(" ");
if(xPos >=1024){
xPos = 0;
}
if(yPos >=1024) {
yPos = 0;
}
if(xPos <0){
xPos = 1024;
}
if(yPos <0) {
yPos = 1024;
}
////////////////////////////////////////////////////////////////////////////////////
// convert the 0-1024 value of xPos to 0-8 to fit the matrix command
int xMax;
/* if(xPos <= 128){
xMax = 0;
}
else if(128 < xPos <= 256){
xMax = 1;
}
else if(256 < xPos <= 384){
xMax = 2;
}
else if(384 < xPos <= 512){
xMax = 3;
}
else if(512 < xPos <= 640){
xMax = 4;
}
else if(640 < xPos <= 768){
xMax = 5;
}
else if(768 < xPos <= 896){
xMax = 6;
}
else if(896 < xPos <= 1024){
xMax = 7;
}
*/
xMax = xPos/128;
////////////////////////////////////////////////////////////////////////////////////
// convert the 0-1024 value of yPos to 0-8 to fit the matrix command
int yMax;
/* if(yPos <= 128){
yMax = 0;
}
if(128 < yPos <= 256){
yMax = 1;
}
if(256 < yPos <= 384){
yMax = 2;
}
if(384 < yPos <= 512){
yMax = 3;
}
if(512 < yPos <= 640){
yMax = 4;
}
if(640 < yPos <= 768){
yMax = 5;
}
if(768 < yPos <= 896){
yMax = 6;
}
if(896 < yPos <= 1024){
yMax = 7;
}
*/
yMax = yPos/128;
///////////////////////////////////////////////////////////////////////////////////
Serial.print(" xMax: ");
Serial.print(xMax);
Serial.print(" yMax: ");
Serial.print(yMax);
lc.setLed(0,xMax,yMax,1);
int xLast;
int yLast;
if(xMax != xLast){
lc.setLed(0,xLast,yLast,0);
}
if(yMax != yLast){
lc.setLed(0,xLast,yLast,0);
}
xLast = xMax;
yLast = yMax;
}
My Main problem is "deleting" the dot before the the current one so that there is no trail to the cursor. I tried this with xLast and yLast at the end of the loop but I could not make it work.
Please help!
comment your code, I can't seem to find where you are setting your leds.
but, you need to turn off the current led before proceeding to the next led
void LedControl::setLed(int addr, int row, int column, bool state) {
edit:
lc.setLed(0,xMax,yMax,1);
at this line:
Change it to:
static int oldxMax = 0;
static int oldyMax = 0;
if(oldxMax != xMax && oldyMax != yMax){
lc.setLed(0,oldxMax,oldyMax,0);
}
lc.setLed(0,xMax,yMax,1);
xMax = oldxMax;
yMax = oldyMax;
I just say this code:
int xLast;
int yLast;
if(xMax != xLast){
lc.setLed(0,xLast,yLast,0);
}
if(yMax != yLast){
lc.setLed(0,xLast,yLast,0);
}
xLast = xMax;
yLast = yMax;
}
the reason it's not working, is that the variable yLast and xLast are getting destroyed after the function ends, the }, then created again once the function runs again, but they are always zero.
to make them not zero, they need to be global variables, or be static, like I did in my code...
Thank you! I will try this as soon as I can and report back to this topic.
I took your suggestion and slightly modified it and it works!
This is my finished code (this time thoroughly annotated):
//Arduino Uno 8x8 LED matrix "cursor" w/ joystick
#include "LedControl.h"
LedControl lc=LedControl(12,11,10,1);
unsigned long delaytime=200;
// Variables:
const int hPin = A0; // left/right joystick pin
const int vPin = A1; // up/down joystick pin
int hVal; // left/right joystick reading
int vVal; // up/down joystick reading
int xPos; // x-axis position
int yPos; // y-axis position
int xMax; // matrix column value
int yMax; // matrix row value
int xLast; // xMax from last loop
int yLast; // yMax from last loop
void setup() {
lc.shutdown(0,false);
lc.setIntensity(0,8);
lc.setLed(0,0,0,1); // debug led for start of setup
delay(delaytime);
lc.clearDisplay(0);
pinMode(hPin,INPUT);
pinMode(vPin,INPUT); // set joystick pins as inputs
Serial.begin(9600); // start serial communication
Serial.println("[setup]"); // serial debug for setup
xPos = 0; //
yPos = 0; // "cursor" starting point
}
void loop() {
hVal = analogRead(hPin);
vVal = analogRead(vPin); // read joytick position pins
Serial.print("Horizontal: "); //
Serial.print(hVal); //
Serial.print(" Vertical: "); //
Serial.println(vVal); // serial debug raw joystick data
if(hVal >= 768){ //
xPos = xPos + 128; //
} //
else if(hVal <= 256){ //
xPos = xPos - 128; //
} //
else{ // Set sensitivity of when joystick triggers a position change,
} // and the speed at whitch it changes (for x-axis).
Serial.print(" X: "); //
Serial.print(xPos); //
Serial.print(" "); // serial debug x-axis position
if(vVal >= 768){ //
yPos = yPos + 128; //
} //
else if(vVal <= 256){ //
yPos = yPos - 128; //
} //
else{ // Set sensitivity of when joystick triggers a position change,
} // and the speed at whitch it changes (for y-axis).
Serial.print(" Y: "); //
Serial.print(yPos); //
Serial.print(" "); // serial debug y-axis position
if(xPos >=1024){ //
xPos = 0; //
} // repeat x-axis in positive direction
if(yPos >=1024) { //
yPos = 0; //
} // repeat y-axis in positive direction
if(xPos <0){ //
xPos = 1024; //
} // repeat x-axis in negative direction
if(yPos <0) { //
yPos = 1024; //
} // repeat y-axis in negative direction
////////////////////////////////////////////////////////////////////////////////////
// ! Taken out because of an updated approach (at bottom of this section) !
// convert the 0-1023 value of xPos to 0-7 to fit the matrix command
/* if(xPos <= 128){
xMax = 0;
}
else if(128 < xPos <= 256){
xMax = 1;
}
else if(256 < xPos <= 384){
xMax = 2;
}
else if(384 < xPos <= 512){
xMax = 3;
}
else if(512 < xPos <= 640){
xMax = 4;
}
else if(640 < xPos <= 768){
xMax = 5;
}
else if(768 < xPos <= 896){
xMax = 6;
}
else if(896 < xPos <= 1024){
xMax = 7;
}
*/
xMax = xPos/128; // convert the 1024 value to 0-7 for matrix ( done here instead of when adding value to xPos because it would of went too fast
////////////////////////////////////////////////////////////////////////////////////
// ! Taken out because of an updated approach (at bottom of this section) !
// convert the 0-1023 value of yPos to 0-7 to fit the matrix command
/* if(yPos <= 128){
yMax = 0;
}
if(128 < yPos <= 256){
yMax = 1;
}
if(256 < yPos <= 384){
yMax = 2;
}
if(384 < yPos <= 512){
yMax = 3;
}
if(512 < yPos <= 640){
yMax = 4;
}
if(640 < yPos <= 768){
yMax = 5;
}
if(768 < yPos <= 896){
yMax = 6;
}
if(896 < yPos <= 1024){
yMax = 7;
}
*/
yMax = yPos/128; // convert the 1024 value to 0-7 for matrix ( done here instead of when adding value to xPos because it would of went too fast
///////////////////////////////////////////////////////////////////////////////////
Serial.print(" xMax: "); //
Serial.print(xMax); //
Serial.print(" yMax: "); //
Serial.print(yMax); // debug cursor position on matrix
lc.setLed(0,xMax,yMax,1); // set cursor position led HIGH (adress,x,y,boolean)
if(xLast != xMax || yLast != yMax){ //
lc.setLed(0,xLast,yLast,0); //
} // if the last x or y coordinate changed then turn off previous led
xLast = xMax; //
yLast = yMax; // set coordinates for previous led
}