analogRead making the program execute the same lines of code again.


hello, need help. got arduino uno last month. project control tv gesture recognition using matlab , arduino connected through usb. matlab has task of image acquisition , database matching. after obtaining match, matlab sends corresponding ascii value arduino , arduino generates corresponding ir signal through ir led philips rc-5 protocol.

all worked fine till , thought of adding additional circuitry in arduino. section has ldr circuit connected analog pin 0. it's function detect changes in voltage values of ldr , send triggering ascii value matlab through usb start programme in matlab. (matlab looping , waiting value come arduino).

here problem started.


code: [select]
int nvm;
int analogpino=0;

void setup()
{
   

  pinmode(12,output);     
  serial.begin(9600);       //start serial port @ 9600 bps baud
}


void loop()
{


     while(analogread(analogpino)>100)
     {
       ;
     }                                        // keep looping till analog reading goes below 100


      serial.print('3');                      //send 3 trigger matlab out of loop continue matlab code execution
     
     
     
      while(serial.available() == 0)
     {
       ;
     }                                        //keep looping till matlab sends byte after database matching
     
     
     
      nvm = serial.read();                      //read buffer after recieving
       
     
     
     
     
     // following part of code instead of ir led blinking rc-5 protocol
     
     
     
     
      if (nvm == 'n')                           
      {
      digitalwrite(12,high);
      delay(5000);                 // stay high 5 seconds if 'n'
 
      digitalwrite(12,low);
      delay(1000);
      }
 
   else   if (nvm == 'd')
      {
      digitalwrite(12,high);
      delay(1000);
 
      digitalwrite(12,low);       // stay high 1 second if 'd'
      delay(1000);
      }
                             
   }
 


matlab code

code: [select]
clc;
close all;
clear all;

%define serial port
        s = serial('com2');
        s.baudrate = 9600;               % define baud rate
        set(s, 'terminator', 'cr/lf');    % define terminator
       
       
        fopen(s);                     %open serial port
        pause(2);
     
fprintf('waiting\r\n');               %prints in matlab

while s.bytesavailable==0
   
end                                   %wait come arduino after ldr triggering

fclose(s);                            %close serial port

fprintf('got ldr\r\n');      %prints in matlab

% image acquisition , database matching section has been removed here , code follows sending of %the ascii value matlab after matching   here send 'n'


fopen(s);                            %open serial port again
pause(2);

fprintf('sending after recieving ldr\r\n');    %prints in matlab

fprintf(s,'%s\r\n','n');             % 'n' arduino through usb

fprintf('sent');                     %prints in matlab

fclose(s);                           %close port



according programs, arduino keeps waiting ldr analog reading go below 100 , matlab keeps waiting come arduino. when less 100, arduino jumps outta loop , sends '3' serially matlab. matlab sees incoming byte, jumps out of loop , executes code further. sends 'n' supposed to.

but arduino not execute code further after sending '3'.  :smiley-sad-blue:

i can see rx led blink on arduino after matlab sends 'n', but doesnot light 5 secs supposed to.....but keeps waiting analog pin go below 100 again, after rest of code executes.....and when swipe ldr second time here (i not supposed to), again sends '3' matlab because see tx pin blink...and rest of code executes.


the code executes fine when there 3-4 second time delay followed sending of '3' matlab....without analogread part.

void loop()
{
delay(3000);

serial.print('3');            //send triggering '3' matlab
.
.
.
.
}
       this instead of analogpin waiting drop below 100

void loop()
{


     while(analogread(analogpino)>100)
     {
       ;
     }                                        // keep looping till analog reading goes below 100


      serial.print('3');                      //send 3 trigger matlab out of loop continue matlab code execution
      .
      .
      .
}


the problem seems analogread function.....i have been trying solve issue since 1 week.
please help! sorry essay!

this may part of it.
code: [select]
     serial.print('3'); //send 3 trigger matlab out of loop continue matlab code execution


it appears matlab code waiting cr/lf terminator. maybe better.
code: [select]
     serial.println("3"); //send 3 trigger matlab out of loop continue matlab code execution



Arduino Forum > Using Arduino > Programming Questions > analogRead making the program execute the same lines of code again.


arduino

Comments

Popular posts from this blog

Thread: PKI Client 5.00 install (for eToken Pro)

ATmega2560-Arduino Pin Mapping

Crossfader Arduino Tutorial