Connect 4 game using the NeoPixel Matrix 8x8 RGB LED and a 5-way Tactile Navigation Switch.

Dependencies:   Matrix PinDetect mbed

Fork of 4180_Project_Finalcode by 4180

Committer:
hdao9
Date:
Tue Apr 17 18:42:39 2018 +0000
Revision:
0:f0807589902f
Child:
1:85d63d7193e3
1st rev

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hdao9 0:f0807589902f 1 #include "mbed.h"
hdao9 0:f0807589902f 2 #include "NeoStrip.h"
hdao9 0:f0807589902f 3 #include "PinDetect.h"
hdao9 0:f0807589902f 4
hdao9 0:f0807589902f 5 #define N 64
hdao9 0:f0807589902f 6 #define PATTERNS 3
hdao9 0:f0807589902f 7
hdao9 0:f0807589902f 8 NeoStrip strip(p18, N);
hdao9 0:f0807589902f 9
hdao9 0:f0807589902f 10 PinDetect right(p5);
hdao9 0:f0807589902f 11 PinDetect down(p6);
hdao9 0:f0807589902f 12 PinDetect left(p7);
hdao9 0:f0807589902f 13 PinDetect center(p8);
hdao9 0:f0807589902f 14 PinDetect up(p9);
hdao9 0:f0807589902f 15
hdao9 0:f0807589902f 16 DigitalOut led1(LED1);
hdao9 0:f0807589902f 17 DigitalOut led2(LED2);
hdao9 0:f0807589902f 18 DigitalOut led3(LED3);
hdao9 0:f0807589902f 19 DigitalOut led4(LED4);
hdao9 0:f0807589902f 20
hdao9 0:f0807589902f 21 int pos = 0;
hdao9 0:f0807589902f 22
hdao9 0:f0807589902f 23 int red = 0xFF0000;
hdao9 0:f0807589902f 24 int blue = 0x0000FF;
hdao9 0:f0807589902f 25 int color = red;
hdao9 0:f0807589902f 26
hdao9 0:f0807589902f 27 void alloff()
hdao9 0:f0807589902f 28 {
hdao9 0:f0807589902f 29 for (int i = 0; i < N; i++)
hdao9 0:f0807589902f 30 strip.setPixel(i, 0, 0, 0);
hdao9 0:f0807589902f 31 strip.write();
hdao9 0:f0807589902f 32 }
hdao9 0:f0807589902f 33
hdao9 0:f0807589902f 34 void right_hit_callback (void) { //move to the flashing LED to the right
hdao9 0:f0807589902f 35 //led1 = !led1;
hdao9 0:f0807589902f 36 strip.setPixel(pos, 0, 0, 0);
hdao9 0:f0807589902f 37 if (pos < 7) pos = pos + 1;
hdao9 0:f0807589902f 38 }
hdao9 0:f0807589902f 39 void left_hit_callback (void) { //move the flashing LED to the left
hdao9 0:f0807589902f 40 //led3 = !led3;
hdao9 0:f0807589902f 41 strip.setPixel(pos, 0, 0, 0);
hdao9 0:f0807589902f 42 if (pos > 0) pos = pos - 1;
hdao9 0:f0807589902f 43 }
hdao9 0:f0807589902f 44 void down_hit_callback (void) {
hdao9 0:f0807589902f 45 led2 = !led2;
hdao9 0:f0807589902f 46 }
hdao9 0:f0807589902f 47
hdao9 0:f0807589902f 48 //A random position in a 8x8 matrix have the neopixel index position: (row-1)*8 + col - 1 //with row and col start from 1
hdao9 0:f0807589902f 49 void center_hit_callback (void) { //drop the coin
hdao9 0:f0807589902f 50 //led1 = 0; led2 = 0; led3 = 0; led4 = 0;
hdao9 0:f0807589902f 51 // in order to drop a coin, first the flashing on top row need to disappear
hdao9 0:f0807589902f 52 strip.setPixel(pos, 0, 0, 0);
hdao9 0:f0807589902f 53 //then coin need to appear at the current column (which can be get from "pos"), on the lowest row not occupied
hdao9 0:f0807589902f 54 //how to keep track of occupied row?
hdao9 0:f0807589902f 55 col = pos + 1;
hdao9 0:f0807589902f 56 for (int r = 1 to 8)
hdao9 0:f0807589902f 57 {
hdao9 0:f0807589902f 58 if (myMatrix.getNumber(r,col) == 0) {row = r; break for;} //have to break out of for loop becuz only want the first empty row
hdao9 0:f0807589902f 59 }
hdao9 0:f0807589902f 60 //got col from pos, once got row, then can convert to index position and light up led at that position
hdao9 0:f0807589902f 61 strip.setPixel((row-1)*8 + col - 1, color);
hdao9 0:f0807589902f 62 //after added LED to that position
hdao9 0:f0807589902f 63 myMatrix.add( row, col, 1); //now also have a matrix to check winner
hdao9 0:f0807589902f 64 //there will be no dropping affect to avoid having to temporary stop the while loop in main
hdao9 0:f0807589902f 65 //change player/color
hdao9 0:f0807589902f 66 if (color == red) color = blue;
hdao9 0:f0807589902f 67 else color = red;
hdao9 0:f0807589902f 68 }
hdao9 0:f0807589902f 69 void up_hit_callback (void) {
hdao9 0:f0807589902f 70 led4 = !led4;
hdao9 0:f0807589902f 71 }
hdao9 0:f0807589902f 72
hdao9 0:f0807589902f 73 void one() //turn on one at a time to test strip
hdao9 0:f0807589902f 74 {
hdao9 0:f0807589902f 75 for (int i = 0; i < N; i++)
hdao9 0:f0807589902f 76 {
hdao9 0:f0807589902f 77 strip.setPixel(i, (uint8_t)(255), 0, 0);
hdao9 0:f0807589902f 78 strip.write();
hdao9 0:f0807589902f 79 wait(1);
hdao9 0:f0807589902f 80 }
hdao9 0:f0807589902f 81 }
hdao9 0:f0807589902f 82
hdao9 0:f0807589902f 83 int main()
hdao9 0:f0807589902f 84 {
hdao9 0:f0807589902f 85 // Use internal pullups for pushbutton
hdao9 0:f0807589902f 86 right.mode(PullUp);
hdao9 0:f0807589902f 87 down.mode(PullUp);
hdao9 0:f0807589902f 88 left.mode(PullUp);
hdao9 0:f0807589902f 89 center.mode(PullUp);
hdao9 0:f0807589902f 90 up.mode(PullUp);
hdao9 0:f0807589902f 91 // Delay for initial pullup to take effect
hdao9 0:f0807589902f 92 wait(.05);
hdao9 0:f0807589902f 93 // Setup Interrupt callback functions for a pb hit
hdao9 0:f0807589902f 94 right.attach_deasserted(&right_hit_callback); //used to move coin
hdao9 0:f0807589902f 95 down.attach_deasserted(&down_hit_callback); // not used
hdao9 0:f0807589902f 96 left.attach_deasserted(&left_hit_callback); //used to move coin
hdao9 0:f0807589902f 97 center.attach_deasserted(&center_hit_callback); //used to drop coin
hdao9 0:f0807589902f 98 up.attach_deasserted(&up_hit_callback); // not used
hdao9 0:f0807589902f 99 // Start sampling pb inputs using interrupts
hdao9 0:f0807589902f 100 right.setSampleFrequency();
hdao9 0:f0807589902f 101 down.setSampleFrequency();
hdao9 0:f0807589902f 102 left.setSampleFrequency();
hdao9 0:f0807589902f 103 center.setSampleFrequency();
hdao9 0:f0807589902f 104 up.setSampleFrequency();
hdao9 0:f0807589902f 105 wait(.01);
hdao9 0:f0807589902f 106
hdao9 0:f0807589902f 107 float bright = 0.2; // 20% is plenty for indoor use
hdao9 0:f0807589902f 108 strip.setBrightness(bright); // set default brightness
hdao9 0:f0807589902f 109 alloff();
hdao9 0:f0807589902f 110
hdao9 0:f0807589902f 111 while(1) //flashing LED
hdao9 0:f0807589902f 112 {
hdao9 0:f0807589902f 113 strip.setPixel(pos, color);
hdao9 0:f0807589902f 114 strip.write();
hdao9 0:f0807589902f 115 wait(0.2);
hdao9 0:f0807589902f 116 strip.setPixel(pos, 0, 0, 0);
hdao9 0:f0807589902f 117 strip.write();
hdao9 0:f0807589902f 118 wait(0.2);
hdao9 0:f0807589902f 119 }
hdao9 0:f0807589902f 120
hdao9 0:f0807589902f 121 }