Jump to content

As of July 17, 2015, the LabJack forums here at forums.labjack.com are shut down. New registrations, topics, and replies are disabled. All forums are in a read-only state for archive purposes.

Please visit our current forums at labjack.com/forums to view and make new posts. To post on the current forums, use your labjack.com login account. Your old LabJack forums login credentials have been retired. There are no longer separate logins for labjack.com and LabJack forums.


Analogue input while loop

analogue input loop while

  • Please log in to reply
3 replies to this topic

#1 safceddy

  • Members
  • 3 posts

Posted 28 April 2014 - 08:10 AM

I'm using analogue inputs to read a voltage and would like to make while a loop which continues until 5 Volts has been exceeded.
Once this has been exceeded the loop can then stop scanning the input voltage and perform an if statement.
here's what I've got so far from the easy functions:
this allows regular readings of the AI0 voltage input; however instead of using n I need to be using the actual voltage that is being checked to close the loop. 
%Reading from analog input AI0, single-ended
while n < 100
n = n+1;    
channel = 0; %AI0
gain = 0;  %G=1, +-20 V (gain 0 = 1)
overVoltage = 0;  %Returned overvoltage. Will get a value >0 if overvoltage is detected.
voltage = 0.0;  %Returned voltage
[errorCode, idnum, overVoltage, voltage] = lj.LabJack.EAnalogIn(idnum, demo, channel, gain, overVoltage, voltage);
if(errorCode ~= 0)
    lj.LabJack.GetErrorString(errorCode, errorString);
    disp(['EAnalogIn error ' num2str(errorCode) ' : ' char(System.String(errorString))])
pause (0.2)
disp(['Single-ended AI' num2str(channel) ' voltage = ' num2str(voltage) ' V, overVoltage = ' num2str(overVoltage)])


#2 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 28 April 2014 - 12:47 PM

It sounds like you need a while loop like this:

voltage = 0
while voltage <= 5
    %Get the latest voltage reading

%voltage > 5 V code here

Note that your EAnalogIn call is performing a single ended reading which is +-10 V range only. The gain setting is only available for differential readings (channels 8-11, where channel 8 is AI0-AI1, channel 9 is AI2-AI3, etc.). Here is the EAnalogIn function and AI0-AI7 hardware documentation which explains this:




#3 safceddy

  • Members
  • 3 posts

Posted 29 April 2014 - 02:53 AM

Thanks that works. how would I be able to check 2 voltages at once the same way, being able to close the loop when either have reached 5 Volts?

#4 LabJack Support

LabJack Support
  • Admin
  • 8677 posts

Posted 29 April 2014 - 12:47 PM

It would look like this:

voltage1 = 0
voltage2 = 0
while ( voltage1 <= 5 && voltage2 <= 5 )
    %Get the latest voltage readings

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users