// Aubrey Simonson // asimonso@mit.edu // How to Make (almost) Anything // December 16th, 2019 // // (c) Aubrey Simonson 2019 // This work may be reproduced, modified, distributed, // performed, and displayed for any purpose. Copyright is // retained and must be preserved. The work is provided // as is; no warranty is provided, and users accept all // liability. // // //Adapted from Ivan Sanchez //link to original: http://fab.academany.org/2018/labs/fablaboulu/students/heidi-hartikainen/W9.html //You made PA2 and PA3 always be on. PB2 will be turned on if power is supplied to PA7 // #ifndef F_CPU #define F_CPU 20000000UL // 20 MHz clock speed #endif //these two lines taken from hello echo board #define serial_port PORTA #define serial_pin_out (1 << PA1)//this may actually be PA0. test that if it doesn't work. #include //Library to handle avr inputs and outputs #include // Library to add delays. Necessary for blinking. //mysterious helper function taken from hello echo board void put_string(volatile unsigned char *port, unsigned char pin, char *str) { // // print a null-terminated string // static int index; index = 0; do { put_char(port, pin, str[index]); ++index; } while (str[index] != 0); } int main(void) { // DDRB Port B Data Direction Register. Selects direction of pins. //This sets pins PB2, PA3, PA2, and PA7 as inputs DDRB = DDRB | (1 << PB2); DDRA = DDRA | (1 << PA3); DDRA = DDRA | (1 << PA2); DDRA = DDRA | (1 << PA7); //this is how we will record which pins are high and which are low. //each digit coresponds to a pad on the sensor. //yes, in c, a string isn't a string, it's an array of characters. //which it is in most languages, technically, but you never have to interact with //a string as a char array in the same way that in the real world we never have to //engage with individual atoms god why is c like this. char cube_reading[] = {'0', '0', '0', '0'}; while (1) { //for each of our 4 pins, check if they are high or low. //if they are high, set the corresponding character of cube_reading to 1. //if they are low, set the corresponding character of cube_reading to 0. uint8_t val = PINB & (1 << PB2); if(!val){ cube_reading[0] = '1'; } else { cube_reading[0] = '0'; } uint8_t val = PINA & (1 << PA3); if(!val){ cube_reading[1] = '1'; } else { cube_reading[1] = '0'; } uint8_t val = PINA & (1 << PA2); if(!val){ cube_reading[2] = '1'; } else { cube_reading[2] = '0'; } uint8_t val = PINA & (1 << PA7); if(!val){ cube_reading[3] = '1'; } else { cube_reading[3] = '0'; } //communicate cube_reading to another device put_string(&serial_port, serial_pin_out, "cube reader: " + cube_reading); } }