Sensor Example Code

int main(void)
{

uint8_t display_distance = true;
uint8_t intensity_level;
uint8_t power_color = 255;
cr8 = cr8_alloc();
cr8_init(cr8);
cr8_adc_enable(ADC_CENTER_1); // distance sensor
cr8_adc_enable(ADC_CENTER_2); // light sensor

// run this program forever.
while(1)
{
cr8_update(cr8);

// use the play button to switch between light/distance sensor
if(cr8->button_play)
{
display_distance = (display_distance + 1) % 2; // switch between true/false
while(cr8->button_play)
{ // wait until user has let go of button
cr8_update(cr8);
cr8_delay(15);
}
}

// display external sensor readings on cricket led and power led
if(display_distance)
{ // use distance sensor
lcd_printf("%d", cr8->adc_center1); // dist sensor, unsigned, decimal
intensity_level = cr8->adc_center1 / 4; // max 1023 / 4 - 1 = 255
power_color = 255;
}
else
{ // use light sensor
lcd_printf("%d", cr8->adc_center2); // light sensor, unsigned, decimal
intensity_level = cr8->adc_center2 / 4; // max 1023 / 4 - 1 = 255
power_color = 0;
}

cr8_set_leds(false, false, power_color, intensity_level);
cr8_delay(250);
}

cr8_free(cr8);
return(0);
}

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License