Hello, i really need help. I got my arduino UNO last month. The project is to control a TV with gesture recognition using MATLAB and arduino connected through the USB. The matlab has the task of image acquisition and database matching. After obtaining a match, MATLAB sends a corresponding ASCII value to Arduino and the Arduino generates the corresponding IR signal through the IR LED for the philips RC-5 protocol.
All this worked fine till now and we thought of adding an additional circuitry in arduino. This section has an LDR circuit connected to the analog pin 0. It's function is to detect any changes in the voltage values of LDR and send a triggering ASCII value to MATLAb through USB to start the programme in MATLAB. (MATLAB will be looping and waiting for some value to come from Arduino).
Here is where the problem started.
int nvm;
int analogPino=0;
void setup()
{
pinMode(12,OUTPUT);
Serial.begin(9600); //start serial port at 9600 bps baud
}
void loop()
{
while(analogRead(analogPino)>100)
{
;
} // Keep looping till analog reading goes below 100
Serial.print('3'); //send 3 to trigger MATLAb out of the loop to continue the MATLAB code execution
while(Serial.available() == 0)
{
;
} //keep looping till MATLAB sends a byte after database matching
nvm = Serial.read(); //read from the buffer after recieving
// this following part of the code is instead of the IR LED blinking for the RC-5 protocol
if (nvm == 'N')
{
digitalWrite(12,HIGH);
delay(5000); // Stay HIGH for 5 seconds if 'N'
digitalWrite(12,LOW);
delay(1000);
}
else if (nvm == 'D')
{
digitalWrite(12,HIGH);
delay(1000);
digitalWrite(12,LOW); // stay HIGH for 1 second if 'D'
delay(1000);
}
}
MATLAB code
clc;
close all;
clear all;
%define serial port
s = serial('COM2');
s.BaudRate = 9600; % define baud rate
set(s, 'terminator', 'CR/LF'); % define the terminator
fopen(s); %open serial port
pause(2);
fprintf('waiting\r\n'); %prints in matlab
while s.BytesAvailable==0
end %Wait for something to come from arduino after LDR triggering
fclose(s); %close serial port
fprintf('got from the LDR\r\n'); %prints in matlab
% the image acquisition and database matching section has been removed here and the code that follows the sending of %the ascii value to matlab after matching here we send an 'N'
fopen(s); %open serial port again
pause(2);
fprintf('sending after recieving from ldr\r\n'); %prints in matlab
fprintf(s,'%s\r\n','N'); % 'N' to the arduino through USB
fprintf('sent'); %prints in matlab
fclose(s); %close port
According to the programs, the arduino keeps waiting for LDR analog reading to go below 100 and MATLAB keeps waiting for something to come from arduino. When less than 100, the arduino jumps outta the loop and sends a '3' serially to matlab. The matlab sees the incoming byte, jumps out of the loop and executes the code further. It then sends an 'N' as it is supposed to.
But arduino does not execute the code further after sending the '3'.
I can see the RX LED blink on arduino after the MATLAB sends 'N', but it doesnot light up for 5 secs as it is supposed to.....but keeps waiting for the analog pin to go below 100 again, only after which the rest of the code executes.....and when i swipe the LDR the second time here (I am not supposed to), it again sends a '3' to matlab because i see the TX pin blink...and rest of code executes.
the code executes fine when there is a 3-4 second time delay followed by the sending of '3' to MATLAb....without analogRead part.
void loop()
{
delay(3000);
Serial.print('3'); //send triggering '3' to MATLAB
.
.
.
.
}
this instead of the analogpin waiting for the drop below 100
void loop()
{
while(analogRead(analogPino)>100)
{
;
} // Keep looping till analog reading goes below 100
Serial.print('3'); //send 3 to trigger MATLAb out of the loop to continue the MATLAB code execution
.
.
.
}
The problem seems to be with the analogRead function.....i have been trying to solve the issue since one week.
Please help! Sorry for the essay!