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

main.cpp

Committer:
hdao9
Date:
2018-04-17
Revision:
0:f0807589902f
Child:
1:85d63d7193e3

File content as of revision 0:f0807589902f:

#include "mbed.h"
#include "NeoStrip.h"
#include "PinDetect.h"

#define N 64
#define PATTERNS 3

NeoStrip strip(p18, N);

PinDetect right(p5);
PinDetect down(p6);
PinDetect left(p7);
PinDetect center(p8);
PinDetect up(p9);

DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
 
int pos = 0;

int red = 0xFF0000;
int blue = 0x0000FF;
int color = red;

void alloff()
{
    for (int i = 0; i < N; i++)
        strip.setPixel(i, 0, 0, 0);
    strip.write();
}

void right_hit_callback (void) { //move to the flashing LED to the right
    //led1 = !led1;
    strip.setPixel(pos, 0, 0, 0);
    if (pos < 7) pos = pos + 1;
}
void left_hit_callback (void) {  //move the flashing LED to the left
    //led3 = !led3;
    strip.setPixel(pos, 0, 0, 0);
    if (pos > 0) pos = pos - 1;
}
void down_hit_callback (void) {
    led2 = !led2;
}

//A random position in a 8x8 matrix have the neopixel index position: (row-1)*8 + col - 1   //with row and col start from 1
void center_hit_callback (void) { //drop the coin
    //led1 = 0; led2 = 0; led3 = 0; led4 = 0;
    // in order to drop a coin, first the flashing on top row need to disappear
    strip.setPixel(pos, 0, 0, 0);
    //then coin need to appear at the current column (which can be get from "pos"), on the lowest row not occupied
    //how to keep track of occupied row?
    col = pos + 1;
    for (int r = 1 to 8)
    {
        if (myMatrix.getNumber(r,col) == 0) {row = r; break for;} //have to break out of for loop becuz only want the first empty row
    }
    //got col from pos, once got row, then can convert to index position and light up led at that position
    strip.setPixel((row-1)*8 + col - 1, color);
    //after added LED to that position
    myMatrix.add( row, col, 1); //now also have a matrix to check winner
    //there will be no dropping affect to avoid having to temporary stop the while loop in main
    //change player/color
    if (color == red) color = blue;
    else color = red;
}
void up_hit_callback (void) {
    led4 = !led4;
}

void one()  //turn on one at a time to test strip
{
    for (int i = 0; i < N; i++)
    {
        strip.setPixel(i, (uint8_t)(255), 0, 0);
        strip.write();
        wait(1);
    }
}

int main()
{
    // Use internal pullups for pushbutton
    right.mode(PullUp);    
    down.mode(PullUp);
    left.mode(PullUp);    
    center.mode(PullUp);
    up.mode(PullUp);    
    // Delay for initial pullup to take effect
    wait(.05);
    // Setup Interrupt callback functions for a pb hit
    right.attach_deasserted(&right_hit_callback); //used to move coin
    down.attach_deasserted(&down_hit_callback); // not used
    left.attach_deasserted(&left_hit_callback);  //used to move coin
    center.attach_deasserted(&center_hit_callback); //used to drop coin
    up.attach_deasserted(&up_hit_callback);  // not used
    // Start sampling pb inputs using interrupts
    right.setSampleFrequency();
    down.setSampleFrequency();
    left.setSampleFrequency();
    center.setSampleFrequency();
    up.setSampleFrequency();
    wait(.01);
    
    float bright = 0.2; // 20% is plenty for indoor use
    strip.setBrightness(bright);    // set default brightness
    alloff();
    
    while(1) //flashing LED
    {
        strip.setPixel(pos, color);
        strip.write();
        wait(0.2);
        strip.setPixel(pos, 0, 0, 0);
        strip.write();
        wait(0.2);
    }
    
}