Tiago Santos
Published © GPL3+

Arduino and LEGO® Power Functions

Project for controlling a LEGO Train with Arduino and LEGO® Power Functions.

IntermediateFull instructions provided10,982
Arduino and LEGO® Power Functions

Things used in this project

Story

Read more

Code

Code snippet #1

Plain text
/*
* Arduino and LEGO Power Functions
* dark_storm@groundzero.com.pt
*/
#include <Servo.h>
#include <legopowerfunctions.h>
Servo servoMain; // Define our Servo
int curSpeed = 0;
// IR led on port 13
LEGOPowerFunctions lego(13);
int irPin = 12;
int count = 0;
int buttonState=0;
int fwdRev=0;
int stationPin = 2;
int buzzerPin = 11;
//irPin - conta as voltas
//count - variavel que conta as voltas
//stationpin - para o comboio na estacao
//buzzerpin - apito de saida de estacao
void setup()
{
 Serial.begin(9600);
 pinMode(irPin, INPUT);
 pinMode(stationPin,INPUT);
 servoMain.attach(10); // servo on digital pin 10
 pinMode(buzzerPin,OUTPUT);
}
void loop()
{ 
 if ( digitalRead(irPin) == 0 ) 
 {
  count++;
  delay(3000);
 }
 if (count <= 1)
 {
  lego.SingleOutput( PWM, PWM_FWD5, RED, CH1);
  delay(100);
  if (digitalRead(stationPin) == 0)
  {
   lego.SingleOutput( PWM, PWM_FLT, RED, CH1);
   servoMain.write(0); // Turn Servo Left to 45 degrees
   delay(2000);
   tone(11,2000,1000);
   delay(500);
  }
 }
 else
 {
  servoMain.write(90); // Turn Servo Left to 45 degrees
  count = 0; 
 }
}

Github

https://github.com/d4rks70rm/ArduinoLegoTrain

Credits

Tiago Santos

Tiago Santos

1 project • 3 followers

Comments