Ireland
Asked — Edited

Ez-B Script

trying to read four digital inputs need a little help please eg there are five possible options

all are "1" or each is at "0" in turn. (d1,d2,d3,d4)

say d1 "0" moves servo d7 to position 50,once it changes to "1" move servo back to position 40

  d2 "0" moves servo d7 to position 30, once it changes to "1" move servo to position 40

d3 "0" moves servo d8 to position 50 , once it changes to "1" move servo back to position 40

d4 "0" moves servo d8 to position 30 , once it changes to "1" move servo back to position 40

finally if all inputs are "1" both d7 and d8 are at position 40

hope I have explained my request clearly


ARC Pro

Upgrade to ARC Pro

Experience the transformation – subscribe to Synthiam ARC Pro and watch your robot evolve into a marvel of innovation and intelligence.

United Kingdom
#1  

Use the GetDigital() command to read the digital port, i.e. GetDigital(D1) Use Ifs and ElseIfs to check the status and perform the action depending on the result. Use an If with the And operator to check for all 4. I would do it with an If statement inside another If statement...

Each of the single port checks would be like this;


If(GetDigital(D1)=0)
  Servo(D7,50)
ElseIf(GetDigital(D1)=1)
  Servo(D7,40)
EndIf

Do the same for the other 3.

The "tricky" part comes by needing to know about all 4 being 1. Although not too tricky...


# If all 4 are 1
If(GetDigital(D1)=1 and GetDigital(D2)=1 and GetDigital(D3)=1 and GetDigital(D4)=1)
  Servo(D7,40)
  Servo(D8,40)
Else
# Otherwise check the single ones.
  If(GetDigital(D1)=0)
    Servo(D7,50)
  ElseIf(GetDigital(D1)=1)
    Servo(D7,40)
  EndIf
# Code for D2, D3 and D4 here, just change the code for D1 to suit.
EndIf

Something like that.

And loop it with the :loop label at the start and a Goto(loop) at the end

United Kingdom
#2  

I guess the ElseIf(GetDigital(D1)=1) could be just an Else to save typing, it's going to be one or the other so if it isn't 0 then you don't need it to check if it's a 1.

So;


:loop
# If all 4 are 1
If(GetDigital(D1)=1 and GetDigital(D2)=1 and GetDigital(D3)=1 and GetDigital(D4)=1)
  Servo(D7,40)
  Servo(D8,40)
Else
# Otherwise check the single ones.
  If(GetDigital(D1)=0)
    Servo(D7,50)
  Else
    Servo(D7,40)
  EndIf
# Code for D2, D3 and D4 here, just change the code for D1 to suit.
EndIf
Goto(loop)

Ireland
#3  

Thanks for your help Rich

Code: :start If(GetDigital(D1)=0) Servo(D7,50) ElseIf(GetDigital(D1)=1) Servo(D7,40) EndIf If(GetDigital(D2)=0) Servo(D7,30) ElseIf(GetDigital(D2)=1) Servo(D7,40) EndIf If(GetDigital(D3)=0) Servo(D8,50) ElseIf(GetDigital(D3)=1) Servo(D8,40) EndIf If(GetDigital(D4)=0) Servo(D8,30) ElseIf(GetDigital(D4)=1) Servo(D8,40) EndIf goto(start)

I am attempting to read the four digital inputs ,two move servo d7 ,two move servo d8

So as you can see I once it finds a low it moves a particular servo, ( and continues to read all ports) so I need to stop script until that input state changes Thanks:Pat

United Kingdom
#4  

I only have a few minutes so this will be short.

Take a look at the Digital_Wait (digitalPort, on/off/true/false) command, but it will pause until the specified port changes, I don't know if it can use multiple ports in the one statement (I've never tried).

You could use variables and check for a change, such as;


# Populate variables to avoid possible errors (may not be needed)
$D1Status = GetDigital(D1)
$D2Status = GetDigital(D2)
...
# Start the script
:loop
# Set previous statuses
$D1LastStatus = $D1Status
$D2LastStatus = $D2Status
...
# Get current statuses
$D1Status = GetDigital(D1)
$D2Status = GetDigital(D2)
...
# Check for change
If($D1Status != $D1LastStatus)
# Run code if changed
...
EndIf
Goto(loop)

The ... parts need the rest of the code, I'm too lazy to write it:)

That will typically check for a change in status on the digital port and if it has changed it'll run the code. An IF with ORs can do that in one IF statement;


If($D1Status != $D1LastStatus or $D2Status != $D2LastStatus or ...)
...
EndIf

Ireland
#5  

Appreciate your time Rich Will try your suggestion later Pat