Can't implement the function

I've been developing a function to simulate a vehicle atuated narrow passage traffic lights and need it to stand on green until recieve an input, then the program continues the cycle until phase 3, when it is on phase 3 and recieves input it increases 3 seconds to that timer with a limit of 20sec max.

Here is the code.

void mod8(void){
policia();
int fase = 0;
int proc = 0;
switch(fase){
case 0:
verde_1_on();
vermelho_2_on();
if(!digitalRead(E0)){
delay(3000);
fase++;
}
break;
case 1:
amarelo_1_on();
vermelho_2_on();
delay(3000);
fase++;
break;
case 2:
vermelho_1_on();
vermelho_2_on();
delay(30000);
fase++;
break;
case 3:
vermelho_1_on();
verde_2_on();
if(!digitalRead(E1) && fase==3){
proc += 3000;
}
delay(3000);
fase++;
break;
case 4:
vermelho_1_on();
amarelo_1_on();
delay(3000);
fase++;
break;
case 5:
vermelho_1_on();
vermelho_2_on();
delay(30000);
fase = 0;
break;

}
}

Can anyone help me?

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

Please post your whole sketch and not just part of it

What does the code do that does not meet your requirements ?

My project has many files with a lot of code, cant upload it all.
The code doesnt stop on phase 0 and it just runs the whole cicle without pauses specified on my requirements.

Please try and make a state diagram that clearly describes each state, and the condition(s) that make the system go from one state to the next. This may make the text above easier to understand for others, but it will also help you to be clear and consistent in designing your state logic.

Well, you could. or you could write a small sketch that shows the problem

In your project what is keeping the input pins in a known state at all times ? Do you use pullup or pulldown resistors, for instance ?

Which Arduino board are you using, which pin does the E0 variable relate to and what is its pinMode() ?

I note a number of long delay()s in your sketch. Do you really want the code to do nothing for 30 seconds at some points ?

The board I'm using is a custom development made for my company, but uses Arduino for programming.

However it is made and who makes it please answer this question

PullUp registor

Without further information it is going to be difficult to provide help. For instance, where is the fase variable declared, what data type is it, is its value changed elsewhere in the code, does the code use arrays and if so are their bounds being exceeded thus overwriting the value of another variable, etc, etc

    case 3:
        vermelho_1_on ();
        verde_2_on ();
        if (! digitalRead (E1) && fase==3)  {
            proc += T0;
        }
        delay (T0);
        fase++;
        Serial.println (fase);
        break;

it doesn't wait for any input in fase 3. it just delays and then advance to the next fase. it only increments proc depending on an input

Hi!

I did this example to help someone here some time ago.

You can play with this code to make what do you want. Otherwise you can contact me for a freelance.

I'm from Brazil.

Best regards.

While I understand the sentiment the OP has made it clear that his company has created the board. It is up to individual helpers whether they are happy to offer advice in these circumstances, if not then they are under no obligation to help. I don't see any reason to attack the OP for asking for help for a company project, just don't help if you don't want to.

1 Like

For help, post the minimum, complete code that can be compiled, run, and demonstrates the problem.

2 Likes

isn't that in post #1?

No.

it recognizes the 2nd input and waits the 20 secs?

it doesn't for me and i believe that's the problem

i can fill in the missing functions and exercise his code. i believe he posed enough to see the problem. i'd prefer that rather than belittle the OP

1 Like

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