tweaked detection to use photoresistor through an opamp

Dependents:   rgb_sensor_buffer

Fork of rgb_sensor by Milosch Meriac

Revision:
6:fc64a14a2f4a
Parent:
5:1fed2b68e661
Child:
8:88acb970df76
--- a/rgb_sensor.cpp	Sat Jun 28 06:59:51 2014 +0000
+++ b/rgb_sensor.cpp	Sat Jun 28 16:02:24 2014 +0000
@@ -109,7 +109,7 @@
 int RGB_Sensor::filter(int sample)
 {
     int a,b,x,y,z;
-    
+
     /* get the two previous samples */
     a = m_adc_filter[m_rgb_channel][0];
     b = m_adc_filter[m_rgb_channel][1];
@@ -121,7 +121,6 @@
 
     /* remember current sample */
     m_adc_filter[m_rgb_channel][m_adc_filter_pos] = sample;
-    m_adc_filter_pos ^= 1;
 
     /* choose edge with shortest distance and
      * return average of the two edge nodes */
@@ -134,7 +133,8 @@
 
 void RGB_Sensor::adc_irq(void)
 {
-    uint32_t sample, status;
+    int sample;
+    uint32_t status;
 
     status = LPC_ADC->ADSTAT;
     if(status & (1UL<<m_adc_channel))
@@ -167,11 +167,13 @@
 
                     case 3:
                         m_green = !RGB_LED_ON;
-                        m_blue= RGB_LED_ON;
+                        m_blue = RGB_LED_ON;
                         break;
 
                     default:
-                        m_blue= !RGB_LED_ON;
+                        m_blue = !RGB_LED_ON;
+                        m_rgb_channel = 0;
+
                         if(!m_callback)
                             m_done = true;
                         else
@@ -179,13 +181,15 @@
                             TRGB rgb;
                             convert(rgb);
                             m_done = !m_callback(rgb);
-                            m_rgb_channel = 0;
                         }
                         /* stop data aquisition */
                         if(m_done)
                             LPC_ADC->ADCR &= ~(1UL<<16);
                         else
+                        {
+                            m_adc_filter_pos ^= 1;
                             memset(&m_adc_aggregation, 0, sizeof(m_adc_aggregation));
+                        }
                 }                        
             }
         }
@@ -201,9 +205,8 @@
 
     m_callback = callback;
     m_done = false;
-    m_adc_count = m_rgb_channel = 0;
+    m_adc_filter_pos = m_adc_count = m_rgb_channel = 0;
     memset((void*)&m_adc_aggregation, 0, sizeof(m_adc_aggregation));
-    m_adc_filter_pos = 0;
     memset(&m_adc_filter, 0, sizeof(m_adc_filter));
 
     /* start ADC burst mode */