ARDUINO & PCLAPCOUNTER & SCALEXTRIC

9

Posted by rdm | Posted in ARDUINO, scalextric | Posted on 18-03-2014

Aquí os dejo como voy llevo el código para hacer funcionar el programa que controla la información, una pista de scalextric PCLAPCOUNTER   www.pclapcounter.com  con un Arduino.

En mi caso he usado el arduino mega, 28 leds y un lcd 20×4. Este código no es definitivo, pero ya es funcional.

arduinomegapclapcounter

 

 

 

 

 

CÓDIGO ACTUAL  (19/03/2014)

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 4);

//start
// define which pins are to be used for the various light functions
#define RED1 22// 1st red pin   1
#define RED2 23// 2st red pin   2
#define RED3 24// 3st red pin   3
#define RED4 25// 4st red pin   4
#define RED5 26// 5st red pin   5
#define RED6 27// STOP LIGT     6

#define GREEN1 30// GO LIGT     7
#define YELLOW1 31// BANDERA AMARILLA 8

#define POWER1 32// CARRIL 1 POWER
#define POWER2 33// CARRIL 2 POWER

#define FALSESTART1 34// CARRIL 1 FS
#define FALSESTART2 35// CARRIL 2 FS

#define STOPGO1 36// CARRIL 1 STOP&GO
#define STOPGO2 37// CARRIL 2 STOP&GO

#define FIRSTPLACE1 40// CARRIL 1 FIRST PLACE
#define FIRSTPLACE2 41// CARRIL 2 FIRST PLACE

#define OUTFUEL1 42// CARRIL 1 OUT FUEL
#define OUTFUEL2 43// CARRIL 2 OUT FUEL

#define LOWFUEL1 44// CARRIL 1 LOW FUEL
#define LOWFUEL2 45// CARRIL 2 LOW FUEL

#define MAXFUEL1 46// CARRIL 1 MAX FUEL
#define MAXFUEL2 47// CARRIL 2 MAX FUEL

#define PITSTOP1 48// CARRIL 1 PIT STOP
#define PITSTOP2 49// CARRIL 2 PIT STOP

int ledPin = 13;
unsigned long nextBlink;
char ledState = LOW;
const int PWRPin = 2;

String inData;
char recieved;
int charsReceived = 0;

// setup the pins for outputs, blinking and open the serial port
void setup()
{
lcd.init();                      // initialize the lcd
lcd.backlight();
digitalWrite (PWRPin, HIGH);
lcd.setCursor (3,0); lcd.print(“PC LAP COUNTER”);
lcd.setCursor (4,1); lcd.print(“ARDUINO MEGA”);
lcd.setCursor (1,2); lcd.print(“24 LEDS & LCD 20×4”);
lcd.setCursor (0,3); lcd.print(“v.19/03/2014”);
pinMode(GREEN1, OUTPUT);
pinMode(RED1, OUTPUT);
pinMode(RED2, OUTPUT);
pinMode(RED3, OUTPUT);
pinMode(RED4, OUTPUT);
pinMode(RED5, OUTPUT);
pinMode(RED6, OUTPUT);
pinMode(YELLOW1, OUTPUT);
pinMode(POWER1, OUTPUT);
pinMode(POWER2, OUTPUT);
pinMode(FALSESTART1, OUTPUT);
pinMode(FALSESTART2, OUTPUT);
pinMode(STOPGO1, OUTPUT);
pinMode(STOPGO2, OUTPUT);
pinMode(FIRSTPLACE1, OUTPUT);
pinMode(FIRSTPLACE2, OUTPUT);
pinMode(OUTFUEL1, OUTPUT);
pinMode(OUTFUEL2, OUTPUT);
pinMode(LOWFUEL1, OUTPUT);
pinMode(LOWFUEL2, OUTPUT);
pinMode(PITSTOP1, OUTPUT);
pinMode(PITSTOP2, OUTPUT);
pinMode(MAXFUEL1, OUTPUT);
pinMode(MAXFUEL2, OUTPUT);

pinMode(ledPin, OUTPUT);

ledState = LOW;
digitalWrite (GREEN1, HIGH) ;

Serial.begin(9600);

}
// look for data on the serial port and turn on/off the lights
// data is ascii strings

void loop()

