problem running duel light sequence

im having a problem running 2 light sequence together the program starts one sequence then its a delay till the other starts but when ran the code starts the first sequence the when the second one starts the first one stops the second one runs then finishes the first sequence here in the section of the code im having problems with but will also add complete code

// start bracket tree
	if (brackettree == true)  
	{
		if (raceStart == true)
		{
			treeState = stage;
			treeState2 = stager;
			raceStart = false;
		}
	{
			if (treeState == stage)
		{
			digitalWrite(stagelPin, LOW);          //left stage light on 
			treeStart = millis();
			treeState = drop;
		}
		else if (treeState == drop & millis() - treeStart >= (1000 + Ldelay))
		{
			digitalWrite(yellow1LPin, LOW);
			treeStart = millis();
			treeState = drop2;
		}
		else if (treeState == drop2 & millis() - treeStart >= 500)
		{
			digitalWrite(yellow1LPin, HIGH);
			digitalWrite(yellow2LPin, LOW);
			treeStart = millis();
			treeState = drop3;
		}
		else if (treeState == drop3 & millis() - treeStart >= 500)
		{
			digitalWrite(yellow2LPin, HIGH);
			digitalWrite(yellow3LPin, LOW);
			if (millis() - treeStart >= 600)
			{
				treeStart = millis();
				if (btime == false)
				{
					timerStartState = true;
					btime = true;
				}
				treeState = go;
			}
		}
		else if (treeState == go & millis() - treeStart >= 400)
		{
			digitalWrite(yellow3LPin, HIGH);
			if ((stageLeft == LOW) & (leftStart == HIGH))  //car must be in stage sensor till green if moves before red ligh will come on insted
			{
				digitalWrite(greenlPin, LOW);        // green light on
				leftJumpStart = false;
				treeState = end;
			}
			else
			{
				digitalWrite(redlPin, LOW);          //red light on car moved befor green light
				leftJumpStart = true;
				treeState = end;
			}

		}
	}
		{
			if (treeState2 == stage)
			{
				digitalWrite(stagerPin, LOW);		   //right stage light on by ir
				treeStart2 = millis();
				treeState2 = dropr;
			}
			else if (treeState2 == dropr & millis() - treeStart2 >= (1000 + Rdelay))
			{
				digitalWrite(yellow1RPin, LOW);
				treeStart2 = millis();
				treeState2 = drop2r;
			}
			else if (treeState2 == drop2r & millis() - treeStart2 >= 500)
			{
				digitalWrite(yellow1RPin, HIGH);
				digitalWrite(yellow2RPin, LOW);
				treeStart2 = millis();
				treeState2 = drop3r;
			}
			else if (treeState2 == drop3r & millis() - treeStart2 >= 500)
			{
				digitalWrite(yellow2RPin, HIGH);
				digitalWrite(yellow3RPin, LOW);
				if (millis() - treeStart >= 600)
				{
					treeStart2 = millis();
					if (btime == false)
					{
					timerStartState = true;
					btime = true;
					}
					treeState2 = gor;
				}
			}
			else if (treeState2 == gor & millis() - treeStart2 >= 400)
			{
				digitalWrite(yellow3RPin, HIGH);
				if ((stageRight == LOW) & (rightStart == HIGH))  //car must be in stage sensor till green if moves before red ligh will come on insted
				{
					digitalWrite(greenrPin, LOW);        // green light on
					rightJumpStart = false;
					treeState2 = endr;
				}
				else
				{
					digitalWrite(redrPin, LOW);         //red light on car moved befor green light
					rightJumpStart = true;
					treeState2 = endr;
				}
				
				
			}
			

		}
		if ((treeState == end) && (treeState2 == endr))
		{
			btime = false;
			brackettree = false;
		}

dragtree1.63.ino (23.1 KB)

Have a look into your brackets. Use the Tools -> Auto Format function to align them automatically.

Every opening bracket has a specific closing bracket. You have brackets in your code that are not necessary but could be a sign that your opening and closing brackets do not belong together in a way that you think they are.

I use vb to do the code it doesn’t look like that when I open it only when I paste it here but the code will run fine if I lengthen the delay of the two sequences starting it’s only messing up when I try to start the two sequences close together

I have been looking at your code for a while and found it a bit hard to read and understand. Could be me not being smart enough. :)

I haven’t found your issue yet but noticed a couple of things.

You are using the bitwise AND (single &) in cases I think you should use the Boolean AND (&&).

You created two enums definitions which are basically identical. You could just create two instances using the same enum. You even compare treeState2 to the wrong enum in if ( treeState2 == stage ). It’s just accidentally the correct value.

To make the code more readable I would give the enum elements a common name structure and use all capital with underscore (common use for constant values). e.g.

enum treeStateType { STATE_STAGE, STATE_DROP, ... STATE_GO, STATE_END};

That way when you read the code its clear "go" is no variable just a constant number.

I would also use the same naming convention for all the pin names. e.g. LEFT_RELAY_PIN

You could use a couple off simpler functions e.g. resetTreeLights for the code where you write all the pins to HIGH or when you print the results.

You are using delay(). Delay should only be use very rarely with very short delays. It’s a waste of processor cycles and often causes other stuff to not work correctly.

Some of your names are misleading or could describe better what they are for. e.g. - boolean leftReactionTimer = false; This is not a timer. Depending of what it is used for you could call it xxxTimerFlag or xxxTimerEnable - const byte leftStartPin = 10; Is there a button or switch on that pin? BUTTON_START_LEFT_PIN

