arduino code:
// include the library code: #include LiquidCrystal.h // initialize the library with the numbers of the interface pins LiquidCrystal lcd(2,4,8,9,10,11); int Contrast=20; void setup() { analogWrite(6,Contrast); Serial.begin(9600); // set up the LCD's number of columns and rows: lcd.begin(16, 2); lcd.clear(); } void loop() { if (Serial.available() > 0) { int d1 = Serial.parseInt(); int d2 = Serial.parseInt(); // d2 is important here so don't remove this lcd.setCursor(0,0); // Sets the cursor to col 0 and row 0 lcd.print("Count Value: "); lcd.print(d1); } }
MATLAB code:
number = 0; % this is used in next section to count total counts portName = 'COM5'; s = serial(portName,'BaudRate',9600,'Terminator','LF'); s.timeout = 1; try try fopen(s); catch delete(s); fopen(s); end catch disp('Unable to open the port '); end
%% this is my red color detection code. function gesturerecognisition(redThresh,numFrame) warning('off','vision:transition:usesOldCoordinates');
if nargin < 1 redThresh = 0.22; numFrame = 400; end
cam = imaqhwinfo; cameraName = char(cam.InstalledAdaptors(end)); cameraInfo = imaqhwinfo(cameraName); cameraId = cameraInfo.DeviceInfo.DeviceID(end); cameraFormat = char(cameraInfo.DeviceInfo.SupportedFormats(end));
vidDevice = imaq.VideoDevice(cameraName, cameraId, cameraFormat, ... 'ReturnedColorSpace', 'RGB');
vidInfo = imaqhwinfo(vidDevice); screenSize = get(0,'ScreenSize'); hblob = vision.BlobAnalysis('AreaOutputPort', false, ... 'CentroidOutputPort', true, ... 'BoundingBoxOutputPort', true', ... 'MaximumBlobArea', 3000, ... 'MinimumBlobArea', 100, ... 'MaximumCount', 3); hshapeinsBox = vision.ShapeInserter('BorderColorSource', 'Input port', ... 'Fill', true, ... 'FillColorSource', 'Input port', ... 'Opacity', 0.4); hVideoIn = vision.VideoPlayer('Name', 'Final Video', ... 'Position', [100 100 vidInfo.MaxWidth+20 vidInfo.MaxHeight+30]); nFrame = 0; count=0;
while (nFrame < numFrame) rgbFrame = step(vidDevice); rgbFrame = flipdim(rgbFrame,2); diffFrameRed = imsubtract(rgbFrame(:,:,1), rgb2gray(rgbFrame)); binFrameRed = im2bw(diffFrameRed, redThresh); [centroidRed, bboxRed] = step(hblob, binFrameRed);
    if length(bboxRed(:,1)) == 1
count=1;  
    end
    vidIn = step(hshapeinsBox, rgbFrame, bboxRed,single([1 0 0]));
    step(hVideoIn, vidIn);
    nFrame = nFrame+1;
end
release(hVideoIn); release(vidDevice); clc; end
fprintf(s,'%d\n',number);
%% % this section used to close com port try fclose(s); try delete(instrfind('port','COM5')); catch end catch disp('Unable to close the port '); end