{

if ( Serial.available() )
{
String b;
b = Serial.readStringUntil(‘[‘);
{ String s;

s = Serial.readStringUntil(‘]’);

if ( s == “SL010″ ) { digitalWrite(RED1, LOW);  lcd.setCursor(7,3); lcd.print(”     “); }
if ( s == “SL011″ ) { digitalWrite(RED1, HIGH); lcd.setCursor(7,3); lcd.print(”   1 “); }
if ( s == “SL020″ ) { digitalWrite(RED2, LOW);  lcd.setCursor(7,3); lcd.print(”     “); }
if ( s == “SL021″ ) { digitalWrite(RED2, HIGH); lcd.setCursor(7,3); lcd.print(”   2 “); }
if ( s == “SL030″ ) { digitalWrite(RED3, LOW);  lcd.setCursor(7,3); lcd.print(”     “); }
if ( s == “SL031″ ) { digitalWrite(RED3, HIGH); lcd.setCursor(7,3); lcd.print(”   3 “); }
if ( s == “SL040″ ) { digitalWrite(RED4, LOW);  lcd.setCursor(7,3); lcd.print(”     “); }
if ( s == “SL041″ ) { digitalWrite(RED4, HIGH); lcd.setCursor(7,3); lcd.print(”   4 “); }
if ( s == “SL050″ ) { digitalWrite(RED5, LOW);  lcd.setCursor(7,3); lcd.print(”     “); }
if ( s == “SL051″ ) { digitalWrite(RED5, HIGH); lcd.setCursor(7,3); lcd.print(”   5 “); }
if ( s == “SL060″ ) { digitalWrite(GREEN1, LOW);  digitalWrite(PWRPin, LOW);  lcd.setCursor(0,3); lcd.print(”        STOP        “) ; }
if ( s == “SL061″ ) { digitalWrite(GREEN1, HIGH); digitalWrite(PWRPin, HIGH); lcd.setCursor(0,3); lcd.print(”         GO “) ; }

if ( s == “SL070” ) { digitalWrite(RED6, LOW); } //; { digitalWrite(RED2, LOW); } ; { digitalWrite(RED3, LOW); } ; { digitalWrite(RED4, LOW); } ; { digitalWrite(RED5, LOW); } ; { digitalWrite(RED6, LOW); }
if ( s == “SL071” ) { digitalWrite(RED6, HIGH); } //; { digitalWrite(RED2, HIGH); } ; { digitalWrite(RED3, HIGH); } ; { digitalWrite(RED4, HIGH); } ; { digitalWrite(RED5, HIGH); } ; { digitalWrite(RED6, HIGH); }

if ( s == “SL080″ ) { digitalWrite(YELLOW1, LOW); digitalWrite(PWRPin, HIGH); lcd.setCursor(0,2);  lcd.print(”                    “) ; } //YELLOW FLAG OFF
if ( s == “SL081″ ) { digitalWrite(YELLOW1, HIGH); digitalWrite(PWRPin, HIGH); lcd.setCursor(0,2); lcd.print(”  BANDERA AMARILLA  “) ; } //YELLOW FLAG ON

if ( s == “PW000” ) { digitalWrite(POWER1, LOW);  digitalWrite(PWRPin, LOW);  lcd.setCursor(0,0);  lcd.print(“1 Off     “) ; }
if ( s == “PW001” ) { digitalWrite(POWER1, HIGH); digitalWrite(PWRPin, HIGH); lcd.setCursor(0,0);  lcd.print(“1 On      “) ; }
if ( s == “PW010″ ) { digitalWrite(POWER2, LOW);  digitalWrite(PWRPin, LOW);  lcd.setCursor(10,0); lcd.print(”     Off 2″) ; }
if ( s == “PW011″ ) { digitalWrite(POWER2, HIGH); digitalWrite(PWRPin, HIGH); lcd.setCursor(10,0); lcd.print(”     On  2″) ; }

if ( s == “FS010″ ) { digitalWrite(FALSESTART1, LOW); digitalWrite(PWRPin, HIGH);lcd.setCursor(0,2);   lcd.print(”            “); }
if ( s == “FS011” ) { digitalWrite(FALSESTART1, HIGH); digitalWrite(PWRPin, HIGH);lcd.setCursor(0,2);  lcd.print(“FALSE START “); }
if ( s == “FS020″ ) { digitalWrite(FALSESTART2, LOW); digitalWrite(PWRPin, HIGH); lcd.setCursor(10,2); lcd.print(”            “); }
if ( s == “FS021” ) { digitalWrite(FALSESTART2, HIGH); digitalWrite(PWRPin, HIGH);lcd.setCursor(10,2); lcd.print(“FALSE START “); }

if ( s == “SG010″ ) { digitalWrite(STOPGO1, LOW); digitalWrite(PWRPin, HIGH);  lcd.setCursor(0,2);  lcd.print(”          “); }
if ( s == “SG011” ) { digitalWrite(STOPGO1, HIGH);digitalWrite(PWRPin, HIGH);  lcd.setCursor(0,2);  lcd.print(“STOP & GO “); }
if ( s == “SG020″ ) { digitalWrite(STOPGO2, LOW); digitalWrite(PWRPin, HIGH);  lcd.setCursor(10,2); lcd.print(”          “); }
if ( s == “SG021″ ) { digitalWrite(STOPGO2, HIGH); digitalWrite(PWRPin, HIGH); lcd.setCursor(10,2); lcd.print(” STOP & GO”); }

if ( s == “FP010” ) { digitalWrite(FIRSTPLACE1, LOW); digitalWrite(PWRPin, HIGH); lcd.setCursor(0,3); lcd.print(“SEGUNDO”); }
if ( s == “FP011” ) { digitalWrite(FIRSTPLACE1, HIGH); digitalWrite(PWRPin, LOW); lcd.setCursor(0,3); lcd.print(“PRIMERO”); }
if ( s == “FP020” ) { digitalWrite(FIRSTPLACE2, LOW); digitalWrite(PWRPin, HIGH); lcd.setCursor(13,3);lcd.print(“SEGUNDO”); }
if ( s == “FP021” ) { digitalWrite(FIRSTPLACE2, HIGH); digitalWrite(PWRPin, LOW); lcd.setCursor(13,3);lcd.print(“PRIMERO”); }

if ( s == “OF010” ) { digitalWrite(OUTFUEL1, LOW); }
if ( s == “OF011″ ) { digitalWrite(OUTFUEL1, HIGH); digitalWrite(MAXFUEL1, LOW ) ; digitalWrite(PWRPin, HIGH); lcd.setCursor(0,1);  lcd.print(”   VACIO  “); }
if ( s == “OF020” ) { digitalWrite(OUTFUEL2, LOW); }
if ( s == “OF021″ ) { digitalWrite(OUTFUEL2, HIGH); digitalWrite(MAXFUEL2, LOW ) ; digitalWrite(PWRPin, HIGH); lcd.setCursor(10,1); lcd.print(”   VACIO  “); }

if ( s == “LF010” ) { digitalWrite(LOWFUEL1, LOW); }
if ( s == “LF011” ) { digitalWrite(LOWFUEL1, HIGH);  digitalWrite(MAXFUEL1, LOW ) ; digitalWrite(PWRPin, HIGH); lcd.setCursor(0,1);lcd.print(“GAS BAJO  “);  }
if ( s == “LF020” ) { digitalWrite(LOWFUEL2, LOW); }
if ( s == “LF021″ ) { digitalWrite(LOWFUEL2, HIGH);  digitalWrite(MAXFUEL2, LOW ) ; digitalWrite(PWRPin, HIGH); lcd.setCursor(10,1); lcd.print(” GAS BAJO “); }

if ( s == “MF011″ ) { digitalWrite(MAXFUEL1, LOW);  digitalWrite(PWRPin, LOW); lcd.setCursor(0,1);   lcd.print(”          “) ; }
if ( s == “MF010” ) { digitalWrite(MAXFUEL1, HIGH); digitalWrite(PWRPin, HIGH); lcd.setCursor(0,1);  lcd.print(“GAS LLENO “) ; }
if ( s == “MF021″ ) { digitalWrite(MAXFUEL2, LOW);  digitalWrite(PWRPin, LOW); lcd.setCursor(10,1);  lcd.print(”          “) ; }
if ( s == “MF020″ ) { digitalWrite(MAXFUEL2, HIGH); digitalWrite(PWRPin, HIGH); lcd.setCursor(10,1); lcd.print(” GAS LLENO”) ; }

if ( s == “PT010″ ) { digitalWrite(PITSTOP1, LOW); digitalWrite(PWRPin, HIGH); lcd.setCursor(0,2);   lcd.print(”          “); }
if ( s == “PT011″ ) { digitalWrite(PITSTOP1, HIGH); digitalWrite(PWRPin, HIGH); lcd.setCursor(0,2);  lcd.print(” EN BOXES “); }//digitalWrite (YELLOW1, HIGH) ; digitalWrite(GREEN1, HIGH ) ; }
if ( s == “PT020″ ) { digitalWrite(PITSTOP2, LOW); digitalWrite(PWRPin, HIGH);  lcd.setCursor(10,2); lcd.print(”          “); }
if ( s == “PT021″ ) { digitalWrite(PITSTOP2, HIGH); digitalWrite(PWRPin, HIGH);lcd.setCursor(10,2);  lcd.print(” EN BOXES “); }//digitalWrite (YELLOW2, HIGH) ; digitalWrite(GREEN2, HIGH ) ; }
}
}
inData = “”; // Clear recieved buffer
}
//end