//this one makes patterns WHEN you press a button
//different pattern depending on the button
//but you can't press two buttons at once and get the same thing


//This is meant to run on an ATMEL ATMega328

//I think these are libraries to include
#include <avr/io.h>
#include <util/delay.h>

//these are functions you'll call later
#define output(directions,pin) (directions |= pin) // set port direction for output
#define input(directions,pin) (directions &= (~pin)) // set port direction for input
#define set(port,pin) (port |= pin) // set port pin
#define clear(port,pin) (port &= (~pin)) // clear port pin
#define pin_test(pins,pin) (pins & pin) // test for port pin
#define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set

//these depend on how you set up the board
//inputs = buttons
#define button_port PORTC
#define button_direction DDRC //do I need this?
#define button1 (1 << PC0)
#define button2 (1 << PC1)
#define button3 (1 << PC2)
#define input_pins PINC //All my inputs are in C because I am just such a thoughtful circuit designer

//outputs = rgb led
#define led_port PORTB
#define led_direction DDRB
#define red (1 << PB2)
#define green (1 << PB1)
#define blue (1 << PB0)

//these are some helpful values you'll want later
#define led_delay() _delay_ms(100) // LED delay
#define PWM_delay() _delay_us(25) // PWM delay

int main (void){ //this is the main function - i.e. what runs all the time
    //int means it returns an integer... though ours doesn't actually return anything
    //I think you just put int
    //void means this main function has no inputs
    
    // set clock divider to /1
    // I don't entirely understand this, I just know it's in all the programs
    CLKPR = (1 << CLKPCE);
    CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
    
    //so you want this program to respond to buttons being pushed
    //and it'll show a different color of LED when each button is pressed
    
    //first initialize the buttons
    clear(button_port, button1 | button2 | button3);
    input(button_direction, button1 | button2 | button3);
    
    //and initialize the LED
    clear(led_port, red | green | blue); // | is bitwise operation, || is logical OR
    output(led_direction, red | green | blue);
   
   //now you want it listening for these buttons
   //the buttons are connected to ground on one end, and to the IC on the other end
   //so, you want the ports to listen for when they become ground, because it means the button is pressed
   
   set(button_port, button1 | button2 | button3);
   
   while (1){ //meaning, all the time
    
    if (pin_test(input_pins,button1)){
        set(led_port,red);
    } else {
        clear(led_port,red);
    }

	if (pin_test(input_pins,button2)){
        set(led_port,green);
    } else {
        clear(led_port,green);
    }
	if (pin_test(input_pins,button3)){
        set(led_port,blue);
    } else {
        clear(led_port,blue);
    }
   }

   
}
