Components and supplies
Analog joystick (Generic)
Breadboard (generic)
Jumper wires (generic)
RGB LED Pixel Matrix, NeoPixel NeoMatrix
Arduino UNO
Project description
Code
Code
c_cpp
1#include <FastLED.h> 2 3//matrix settings 4#define NUM_LEDS 256 5#define DATA_PIN 3 6#define BRIGHTNESS 8 7 8//joystick settings 9#define pinX A2 // ось X джойстика 10#define pinY A1 // ось Y джойстика 11#define swPin 2 // кнопка джойстика 12 13int snake[256]; // array of snake elements 14int snakeSize = 2; // real snake size 15int snakeSpeed = 500; 16 17int row; // row number 18int col; // column number 19 20int lastDirection = 135; // start direction 21int i, newDirection, OlddX = 1, OlddY, f; 22 23int red, green, blue, fred, fgreen, fblue; //colors 24CRGB leds[NUM_LEDS]; 25 26void setup() { 27 red = random(0, 255); 28 green = random(0, 255); 29 blue = random(0, 255); 30 fred = random(127, 255); 31 fgreen = random(127, 255); 32 fblue = random(127, 255); 33 34 Serial.begin(9600); 35 pinMode(pinX, INPUT); 36 pinMode(pinY, INPUT); 37 pinMode(swPin, INPUT); 38 digitalWrite(swPin, HIGH); 39 40 FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS); 41 FastLED.setBrightness(BRIGHTNESS); 42 for( i=0; i<=255; i++ ){ 43 snake[i] = 0; 44 } 45 46 for( i=0; i<=snakeSize; i++ ){ 47 snake[i] = lastDirection+i; 48 } 49 50 f = random(0, 255); 51 FastLED.show(); 52} 53 54int Snakedirection(int last, int dX, int dY ){ 55 dX = map(dX, 0, 1000, -1, 1); 56 dY = map(dY, 0, 1000, -1, 1); 57 if(dX == 0 && dY == 0 && OlddX != dX){ 58 dX = OlddX; 59 } 60 if(dY == 0 && dX == 0 && OlddY != dY){ 61 dY = OlddY; 62 } 63 int newDirection = last; 64 if( dX != 0 ){ // moving in X direction 65 if ( row&1 ){ 66 if( col == 0 && dX == 1){ newDirection = last -15; } 67 else if( col == 15 && dX == -1){ newDirection = last +15; } 68 else newDirection = last + dX; // четная 69 } else { 70 if( col == 0 && dX == 1){ newDirection = last +15; } 71 else if( col == 15 && dX == -1 ){ newDirection = last -15; } 72 else newDirection = last - dX; // не четная 73 } 74 } 75 if( dY < 0){ // moving in Y DOWN direction 76 if(row == 15 && dY == -1){newDirection = col;} 77 else if ( row&1 ){ 78 newDirection = last + (col*2)+1; // четная 79 } else { 80 newDirection = last + (16-col-1)+(16-col); // не четная 81 } 82 } 83 if( dY > 0){ // moving in Y UP direction 84 if( row == 0 && dY == 1){ newDirection = 255 - col;} 85 else if ( row&1 ){ 86 newDirection = last - (last - 16*row) - (16 - col); // четная 87 } else { 88 newDirection = last - (col*2)-1; // не четная 89 } 90 } 91 OlddX = dX; 92 OlddY = dY; 93 return newDirection; 94} 95 96int snakeMove(int snakeDirection){ 97 98 for( i=0; i<=255; i++ ){ 99 if( snake[i] == snakeDirection ){ 100 death(); 101 } 102 } 103 104 FastLED.clear(); 105 for(i=snakeSize; i>=1; i--){ 106 snake[i] = snake[i-1]; 107 } 108 snake[0] = snakeDirection; 109 for( i=0; i<=255; i++ ){ 110 if( snake[i] ){ 111 leds[snake[i]].setRGB(red, green, blue); 112 } 113 } 114 FastLED.show(); 115 row = (int)(snakeDirection/16); // row number 116 if ( row&1 ){ 117 col = (row+1) * 16 - snakeDirection - 1; 118 } else { 119 col = snakeDirection - row * 16; 120 } 121 return snakeDirection; 122} 123 124void food( int eaten ){ 125 if( eaten == f ){ 126 snakeSize++; 127 f = random(0, 255); 128 red = fred; 129 green = fgreen; 130 blue = fblue; 131 fred = random(0, 255); 132 fgreen = random(0, 255); 133 fblue = random(0, 255); 134 snakeSpeed = snakeSpeed / 1.1; 135 } else { 136 leds[f].setRGB(fred, fgreen, fblue); 137 FastLED.show(); 138 } 139} 140 141void death(){ 142 snakeSize = 2; 143 snakeSpeed = 500; 144 red = 255; 145 green = 0; 146 blue = 0; 147} 148 149void color(boolean sw){ 150 if(!sw){ 151 152 red = random(0,255); 153 green = random(0,255); 154 blue = random(0,255); 155 156 } 157} 158 159void loop() { 160 color( digitalRead(swPin) ); 161 newDirection = Snakedirection(lastDirection, analogRead(pinX), analogRead(pinY)); 162 lastDirection = snakeMove(newDirection); 163 food(newDirection); 164 delay(snakeSpeed); 165}
Code
c_cpp
1#include <FastLED.h> 2 3//matrix settings 4#define NUM_LEDS 256 5#define DATA_PIN 3 6#define BRIGHTNESS 8 7 8//joystick settings 9#define pinX A2 // ось X джойстика 10#define pinY A1 // ось Y джойстика 11#define swPin 2 // кнопка джойстика 12 13int snake[256]; // array of snake elements 14int snakeSize = 2; // real snake size 15int snakeSpeed = 500; 16 17int row; // row number 18int col; // column number 19 20int lastDirection = 135; // start direction 21int i, newDirection, OlddX = 1, OlddY, f; 22 23int red, green, blue, fred, fgreen, fblue; //colors 24CRGB leds[NUM_LEDS]; 25 26void setup() { 27 red = random(0, 255); 28 green = random(0, 255); 29 blue = random(0, 255); 30 fred = random(127, 255); 31 fgreen = random(127, 255); 32 fblue = random(127, 255); 33 34 Serial.begin(9600); 35 pinMode(pinX, INPUT); 36 pinMode(pinY, INPUT); 37 pinMode(swPin, INPUT); 38 digitalWrite(swPin, HIGH); 39 40 FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS); 41 FastLED.setBrightness(BRIGHTNESS); 42 for( i=0; i<=255; i++ ){ 43 snake[i] = 0; 44 } 45 46 for( i=0; i<=snakeSize; i++ ){ 47 snake[i] = lastDirection+i; 48 } 49 50 f = random(0, 255); 51 FastLED.show(); 52} 53 54int Snakedirection(int last, int dX, int dY ){ 55 dX = map(dX, 0, 1000, -1, 1); 56 dY = map(dY, 0, 1000, -1, 1); 57 if(dX == 0 && dY == 0 && OlddX != dX){ 58 dX = OlddX; 59 } 60 if(dY == 0 && dX == 0 && OlddY != dY){ 61 dY = OlddY; 62 } 63 int newDirection = last; 64 if( dX != 0 ){ // moving in X direction 65 if ( row&1 ){ 66 if( col == 0 && dX == 1){ newDirection = last -15; } 67 else if( col == 15 && dX == -1){ newDirection = last +15; } 68 else newDirection = last + dX; // четная 69 } else { 70 if( col == 0 && dX == 1){ newDirection = last +15; } 71 else if( col == 15 && dX == -1 ){ newDirection = last -15; } 72 else newDirection = last - dX; // не четная 73 } 74 } 75 if( dY < 0){ // moving in Y DOWN direction 76 if(row == 15 && dY == -1){newDirection = col;} 77 else if ( row&1 ){ 78 newDirection = last + (col*2)+1; // четная 79 } else { 80 newDirection = last + (16-col-1)+(16-col); // не четная 81 } 82 } 83 if( dY > 0){ // moving in Y UP direction 84 if( row == 0 && dY == 1){ newDirection = 255 - col;} 85 else if ( row&1 ){ 86 newDirection = last - (last - 16*row) - (16 - col); // четная 87 } else { 88 newDirection = last - (col*2)-1; // не четная 89 } 90 } 91 OlddX = dX; 92 OlddY = dY; 93 return newDirection; 94} 95 96int snakeMove(int snakeDirection){ 97 98 for( i=0; i<=255; i++ ){ 99 if( snake[i] == snakeDirection ){ 100 death(); 101 } 102 } 103 104 FastLED.clear(); 105 for(i=snakeSize; i>=1; i--){ 106 snake[i] = snake[i-1]; 107 } 108 snake[0] = snakeDirection; 109 for( i=0; i<=255; i++ ){ 110 if( snake[i] ){ 111 leds[snake[i]].setRGB(red, green, blue); 112 } 113 } 114 FastLED.show(); 115 row = (int)(snakeDirection/16); // row number 116 if ( row&1 ){ 117 col = (row+1) * 16 - snakeDirection - 1; 118 } else { 119 col = snakeDirection - row * 16; 120 } 121 return snakeDirection; 122} 123 124void food( int eaten ){ 125 if( eaten == f ){ 126 snakeSize++; 127 f = random(0, 255); 128 red = fred; 129 green = fgreen; 130 blue = fblue; 131 fred = random(0, 255); 132 fgreen = random(0, 255); 133 fblue = random(0, 255); 134 snakeSpeed = snakeSpeed / 1.1; 135 } else { 136 leds[f].setRGB(fred, fgreen, fblue); 137 FastLED.show(); 138 } 139} 140 141void death(){ 142 snakeSize = 2; 143 snakeSpeed = 500; 144 red = 255; 145 green = 0; 146 blue = 0; 147} 148 149void color(boolean sw){ 150 if(!sw){ 151 152 red = random(0,255); 153 green = random(0,255); 154 blue = random(0,255); 155 156 } 157} 158 159void loop() { 160 color( digitalRead(swPin) ); 161 newDirection = Snakedirection(lastDirection, analogRead(pinX), analogRead(pinY)); 162 lastDirection = snakeMove(newDirection); 163 food(newDirection); 164 delay(snakeSpeed); 165}
Downloadable files
Schematics
Schematics
Comments
Only logged in users can leave comments