Hello,
i'm working on a little Project for the university.
It's a LED-labyrinth connected to a arduino and a gaming controller.
I simply took a 8 meter LED-Strip. Cut it in eight pieces and make with them a matrix.
I use a one dimensional array and it works well. But what I'm trying to do is to make more than one Labyrinth. So I tried it with a two-dimensional array, but it doesn't work and I don't know why!
What I'm trying to fix:
- When the mover (moveX) reach the goal (ziel). Not only get the lights off, also that the mover don't get obstacles at all.
- Than the second labyrinth should read.
Is there a way to reset the programm? So I can set a random labyrinth and it should work, or not?
This is my first version, which works:
// Joystick movement
// Joystick on A0=X / A1= Y
#include "LPD8806.h"
#include "SPI.h"
int line= 32; // LED in one line
int column=8; // LED in a column
int nLEDs = 256; //
int dataPin = 2;
int clockPin = 3;
LPD8806 strip = LPD8806(nLEDs, dataPin, clockPin);
int moveX=4*line; // Startpoint green omver
int ziel=5*line+25;
int testLed=0;
int test1;
boolean left,right,up,down;
int obstacle[]={
0,1,1,1,0,0,0,1,1,1,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,1,0,1,0,0,0,1,0,0,1,0,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,0,1,0,
1,1,0,1,0,1,1,1,0,1,0,1,1,0,0,0,0,1,0,1,0,0,1,1,1,1,1,0,1,0,1,0,
0,1,0,0,0,1,0,1,0,0,0,0,1,0,1,1,0,1,0,1,0,0,1,0,0,0,1,0,1,0,1,0,
0,0,0,1,0,1,0,1,1,1,1,0,1,0,1,0,0,1,1,1,1,1,1,0,1,0,1,0,1,0,1,0,
1,1,0,1,1,1,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,
0,0,0,1,0,1,0,1,0,1,0,1,1,0,1,0,1,0,1,1,1,1,1,0,1,1,1,1,1,1,1,0,
0,1,0,1,0,0,0,0,0,1,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,
};
void setup() {
// Start up the LED strip
strip.begin();
// Update the strip, to start they are all 'off'
strip.show();
for (int i=0 ; i < nLEDs; i++) {
if (obstacle[i]==1) {
strip.setPixelColor(i,10,60,40); // obstacle color
}
strip.show();
}
Serial.begin(9600);// Debugging only
strip.setPixelColor(ziel,127,0,10);
}
void loop() {
Serial.print(analogRead(A0));
Serial.print(" ");
Serial.println(analogRead(A1));
if (analogRead(A1) > 1000){
down=true;
}
else {
down=false;
}
if ((analogRead(A1) <800)&& (analogRead(A1)>100)){
up=true;
}
else {
up=false;
}
if ((analogRead(A0) <650)&& (analogRead(A0)>100)){
right=true;
}
else {
right=false;
}
if (analogRead(A0) >900) {
left=true;
}
else {
left=false;
}
go();
}
//
// set movement according reading
int go(){
strip.setPixelColor(moveX,80,80,0); // mover on startposition
strip.show();
delay(10); //
if ((left)&& (moveX%line !=0))
{
int test=moveX -1;
if (checkO(test)) {
strip.setPixelColor(moveX,0); // clear green mover
moveX--;
}
}//walk right
if ((right)&&(moveX%line !=(line-1))){
int test=moveX+1;
if (checkO(test)) {
strip.setPixelColor(moveX,0); // clear green mover
moveX++;
}
}
if ((down)&& (moveX <(line*(column-1))))
{
int test=moveX+line;
if (checkO((test))) {
strip.setPixelColor(moveX,0); // clear green mover
moveX+=line; //walk down
}
}
if ((up)&&(moveX > (line-1))){
int test=moveX-line;
if (checkO((test))) {
strip.setPixelColor(moveX,0); // clear green mover
moveX-=line;// walk up
}
}
}
// Check Obstacles
boolean checkO(int pos){
boolean test=false;
if (obstacle[pos] ==0){
test=true;
}
else
{
test=false;
}
return test;
}
And this is the second version, which doesn't work:
// Joystick movement
// Joystick on A0=X / A1= Y
#include "LPD8806.h"
#include "SPI.h"
int line= 32; // LED in one line
int column=8; // LED in a column
int nLEDs = 256; //
int dataPin = 2;
int clockPin = 3;
LPD8806 strip = LPD8806(nLEDs, dataPin, clockPin);
int moveX=4*line; // Startpoint green omver
int ziel=4*line+2; //Erstes Ziel
int testLed=0;
int lab = 0;
boolean left,right,up,down;
int obstacle[][256]={
{
0,1,1,1,0,0,0,1,1,1,1,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
,{
0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,0,1,0,1,1,1,1,1,0,0,0,0,0,0,0,
0,0,0,1,0,1,0,1,0,1,1,1,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,1,1,1,0,1,
0,1,0,1,0,0,0,1,0,0,1,0,1,1,0,0,1,0,1,0,1,0,1,0,1,0,1,1,0,0,0,0,
0,1,0,1,0,1,1,1,1,0,0,0,1,0,0,1,1,0,1,0,0,0,1,0,0,0,0,1,1,1,1,0,
0,1,0,1,0,1,0,0,0,0,1,0,0,0,1,1,0,0,1,0,1,1,1,0,1,1,1,1,0,1,0,0,
0,0,0,1,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,
0,1,1,1,0,1,0,0,1,0,0,0,1,0,0,0,1,0,1,1,1,1,0,0,1,1,1,0,1,1,0,0,
0,0,1,0,0,0,0,1,1,0,1,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,
}
};
void setup() {
// Start up the LED strip
strip.begin();
// Update the strip, to start they are all 'off'
strip.show();
for (int i=0 ; i < nLEDs; i++) {
if (obstacle[0][i]==1) {
strip.setPixelColor(i,0,10,117); // obstacle color
}
strip.show();
}
Serial.begin(9600);// Debugging only
strip.setPixelColor(ziel,127,0,0);
}
void loop() {
Serial.print(analogRead(A0));
Serial.print(" ");
Serial.println(analogRead(A1));
if (analogRead(A1) > 1000){
down=true;
}
else {
down=false;
}
if ((analogRead(A1) <800)&& (analogRead(A1)>100)){
up=true;
}
else {
up=false;
}
if ((analogRead(A0) <650)&& (analogRead(A0)>100)){
right=true;
}
else {
right=false;
}
if (analogRead(A0) >900) {
left=true;
}
else {
left=false;
}
go();
/* if(moveX==ziel) {
// strip.setPixelColor(testLed,100,100,0);
lab=1;
for (int i=0 ; i < nLEDs; i++) {
if (obstacle[0][i]==1) {
strip.setPixelColor(i,0,0,0); // obstacle color
}
strip.show();
}
int moveX=4*line;
go();
}*/
}
//
// set movement according reading
int go(){
strip.setPixelColor(moveX,0,32,0); // green mover on startposition
strip.show();
delay(10); //
if ((left)&& (moveX%line !=0))
{
int test=moveX -1;
if (checkO(test)) {
strip.setPixelColor(moveX,0); // clear green mover
moveX--;
}
}//walk right
if ((right)&&(moveX%line !=(line-1))){
int test=moveX+1;
if (checkO(test)) {
strip.setPixelColor(moveX,0); // clear green mover
moveX++;
}
}
if ((down)&& (moveX <(line*(column-1))))
{
int test=moveX+line;
if (checkO((test))) {
strip.setPixelColor(moveX,0); // clear green mover
moveX+=line; //walk down
}
}
if ((up)&&(moveX > (line-1))){
int test=moveX-line;
if (checkO((test))) {
strip.setPixelColor(moveX,0); // clear green mover
moveX-=line;// walk up
}
}
}
// Check Obstacles
boolean checkO(int pos){
boolean test=false;
if (obstacle[0][pos] ==0){
test=true;
}
else
{
test=false;
}
return test;
}
boolean checkZiel(int pos){
boolean test1=false;
if (obstacle[0][pos] == ziel){
test1 = true;
}
else {
test1 = false;
return test1;
}
}
Maybe someone can give me a little help.
Regards
Patrick