Help with my robot code

Hi everyone i'm new here so be gentle
i'm trying to write a code for my washing robot
i have
arduino
6ch relays
motor
electric valve
sensor right and left
buttons right and left

i have a code that someone wrote for me with other platform but also code c
he used there with different deffinitions - for example DI mean digital input.

i will copy to here example of the code
please if someone can change it for me to arduino language it will be fantastic.
thank you

top:

LimtP = getvalue(_DI, 1)
LimtN = getvalue(_DI, 2)
StartP = getvalue(_DI, 3)
StartN = getvalue(_DI, 4)

if StartP or StartN then
timer = timer - 1
if timer <= 0 then
timer = 0
end if
else
timer = starttimer
end if

if state = 0 then'wait for command from button
dir = 0
flag = 0
if StartP = 1 and LastStartP = 0 and LimtP = 1 then
dir = 1
state = 10
elseif StartN = 1 and LastStartN = 0 and LimtN = 1 then
dir = -1
state = 10
end if

elseif state = 10 then' got to limit
if timer = 0 then ' check if press is longer than starttimer
flag = 1
end if

if (StartP = 1 and LastStartP = 0) or (StartN = 1 and LastStartN = 0) or (flag = 1 and (LimtP = 0 or LimtN = 0)) then
	state = 100
	stoptimer = stopDelay
elseif (flag = 0 and (LimtP = 0 or LimtN = 0)) then 
	state = 20
	stoptimer = stopDelay
	if dir = 1 then
		dir = -1	
	elseif  dir = -1 then
		dir = 1
	end if		
end if 

elseif state = 20 then ' goto to the 2nd limit
stoptimer = stoptimer - 1
if stoptimer <= 0 then
stoptimer = 0
end if
if (StartP = 1 and LastStartP = 0) or (StartN = 1 and LastStartN = 0) or ((LimtP = 0 or LimtN = 0) and stoptimer = 0) then
state = 100
stoptimer = stopDelay
end if
elseif state = 100 then'stopping
stoptimer = stoptimer - 1
if stoptimer <= 0 then
stoptimer = 0
dir = 0
state = 0
end if
end if

Doesn't look like "C" to me.
Perfect time for you to learn by rewriting the code, but keeping the logic.
Do you have a hardware setup to test as you go?
Paul

1 Like

yeah
im testing the arduino by voltmeter
the robot action should be that way:
button right - go right- if one of the sensor reserve signal stop and go back
when sensor reserve signal again - stop and "LOW" all outputs

Don't look like C to me either, might be psudo did the original robot ever work or is it just at the concept stage. Might be Pascal, Might be one of the many version of Basic but my money is on psudo code. What was the other platform BTW?

There is not enough information to translate this pseudocode (or whatever it is) to C++ for the Arduino.

  1. There is no description of how the robot is supposed to behave given the inputs. This is critical.
  2. There is no information given to correlate the hardware above to the inputs and I see no outputs to correlate to the relays, motor, or electric valve.
  3. How is the valve interfaced?
  4. What kind of motor do you have?
  5. What sense (LOW, HIGH versus open/close) are the relays?
  6. What sense are the buttons (LOW, HIGH versus Pressed/Not pressed)?

A schematic and the Arduino board would be very helpful too.

int timer;
int starttimer, stoptimer, stopDelay;
int state = 0;
int dir = 0;
boolean flag = false;

int LastStartP;
int LastStartN;

void setup() {}

void loop()
{
  int LimtP = digitalRead(1);
  int LimtN = digitalRead(2);
  int StartP = digitalRead(3);
  int StartN = digitalRead(4);

  if (StartP or StartN)
  {
    timer--;
    if (timer <= 0)
    {
      timer = 0;
    }
    else
      timer = starttimer;
  }

  if (state == 0)
  {
    // wait for command from button
    dir = 0;
    flag = 0;
    if (StartP == 1 and LastStartP == 0 and LimtP == 1)
    {
      dir = 1;
      state = 10;
    }
    else if (StartN == 1 and LastStartN == 0 and LimtN == 1)
    {
      dir = -1;
      state = 10;
    }
  }

  else if (state == 10)  //  got to limit
  {
    if (timer == 0)
    {
      //  check if (press is longer than starttimer
      flag = 1;
    }

    if ((StartP == 1 and LastStartP == 0) or (StartN == 1 and LastStartN == 0) or (flag == 1 and (LimtP == 0 or LimtN == 0)))
    {
      state = 100;
      stoptimer = stopDelay;
    }
    else if ((flag == 0 and (LimtP == 0 or LimtN == 0)))
    {
      state = 20;
      stoptimer = stopDelay;
      dir = -dir;
    }
  }


  else if (state == 20)   //  goto to the 2nd limit
  {
    stoptimer--;
    if (stoptimer <= 0)
    {
      stoptimer = 0;
    }
    if ((StartP == 1 and LastStartP == 0) or (StartN == 1 and LastStartN == 0) or ((LimtP == 0 or LimtN == 0) and stoptimer == 0))
    {
      state = 100;
      stoptimer = stopDelay;
    }
  }

  else if (state == 100)
  {
    // stopping
    stoptimer--;
    if (stoptimer <= 0)
    {
      stoptimer = 0;
      dir = 0;
      state = 0;
    }
  }
}

Thank you john, i will reply when i'll check the code

Thanks all for replying
The robot have 6 ch relays
Motor type is not matter now
Im using 24v battery to run the robot.
The arduino job is only to open the right relay on the right time.
So 4 relays is for motor
Two relays for positive, negative
And two relays for reverse (negative, positive)
For that i will use 2 digital output from the arduino.
Once it will send 5v signal it will open the relays.

So for example
If i pressed momentary button for half a second and it send pulse of 5v to digital input
Then
Start the motor forward - digital input of forward = HIGH.
if pressed the button again it will shut off all outputs.

If some one can do that i will provide more if

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.