// gets the serial zones and then updates the sprinklerZones array void getZonesFromSerial(int sprinklerZoneCount, int sprinklerZones[6], unsigned char confirmationChar) { int i = 0; for (i = 0; i < sprinklerZoneCount; i++) { unsigned char rawIn = usart_in(); if (rawIn == 48) { //0 sprinklerZones[i] = 0; } else if (rawIn == 49) { //1 sprinklerZones[i] = 1; } // otherwise doesn't modify the value } // prints out a confirmation that it sent correctly usart_out(confirmationChar); }
int main(void) { // sets all our IO bits // sets up communication with raspberry pi over serial usart_init(47); const int sprinklerZoneCount = 6; int sprinklerZones[sprinklerZoneCount]; initSprinklerZones(sprinklerZoneCount, sprinklerZones); //initializes sprinkler zones to 0 setSprinklerZones(sprinklerZoneCount, sprinklerZones); _delay_ms(10); // SERIAL VARIABLES unsigned char zoneUpdate = 122; //'z' if we get this, we know to update the zones unsigned char confirmationChar = 97; //'a' sends this back on good data received unsigned char getMoist = 98; //forever loop while (1) { // checks for a serial value to come across that corresponds to the zones to be on unsigned char rawInput = usart_in(); if (rawInput == zoneUpdate) { getZonesFromSerial(sprinklerZoneCount, sprinklerZones, confirmationChar); setSprinklerZones(sprinklerZoneCount, sprinklerZones); } else if (rawInput == getMoist) { getMoistureLevel(); } _delay_ms(10); // delays for 10ms between iterations } return 0; /* never reached */ }
int main() { /* * used by rotary encoder int rot_a = 0; int rot_b = 0; int prev_val = 0; */ /* Initialize Clock Rate */ usart_init(47); shift_init(); DDRD &= (1 << PD5); // Set PORTD bit 5 for input, take in rotary bit A DDRD &= (1 << PD6); // Set PORTD bit 6 for input, take in rotary bit B PORTD |= (1 << PD5); // Activate pull-ups in PORTD pi PORTD |= (1 << PD6); // Activate pull-ups in PORTD pi /* main loop * every cycle, read at the beginning, write at the end * */ uint8_t zone_reg = 0; while (1) { int temperature = get_temperature(); int moisture = get_moisture(); char zone_char = usart_in(); usart_putchar('\n'); char temp_write_buf[36]; snprintf(temp_write_buf, 36, "Temp: %ld F, Moisture: %d, ", CtoF(temperature), moisture); usart_out(temp_write_buf); _delay_ms(200); /** * Rotary Encoder - Deprecated * Read inputs rot_a = (PIND & (1 << PD5)); // Read value on PD5 rot_b = (PIND & (1 << PD6)); // Read value on PD6 if ((prev_val == 0) && (rot_a == 32)) { if (rot_b == 64) { zone_char--; } else { zone_char++; } } prev_val = rot_a; */ /* we only accept zones 1 through 8, 0 to turn off */ char zone_write_buf[17]; if (zone_char >= '0' && zone_char <= '8') { snprintf(zone_write_buf, 17, "Current Zone: %c\r\n", zone_char); usart_out(zone_write_buf); _delay_ms(200); if (zone_char > '0') { uint8_t zone_onehot = (uint8_t) (1 << (zone_char - '1')); shift_write(zone_onehot); zone_reg = (uint8_t) (zone_char - '0'); } else { if (zone_char == '0') { zone_reg = 0; } shift_write(0); } } else { snprintf(zone_write_buf, 17, "Current Zone: %c\r\n", (char) (zone_reg + '0')); usart_out(zone_write_buf); _delay_ms(200); } } return 0; }