Asked — Edited

Continues Read Of Ir Sensor

Im a little confused here. I'm simply trying to read the value coming from my IR sensor (one of the Sharp IR ones). The script I wrote should should be a simple loop and display the value, can continue to display the value until I tell it to stop.


goto(sensor)
  :sensor
  $IRSensor = GetADC(ADC4)
 # Sharp IR Sensor
print($IRSensor)
sleep(1000)
Return()

It seems to run only a couple of times then stops. That's my issue, why it displays the values twice then stops. What am I missing? confused


ARC Pro

Upgrade to ARC Pro

Harnessing the power of ARC Pro, your robot can be more than just a simple automated machine.

PRO
USA
#1  

script to perform a loop reading:


:sensor
$IRSensor = GetADC(ADC4)
print($IRSensor)
sleep(1000)
Goto(sensor)

PRO
USA
#2  

your code only prints two readings and the behavior is correct, let's investigate how return works:

Quote:

Return() Return from a Goto()

Quote:

  1. If you jump to a position of code with a Goto(), the Return statement will allow you to return back to that piece of code following the last Goto() statement.

Quote:

2) If you attempt to Return() with an empty stack, nothing will happen. The script will ignore the Return() statement.

let's follow the execution:

  1. goto sensor
  2. arrive at sensor label
  3. read & print & sleep 1 second
  4. call return => do a stack pop => founds a goto (quote point 1)
  5. arrive :sensor
  6. read & print & sleep 1 second
  7. call return => do a stack pop => stack is empty no goto (quote point 2) => do nothing

conclusion 2 readings

#3  

@ptp ok, I think I see it now.I was following a method I've used when programming a Basic Stamp. next test will be to add a ping sensor, then to add a specific value to each to cause some action to happen.

#4  

@ptp

This was my next test, which am happy to say works!


:sensors
$IRSensor = GetADC(ADC4) #Sharp IR sensor
$PingS =GetPing(D16,D16) #Parallax Ping sensor
print($PingS) 
print($IRSensor)
sleep(250)
if($IRSensor >=160) #a random value I picked
print("too close")
endif
if($PingS = 12) #should be about 12inches
print(" this is too close!")
endif
Goto(sensors)

Next, since I have 3 stationary ping sensors to add in, and want to run the IR on a servo to sweep.

#5  

So I've added a few more sensors, and am able to read the values. But now I wanted to add a couple of mechanical bumper switches. The swicthes work fine in a seperate script, but not quite sure how to add it into this script. Im sure it has something to do with it being in the proper spot within the code,but not quite getting it. Any ideas? Thank you


:sensors
$IRSensor = GetADC(ADC4) #Sharp IR sensor
$PingL =GetPing(D14,D15) #EZB Ping sensor
$PingF =GetPing(D16,D16) #Parallax Ping sensor
$PingR =GetPing(D18,D19) #EZB Ping sensor
$PingRr=GetPing(D17,D17) #Parallax Ping sensor
print($PingL)
print($PingF)
print($PingR)
print($PingRr) 
print($IRSensor)
sleep(750)
if($IRSensor >=160) #a random value I picked
print("too close")
#sleep(500)
endif
if($PingL <= 14) #should be about 14inches
print("Left corner is near!")
#sleep(500)
endif
if($PingF <= 12) #should be about 12inches
print(" this is too close!")
#sleep(500)
endif
if($PingR <= 16) #should be about 16inches
print("Right corner is near!")
#sleep(500)
endif
if($PingRr <= 18) #should be about 14inches
print("Backed up too close!")
#sleep(500)
endif
#goto(sensors)
:switches #this part still not working right---
if (ADC_Wait(ADC0,lower,50)) #Left Bumer Switch
print("Left Bumper")
endif 
if (ADC_Wait(ADC1,lower,50)) #Right Bumper Switch
print("Right Bumber")
endif 
goto (switches)
goto(sensors)

confused confused confused

PRO
USA
#6  

it's important to understand how ADC_Wait works and the impact in a loop script.


ADC_Wait(ADC0,lower,50)

is similar to:


:adc_wait_loop
Sleep(100) # ADC_Wait's 4 Th parameter if not specified the delay is 100 ms
$a = GetADC(ADC0)
if ($a >= 50)
    #value is not less than 50 so keep the loop
    Goto(adc_wait_loop)
endif

it's easy to understand each ADC_Wait blocks the script until the condition is met, the delay is used to define how frequent ARC pulls an ADC reading from the EZB.

The question is this what you want ?

if you don't want ADC_Wait to block the script you should change to :


$a = GetADC(ADC0)
if ($a<50)
    print("left bumper")
endif

#7  

@ptp Morning! yes that is correct, I don't want the ADC to Wait. I want to run and loop just like the sensors are doing. In most cares the bumpers will act as a secondary if any of the other sensors failed to stop the robots forward motion.

The ultimate goal will be to have the IR sensor on a servo to sweep, but will only sweep after one of the other sensors have been triggered. Then the robot performs another task,etc. Thank you again. I will try some more experiments tonight,love to see what happens! :D

#8  

So I've made alt of progress with adding all the sensors in my test routine here. My newest attempt was the IR sensor as a radar (is servo on SSC32- V0). I figured out how to use the ControlCommand("Sharp IR Radar", PauseOn) and pauseoff commands. What i really would like to figure out now is if there is a way to make the Sharp IR Radar command only be activated when called upon to do so, then made to stop when told to do so. I didn't see anything usuefull within the "cheat sheet". Is there another command to use I may have missed? or a script to use?


:sensors
$IRSensor = GetADC(ADC4) #Sharp IR sensor
$PingL =GetPing(D14,D15) #EZB Ping sensor
$PingF =GetPing(D16,D16) #Parallax Ping sensor
$PingR =GetPing(D18,D19) #EZB Ping sensor
$PingRr=GetPing(D17,D17) #Parallax Ping sensor
print($PingL)
print($PingF)
print($PingR)
print($PingRr) 
print($IRSensor)
sleep(750)
#starts the IR radar sweeping
ControlCommand("Sharp IR Radar", PauseOn)
if($IRSensor >=160) #a random value I picked.
print("too close")
ControlCommand("Sharp IR Radar", PauseOn)
#pauses the sweep when thevalue is found, then resumes when clear.
#sleep(500)
endif
if($PingL <= 14) #should be about 14inches.
print("Left corner is near!")
#sleep(500)
endif
if($PingF <= 12) #should be about 12inches.
print(" this is too close!")
#sleep(500)
endif
if($PingR <= 16) #should be about 16inches.
print("Right corner is near!")
#sleep(500)
endif
if($PingRr <= 18) #should be about 14inches
print("Backed up too close!")
#sleep(500)
endif
$a = GetADC(ADC0) #left bumper switch
if ($a<50)
print("left bumper")
endif
$a = GetADC(ADC1) #right bumper switch
if ($a<50)
print("right bumper")
endif
goto(sensors)

eek

Portugal
#9  

Hi, RoboHappy. Why are you using the analog ports for the bumpers. It is a simple 1 or 0 contact, use the digital ports and save the analog for future use. Regards.

#10  

@proteusy That is true, I could use the digital ports. At least now I could since I've ported all the servos over to the SSC32 and in fact freed up the digital ports. At the time I did not have any more ports left except the adc, so wanted to learn best way to use with the switches since I've seen it done before on the forums earlier. I still may do the digital route again, but for now I'm sticking with the adc, and the best, for me, at least it is getting some more programming experience out of it the best part with the help of this great community here. :)