Pc Lap Counter

Logiciel de comptage
 
HomeHome  FAQFAQ  SearchSearch  RegisterRegister  MemberlistMemberlist  UsergroupsUsergroups  Log in  

Share | 
 

 Sketch with 4 lanes- Pause Button & Power control for Analogue Track

View previous topic View next topic Go down 
AuthorMessage
steve112



Number of posts : 3
Registration date : 2013-10-26

PostSubject: Sketch with 4 lanes- Pause Button & Power control for Analogue Track   9/12/2014, 22:56

Here my sketch for Arduino & PCLC..

Good Race to All  Very Happy

Steve112


// Pclapcounter & Arduino
// sketch for 4 lanes - 1 power rele - 1 PauseButton
// Autor: Steve112
//
// Lanes = InputPins 2-3-4-5€
// Pause Button = InputPin 8
// Porew control = OutputPin 6

int LANE1 = 2;
int LANE2 = 3;
int LANE3 = 4;
int LANE4 = 5;
int RELE = 12;
int LED = 13;
int PAUSA = 6;

int L1Flag = 0;
int L2Flag = 0;
int L3Flag = 0;
int L4Flag = 0;
int PAFlag = 0;
int ledState = LOW;
long Inibit_Lane=2400;
long Inibit_Button=300;

unsigned long L1;
unsigned long L2;
unsigned long L3;
unsigned long L4;
unsigned long P1;
unsigned long TLED=1000;
long previousMillis = 0;



int val=0;
int L1State = 0;
int L2State = 0;
int L3State = 0;
int L4State = 0;
int Pa=0;
// setup routine :
void setup() {
// serial communication at 9600 bits :
Serial.begin(9600);
// here we set input/output Pins :
pinMode(LANE1, INPUT);
pinMode(LANE2, INPUT);
pinMode(LANE3, INPUT);
pinMode(LANE4, INPUT);
pinMode(PAUSA, INPUT);//pause button
pinMode(LED, OUTPUT);
pinMode(RELE, OUTPUT);//power
}


