Yesterday, we have seen yesterday how to make a cheap and simple proximity sensor. Today this post is about reading the output given by the sensor, by removing the ambient light intensity from the total amount of light falling on the sensor and making the measurements more reliable with the surrounding lighting conditions.
This arduino code works well in different ambient light conditions. This code has control over the IR led too instead of keeping the led on forever. First the led is kept switched off and the intensity of light is measured, this being the ambient light intensity. Next the led is switched on and analog voltage is sampled from arduino's ADC pin now this being the amount of reflected light + ambient light. The difference between these values gives the amount of light reflected. This thing is repeated a few times and average amount is calculated as the final value. The code was actually taken from here. So you can take away code from there and modify it.
Now, as the sensor is ready to be used, we will try having fun controlling a game on PC (here Jetpack Joyride). A program continuously reads the sensor's output from Arduino via serial communication and simulates the space key press when there is a value from the sensor greater than a threshold value.
So here's a demo video...
And here's the code....
clear all
clc
%Your Arduino's COM port
s = serial('COM6');
%Importing java classes for simulating key press or release
import java.awt.Robot;
import java.awt.event.*;
SimKey=Robot;
%Threshold value for sensor
th = 100;
fopen(s);
s.BaudRate = 9600;
readasync(s)
while(1)
out = fscanf(s);
x = str2num(out)
%press Space if x > threshold and release if lesser
if (x > th)
SimKey.keyPress(java.awt.event.KeyEvent.VK_SPACE);
end
if (x<th)
SimKey.keyRelease(java.awt.event.KeyEvent.VK_SPACE);
end
end
So, all you need to do is connect the sensor to Arduino as shown in previous post and upload the code from mentioned link (here) into your arduino and run the above code in matlab, then start the game and have fun. Well, the code is very simple but is'nt it cool... I got the game for my PC from Windows 8 appstore... Hope you will find it for yours..
0 comments:
Post a Comment