In other cases, the names could be more consistent e.g. - you use startTime and stopLeft. Maybe LeftStartTime and LeftStopTime could be an alternative showing the two belong together. - AutoWin, Nprotree, .. all variables should start with a small letter

You create a global variable state that is only used in Timing. If you create a static variable inside Timing it will have the same effect, but it is clear state can only be used inside Timing. e.g. static byte state = 0;

A short description what is connected to Serial and Serial1 would be nice.

In PCInput you check if Serial is available, but you do not read the buffer when the race has not started. This could lead to characters being stored into the buffer before the raceStart and then you read them when race starts.

You only expect val to be a single character, but you use a String type. Strings seem to cause issues on some Arduinos because of memory limitations. You could simply use a char instead. Also, there is no need for val to be a global variable. Just use char c = ""; at the beginning of PcInput and use c = Serial.read();

In case Serial and Serial1 receives something in your current code the letter send trough Serial will be lost.

Let me know if this was useful and hopefully, we can find the other issue as well.

Serial 1 is Bluetooth link to pc and serial is there just for back up as far as the button pin I’ll have to look this has been a on going project for me for about a year adding more to it there was a start button before I done a vb program to run it so if that’s still there I’ll remove it i may have just over looked it and yes I have had problems in the past with the pc input using words sent from the pc some times did not work so I changed to a single letter. I set 2 different enums because I thought they would interrupt each other and I must have over looked using the wrong one on tree2. This is my first pretty much my only big project I have done so I’m still learning eventually I will also need to send a time to be put in the Ldelay and Rdelay from the pc but I’m not sure how yet but thanks for helping me

What Arduino board are you using and do you have a schematic of your system? It looks like it some kind of light system for car races.

It is a drag tree and Timming system every thing works and has worked for about a year except the bracket part I’m working on now everything else the lights was on at the same time but now that I’m trying to control each lanes lights on different time I can’t get that section of the code to work properly. No I have never really sat down and done a wiring diagram of the system it’s using a mega 2560, 2 relay boards with 8 relays each to control the 14 lights 7 per lane and 10 npn reto reflections sensors 5 per side to control the timing

thanks for your help i found the problem i fixed the enum i had wrong and then found i had used the same millis reading for both lanes in one spot so that was my problem

i also need to change my serial input now to something like this 0,.400,B i need the first number to be stored in Ldelay and the other to be Rdelay and then the letter to tell witch light sequence to use whats the easiest way to do this right now i just send the letter only

Good news. One recommendation would be to turn on C compiler warnings.

File -> Preferences -> Settings Tab -> Compiler Warnings -> Set to ALL

I compiled your code and found the following line (around line 900, function timing, cases 2). This is a typical C bug. 99.99% of cases you need ==

if ( LeftReactionTime = RightReactionTime )

The compiler will also warn you about the bitwise AND as mentioned before. You should use Boolean (&&). You can do a search and replace.

Regarding the serial. Have a look at the TinyGPS library. You can install it from the Library Manager. It is a pure parsing library no code specific to GPS chips. GPS modules have a standard way of sending data to microcontrollers using human readable ASCII serial strings. The standard is called NMEA 0183. It’s simple

  • Message starts with $ sign.
  • A few letters identify the format
  • then the numbers and some letters follow with the data separated by comma

Your message could look like

$DRAG,0,400,B

You can modify the TinyGPS library with your own messages and most of the functionality needs only some modification. With the code you have written so far, I think you should be able to do this. The Tiny GPS code is shorter than your program. :)

https://en.wikipedia.org/wiki/NMEA_0183

that sound like it would work but im not sure how to set that up and modify it.

First go to the library manager and install the TinyGPS library. Just type the name and click install. This will copy the files to your sketchbook directory.

See File -> Preferences -> Sketchbook location

There is a folder called libraries. Look inside and find the TinyGPS folder.

Now create a new folder call it whatever you like TinyDragTree, TinyParser ... and a subfolder called src.

Copy the two files TinyPGS.cpp and TinyGPS.h and rename them.

Now you have your own library and need to work through the code to adapt it to your protocol.

Use one of the examples and verify that you can compile the code and do some tests while changing things.

Ok so I don't have to actually go in and change anything in the library just the file names

No, you must change the library. But first make a copy and then change the function that need changing. There is lots of stuff that you can uses as is. I will help you on the way.

First have a look at the two files mentioned and see whether you understand most of it. And I do not mean the details. Just roughly what is going on. There are some definitions, enums, public and private keywords, inline functions, see that most functions are very short, in the cpp file the function declaration looks slightly different than normal, ...

Do you think you can work through that with some help?

Next think of a name that we can use instead of TinyGPS.

Then write down a few parameters that you would like to send and give them good names.

Then think of a few message formats that you would like to send and write them down.

yes i looked at the two files and i kinda understand whats doing on. I need to send to send to send a left lane time and a right lane time then a tree function only one tree function requires the two time so i fiqur to send the others with the two times as 0 like $drag,0,0,P and the on that will require the time will be $drag,.300,0,B or $drag,0,.300,B one lane will always be a 0 and a time in the other. So u think with some help i can work though the changes with some help.

OK, so the message format will be something like the following. Please correct me if I am wrong or missunderstood something.

$DRAG,leftDelay,rightDelay,command

leftDelay, rightDelay will be an integer number command will be a single letter aka the "B" or "P" and so on

So we start with one message format. If you want to send different parameters we can create another format later.

What do you want to call the library?

I changed the file names to DRAGtree and yes all command will be a single letter should we do one message format or two since i only need the times on the B command every other command i only need the letter.