Go Down

Topic: analogRead making the program execute the same lines of code again. (Read 2920 times) previous topic - next topic

sachin-nayak

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.


Code: [Select]
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

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 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'.  :smiley-sad-blue:

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!

SurferTim

This may be part of it.
Code: [Select]
     Serial.print('3'); //send 3 to trigger MATLAb out of the loop to continue the MATLAB code execution


It appears your MATLAB code is waiting for a CR/LF as a terminator. Maybe this would be better.
Code: [Select]
     Serial.println("3"); //send 3 to trigger MATLAb out of the loop to continue the MATLAB code execution


sachin-nayak

#2
Apr 17, 2012, 01:57 pm Last Edit: Apr 17, 2012, 02:00 pm by sachin-nayak Reason: 1
Thanks for looking into it! :)

I tried it but the terminator does not seem to be the problem....the arduino code is running out of sequence when the analogRead section is put in...


Code: [Select]
unsigned char Rx_PIN = 0;
int nvm;
static int i=0;
int analogPino=0;

void setup()
{
 

 pinMode(11,OUTPUT);  
pinMode(12,OUTPUT);  
 Serial.begin(9600);       //start serial port at 9600 bps baud
}


void loop()
{
 
if(i==0)
{
digitalWrite(11,HIGH);
delay(500);
digitalWrite(11,LOW);                   //blink 0.5 sec if i is 0

}
else if(i==1)
{
digitalWrite(11,HIGH);
delay(2500);
digitalWrite(11,LOW);                    //blink 2.5 secs if i equals 1

}
 
 
 if(i==1)
 i=0;
 
 else if(i==0)
 i=1;                                   //toggling i to see if the programme is going back again but the toggle is not seen when it executes again....as if the initial value of i has been saved
                                        // the toggle is seen only after i swipe the LDR again which i am not supposed to
   
   while(analogRead(analogPino)>100)
   {
     ;
   }
 
   
 
 Serial.print('3');
 
 
 
    while (Serial.available()==0)
   {
      ;
   }
     
     nvm=Serial.read();  
     if (nvm=='N')
     {
     digitalWrite(12,HIGH);
 delay(5000);
 
 digitalWrite(12,LOW);
 delay(5000);
     }
  else   if (nvm=='D')
     {
     digitalWrite(12,HIGH);
 delay(1000);
 
 digitalWrite(12,LOW);
 delay(5000);
     }
                             
}
 




Here i'm toggling ' i ' to see if the programme is going back again,.... but the toggle does not happen when it jumps back up (apparently) instead of going down sequentially....as if the initial value of i has been saved.
            the toggle is seen only after I swipe the LDR again which I am not supposed to...and rest of code executes only then :(
   

SurferTim

This won't help. Remove the "static".
Code: [Select]
static int i=0;


Are you watching the serial monitor output?

Code: [Select]
    Serial.print("Analog read start...");

    while(analogRead(analogPino)>100)
    {
      ;
    }

    Serial.println("finished");

sachin-nayak

Yea, this worked fine.....infact, i can even continuously moniter the analog pin readings.....the downward sequential code non-execution problem occurs as soon as it receives something, when the analogRead function has been used at the top of the code.

SurferTim

Is it the Serial.read() call that is not functioning correctly?
Or is it not exiting the "while(Serial.available() == 0)" loop?

I would try this, and watch the serial monitor to check what is received on the serial port.
Code: [Select]
      nvm=Serial.read(); 
      if (nvm == 'N')
      {
         digitalWrite(12,HIGH);
         delay(5000);
         digitalWrite(12,LOW);
         delay(5000);
      }
      else if (nvm == 'D')
      {
         digitalWrite(12,HIGH);
         delay(1000);
         digitalWrite(12,LOW);
         delay(5000);
      }
      else
      {
         Serial.print("Invalid char ");
         Serial.println(nvm,DEC);
      }                                     



sachin-nayak

Hey thanks SurferTim! As i figured out, the problem is not with analogRead.....the problem has something to do with the closing and opening of the port in the MATLAB code....the code in Arduino jumps back up as soon as the port in MATLAB is closed.....i put a 10 sec delay in arduino with delay(10000) and at about 5 seconds into waiting, i made the matlab close the port.....this made arduino skip the remaining 5 deconds wait and go back up again! Any ways to get around this problem?

P.S. I cannot keep the port open in matlab....I MUST close the port.....if i keep it open, everything works fine....but i have to close it in the middle atleast once before opening again later....

AWOL

Quote
I cannot keep the port open in matlab....I MUST close the port...

So, disable the auto reset on your Arduino.

sachin-nayak

Thank you so much! :) I connected a 10uF capacitor across the RESET and ground pins as said in the link below. It's not resetting now every time the port is closed and opened.

http://arduino.cc/playground/Main/DisablingAutoResetOnSerialConnection


Thanks again! :)

Go Up