Rube Goldberg machine

Dependencies:   4DGL-uLCD-SE mbed PinDetect

main.cpp

Committer:
nhardy6
Date:
2016-09-25
Revision:
6:8ec8b00d6d7c
Parent:
5:8ff6c76ead80

File content as of revision 6:8ec8b00d6d7c:

#include "mbed.h"
#include "PinDetect.h"
#include "uLCD_4DGL.h"

Timer t;    //timer to generate number

PinDetect begin(p8);  //start of it all
bool volatile stop = 0;
int num;
int smallFinale;
int inc = 1;

DigitalOut d1(p21); //display number in binary
DigitalOut d2(p22);
DigitalOut d3(p23);

uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;

AnalogIn finish(p15);

DigitalOut latch(p26);
DigitalOut enable(p27);
SPI spi(p5, p6, p7);

int red=0;  //for shiftbrite
int green=0;
int blue=0;

void show() {
    uLCD.cls();
    uLCD.locate(0,0);
    uLCD.text_width(2);
    uLCD.text_height(2);
    uLCD.printf("GRINDING GEARS...\n\n");
    wait(0.5);
    uLCD.printf("STAR \nSEARCHING...");
    wait(1);
}

void RGB_LED(int red, int green, int blue)
{
    unsigned int low_color=0;
    unsigned int high_color=0;
    high_color=(blue<<4)|((red&0x3C0)>>6);
    low_color=(((red&0x3F)<<10)|(green));
    spi.write(high_color);
    spi.write(low_color);
    latch=1;
    latch=0;
}

void start()    //make random numbers and fast lights until flip value
{
    while(!stop) {
        d1 = !d1;
        d2 = !d2;
        d3 = !d3;
        num = t.read_us()%8;
        wait(0.2);
    }
}

void stopper(void)     //flip the value
{
    stop = 1;
}

void lights()
{
    if (num > 3) {
        d3 = 1;
    } else {
        d3 = 0;
    }
    if (num == 2 || num == 3 || num == 6 || num == 7) {
        d2 = 1;
    } else {
        d2 = 0;
    }
    if ((num % 2) == 1) {
        d1 = 1;
    } else {
        d1 = 0;
    }
}

void bigFinale(int a, int b)
{
    red = a;
    green = b;
    blue = 0;
    while (true) {
        RGB_LED(red, green, blue);
        blue = blue + inc;
        if (blue == 0 || blue == 256) {
            inc = -inc;
        }
        wait(0.01);
    }
}

int main()
{
    t.start();              //start timer
    uLCD.cls();             //prepare screen
    wait(0.1);
    spi.format(16,0);       //setup spi
    spi.frequency(500000);
    enable=0;
    latch=0;
    wait(2);
    uLCD.locate(0,0);
    uLCD.color(WHITE);
    begin.mode(PullUp);     //attach pushbutton
    wait(.01);              //setup delay
    begin.attach_deasserted(&stopper);
    begin.setSampleFrequency();
    uLCD.text_width(3);
    uLCD.text_height(3);
    uLCD.printf(" FIRE\n WHEN\n READY");
    start();                //begin the choosing
    lights();               //light lights according to generated number
    show();
    uLCD.cls();
    uLCD.locate(0,0);
    uLCD.printf("You got the number");
    uLCD.text_width(5);
    uLCD.text_height(5);
    uLCD.locate(1,1);
    uLCD.printf("%2D",num);
    uLCD.text_width(1);
    uLCD.text_height(1);
    uLCD.locate(0,13);
    uLCD.printf("The lights knew\n this would happen");
    wait(4);
    
    smallFinale = 256*finish.read();
    uLCD.cls();
    uLCD.text_width(3);
    uLCD.text_height(3);
    uLCD.locate(0,0);
    uLCD.printf(" YOUR\nSTATS:");
    uLCD.text_width(2);
    uLCD.text_height(2);
    uLCD.locate(1,5);
    uLCD.color(RED);
    uLCD.printf("\t%2D",num*256/7);
    uLCD.color(GREEN);
    uLCD.printf("\t %2D",smallFinale);
    bigFinale(num*256/7,smallFinale); //output branch numbers to light
}