Barc Assembled | サイプレス セミコンダクタ
Barc (finally) got his nose back this week. I replaced the ugly yellow wire loops with some copper tape and added an Adafruit IR sensor that pokes out of his mouth. Subtle stuff! It is way easier to work on this guy now he is stretched out on one level. In my program I am going to detect the presence of a hand via the IR or CapSense sensors. I started with the new IR sensor, which is controlled via three wires.
The red wire is power and black is ground. I could just hook them directly to the power rails on the proto board but, since I may decide to turn the sensor on/off in future (and because I soldered a header to the wires), I am just connecting them to PSoC pins and will drive them from my program. The white wire is the analog input, which I am connecting to the PSoC ADC.
The I2C component (set to 400kbps data rate) is there so that I can send the ADC data to the PC via the kitprog (more in a while). I configured the ADC to average out noise and give me a steady stream of results. I only need one single-ended channel with a range of 0.0V to 5.5V that returns an averaged result of 256 samples.
I then chose pins P1, P1 and P1 for the sensor (ADC), ground and power respectively. The I2C goes on P4 and P4. My design was ready and I just needed to write a little code. I was not sure how sensitive the sensor would be so I decided to use our Bridge Control Panel to plot the ADC values in real time. The following code turns on the IR sensor and sets up the ADC.
Pin_IR_GND_Write( 0 ); // Make sure ground is low
Pin_IR_Power_Write( 1 ); // Turn on the sensor
CyDelay( IR_SENSOR_SETTLE_TIME ); // Allow time for sensor output to be valid
ADC_IR_Sensor_Start(); // Turn on the ADC
ADC_IR_Sensor_StartConvert(); // Start sampling (free running)
This turns on I2C and sets it up to communicate across the kitprog I2C-USB bridge to the PC.
#define I2CBUFSIZE (2)
static uint8 i2cbuf[I2CBUFSIZE];
EZI2C_Start(); // Turn on I2C (over kitprog bridge)
EZI2C_EzI2CSetBuffer1( I2CBUFSIZE, 0, i2cbuf ); // Set up the buffer for communication
I then put this code into the main loop. It just samples the ADC and writes the 16-bit values into the I2C buffer. The EZI2C component does the hard part - in the background it sends the buffer up to the Bridge Control Panel (BCP) software.
int16 range = 0; // ADC value
/* Get an ADC value from the distance sensor and convert it to millivolts */
if( ADC_IR_Sensor_IsEndConversion( ADC_IR_Sensor_RETURN_STATUS ) )
range = ADC_IR_Sensor_GetResult16( IR_SENSOR_CHANNEL );
range = ADC_IR_Sensor_CountsTo_mVolts( IR_SENSOR_CHANNEL, range );
i2cbuf = ( range >> 8 ) & 0xFF;
i2cbuf = ( range >> 0 ) & 0xFF;
Once I had this programmed I started up the BCP, selected the kitprog connection, and configured I2C to be 400kHz so that it matches the component speed. I was almost ready to look at the signal from the IR sensor. The PSoC sends 2 bytes and I needed to pack them back into a 16-bit value. So I created a variable, gave it the name "range", and made it 16-bits wide.
Back in the editor I write a command to read ('r') from address "08" into byte 1 "@1range" and byte 0 "@0range" of the range variable:
r 08 @1range @0range p
When I press the "Repeat" button and switch to the Chart view it shows me the ADC output in real-time.
You can see that there is a floor of about 1800mV and that, as I moved my hand in and out of range, the value peaked at about 3300V. There are two peaks because I moved my hand from afar, all the way into Barc's nose, and then back out again. If you get too close the IR sensor goes "blind" and the voltage drops. This is really good information because I now know that I can detect the presence of a hand by simply comparing the ADC voltage with a value of about 1800V plus 100mV for safety. That's a simple piece of code that I will add in a week or so... when I re-tune the CapSense sensors and create a state machine for moving the dog around.
Unfortunately, as you may have guessed, Barc missed his flight to Shenzen for Maker Faire. There was a bit of a mix up about when he needed to be ready and after all the arguing stopped there was just a really disappointed dog. So we promised ot take him back to Germany for Embedded World next year...