void loop() {

L1State = digitalRead(LANE1);
L2State = digitalRead(LANE2);
L3State = digitalRead(LANE3);
L4State = digitalRead(LANE4);
Pa = digitalRead(PAUSA);


unsigned long currentMillis = millis();


if ( Serial.available() )
{
String b;
b = Serial.readStringUntil('[');
{
String s;
//power control
s = Serial.readStringUntil(']');
if ( s == "PW000" ) {

digitalWrite(RELE, LOW);
val = 0;
}
if ( s == "PW001" ) {
digitalWrite(RELE, HIGH);
val = 1;
}
if ( s == "SL071" ) {
TLED=800;
}
if ( s == "SL051" ) {
TLED=500;
}
if ( s == "SL041" ) {
TLED=400;
}
if ( s == "SL031" ) {
TLED=300;
}
if ( s == "SL021" ) {
TLED=200;
}
if ( s == "SL011" ) {
TLED=70;
}
if ( s == "SL061" ) {
TLED=1;
}
}
}




if(currentMillis - previousMillis > TLED) {
// save the last time you blinked the LED
previousMillis = currentMillis;

// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;

if (TLED == 1){
ledState = HIGH; }
// set the LED with the ledState of the variable:
digitalWrite(LED, ledState);
}




if (val == 1) {
// Car cross the Start/Finish line




if ((L1State == 1) && ((currentMillis-L1)>Inibit_Lane) && (L1Flag == 0) ){
Serial.println("[SF01]");
L1=currentMillis;
L1Flag = 1;
};
if ((L2State == 1)&& ((currentMillis-L2)>Inibit_Lane)&& (L2Flag == 0)){
Serial.println("[SF02]");
L2=currentMillis;
L2Flag = 1;
};
if ((L3State == 1)&& ((currentMillis-L3)>Inibit_Lane)&& (L3Flag == 0)){
Serial.println("[SF03]");
L3=currentMillis;
L3Flag = 1;
};
if ((L4State == 1)&& ((currentMillis-L4)>Inibit_Lane)&& (L4Flag == 0)){
Serial.println("[SF04]");
L4=currentMillis;
L4Flag = 1;
};



if ((L1State == 0) && ((currentMillis-L1)>Inibit_Lane) ){
L1Flag = 0;
};
if ((L2State == 0) && ((currentMillis-L2)>Inibit_Lane) ){
L2Flag = 0;
};
if ((L3State == 0) && ((currentMillis-L3)>Inibit_Lane) ){
L3Flag = 0;
};
if ((L4State == 0) && ((currentMillis-L4)>Inibit_Lane) ){
L4Flag = 0;
};

}

//pause/resume command


if ((Pa == 0) && (PAFlag == 2)){
P1=currentMillis;
PAFlag = 0;
}
if ((Pa == 1)&& (PAFlag == 0)){
PAFlag = 1;
P1=currentMillis;
}
if ((Pa == 1) && (PAFlag == 1)&& ((currentMillis-P1)>Inibit_Button)){
PAFlag = 2;
Serial.println("[BT04]");
}

if ((Pa == 0)&& ((currentMillis-P1) PAFlag = 0;
}




}


Last edited by steve112 on 20/9/2017, 13:09; edited 1 time in total
Back to top Go down
View user profile
gus797



Number of posts : 6
Registration date : 2017-07-05

PostSubject: Re: Sketch with 4 lanes- Pause Button & Power control for Analogue Track   19/9/2017, 00:20

Hi,

I have tried to use this sketch without success for a long time, although having now some experience with arduino sketches.

After many investigations, I have eventually found what seems to be a bug in the above sketch.

In the second part of the sketch, where the flag is put back to "0", the test include L1flag=1, which is false when part of a testing condition. The correct test is L1flag==1.

When correcting this bug, the sketch works fine.

I have found also on arduino.cc a remark about mixing "long" variables with millis() function, which by the way is "unsigned long", and that could bring unpredictable errors....


And even more here :

 if ((L1State == 0) &&  (L1Flag = 1) && ((millis()-L1)>Inibit)  ){
  L1Flag = 0;
      L1=millis();
};
    if ((L2State == 0) &&  (L1Flag = 2) && ((millis()-L2)>Inibit)  ){
  L2Flag = 0;
      L2=millis();
};
 if ((L3State == 0) &&  (L1Flag = 3) && ((millis()-L3)>Inibit)  ){
  L3Flag = 0;
      L3=millis();
};
    if ((L4State == 0) &&  (L1Flag = 4) && ((millis()-L4)>Inibit)  ){
  L4Flag = 0;

L1Flag should not be used for lane2, 3 and 4....
Back to top Go down
View user profile
steve112



Number of posts : 3
Registration date : 2013-10-26

PostSubject: Difference   20/9/2017, 13:05

Hi gus,
Sorry for the problems, you are right.

This version was not completed, so there are some mistakes.

As soon as possible i will post the new one..


Many thanks

Steve

OK Done....
Back to top Go down
View user profile
gus797



Number of posts : 6
Registration date : 2017-07-05

PostSubject: Re: Sketch with 4 lanes- Pause Button & Power control for Analogue Track   20/9/2017, 16:41

Hi Steve,

No issue, no hurt :-)

I was just surprised that your initial post from 2013 did not receive any comment. There are so many people outside trying to implement their own sketch in order to use the wonderful arduino in combination with PCLC... most of them only wants to have a simple system for detection including a bridge, an interface (arduino) and PCLC, without too much complications like penalties or fuel management. I think your post would satisfy lots of people if you could just add a link to existing posts explaining how to build a bridge.

Mine is a DS racing bridge for 2 lanes where I kept the original red connector in place but unused, and soldered 4 wires from the inside of the bridge in order to be connected to the arduino. It works fine, with the few corrections on your sketch :-)

kr

Phil
Back to top Go down
View user profile
Sponsored content




PostSubject: Re: Sketch with 4 lanes- Pause Button & Power control for Analogue Track   

Back to top Go down
 
Sketch with 4 lanes- Pause Button & Power control for Analogue Track
View previous topic View next topic Back to top 
Page 1 of 1
 Similar topics
-
» Dead battery, Climate control stuck on,trac light stuck off
» Sargent EC155 Power Control System problem
» Broadway Sargent control panel fault
» FAQ: How to tune the PCM? (Series II SC)
» Quick Track Button repair

Permissions in this forum:You cannot reply to topics in this forum
Pc Lap Counter :: Hardware :: Arduino-
Jump to: