Devices & Components
Arduino Mega 2560 Rev3
Breadboard - 840 contacts
Too many cables...
Joystick Module Dual Axis
Mini breadboard - White
Resistor 1k Ohm
Dot Matrix LED 8×8 1088BS
Hardware & Tools
Hands
Software & Tools
Arduino IDE
Project description
Code
moving_pixel
csharp
Code for the project
1//Joystick configuration 2#define ANALOG_X_PIN A0 3#define ANALOG_Y_PIN A1 4#define ANALOG_BUTTON_PIN A2 5#define ANALOG_X_CORRECTION 125 6#define ANALOG_Y_CORRECTION 128 7#define MOVE_THRESHOLD 50 // minimal value from joystick to move 8#define MOVING_TIME 500 //Time in miliseconds 9 10struct button { 11 byte pressed = 0; 12}; 13 14struct analog { 15 short x, y; 16 17 button button; 18}; 19 20struct position { 21 short x, y; 22}; 23 24position currentPosition{ 0, 0 }; 25 26//Dot matrix 8x8 config 27//ROW x Arduino PIN for example row[0] = PIN 22, row[5] = PIN 9 28const int row[8] = { 29 22, 34, 2, 28, 9, 3, 8, 5 30}; 31//COLUMN x Arduino PIN for example col[0] = PIN 32, row[5] = PIN 4 32const int col[8] = { 33 32, 7, 6, 24, 4, 26, 36, 38 34}; 35 36void setup() { 37 pinMode(ANALOG_BUTTON_PIN, INPUT_PULLUP); 38 39 resetPosition(currentPosition); 40 41 Serial.begin(9600); 42} 43 44void loop() { 45 analog analog; 46 47 analog.x = readAnalogAxisLevel(ANALOG_X_PIN) - ANALOG_X_CORRECTION; 48 analog.y = readAnalogAxisLevel(ANALOG_Y_PIN) - ANALOG_Y_CORRECTION; 49 analog.y = analog.y * -1; //reverse axis 50 51 analog.button.pressed = isAnalogButtonPressed(ANALOG_BUTTON_PIN); 52 53 Serial.print("Analog X:"); 54 Serial.println(analog.x); 55 56 Serial.print("Analog Y:"); 57 Serial.println(analog.y); 58 59 if (analog.button.pressed) { 60 Serial.println("Button pressed"); 61 resetPosition(currentPosition); 62 } else { 63 Serial.println("Button not pressed"); 64 } 65 66 move(analog.x, analog.y); 67 68 Serial.println("Current Dot Position x:" + (String)currentPosition.x + " y:" + (String)currentPosition.y); 69 70 delay(MOVING_TIME); 71} 72 73byte readAnalogAxisLevel(int pin) { 74 return map(analogRead(pin), 0, 1023, 0, 255); 75} 76 77bool isAnalogButtonPressed(int pin) { 78 return digitalRead(pin) == 0; 79} 80 81void resetPosition(position &pos) { 82 for (int thisPin = 0; thisPin < 8; thisPin++) { 83 pinMode(col[thisPin], OUTPUT); 84 pinMode(row[thisPin], OUTPUT); 85 digitalWrite(col[thisPin], HIGH); 86 digitalWrite(row[thisPin], LOW); 87 } 88 89 digitalWrite(row[0], HIGH); 90 digitalWrite(col[0], LOW); 91 pos.x = 0; 92 pos.y = 0; 93} 94 95void move(short x, short y) { 96 position oldPosition = currentPosition; 97 98 if (x > MOVE_THRESHOLD && oldPosition.x < 7) { 99 digitalWrite(col[oldPosition.x + 1], LOW); 100 digitalWrite(col[oldPosition.x], HIGH); 101 currentPosition.x = oldPosition.x + 1; 102 } 103 104 if (x < -1 * MOVE_THRESHOLD && oldPosition.x > 0) { 105 digitalWrite(col[oldPosition.x - 1], LOW); 106 digitalWrite(col[oldPosition.x], HIGH); 107 currentPosition.x = oldPosition.x - 1; 108 } 109 110 if (y < -1 * MOVE_THRESHOLD && oldPosition.y < 7) { 111 digitalWrite(row[oldPosition.y + 1], HIGH); 112 digitalWrite(row[oldPosition.y], LOW); 113 currentPosition.y = oldPosition.y + 1; 114 } 115 116 if (y > MOVE_THRESHOLD && oldPosition.y > 0) { 117 digitalWrite(row[oldPosition.y - 1], HIGH); 118 digitalWrite(row[oldPosition.y], LOW); 119 currentPosition.y = oldPosition.y - 1; 120 } 121}
Comments
Only logged in users can leave comments