> For the complete documentation index, see [llms.txt](https://pcmola.gitbook.io/sample-space/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pcmola.gitbook.io/sample-space/github-import/untitled-1/blob/main/pcmola-lego-magic-train-1.md).

# pcmola/lego-magic-train

&#x20;\#include \<PowerFunctions.h> #define IR\_TRANS\_IN 2 //IR Trans PIN #define IR\_DEBUG\_OFF 0 //IR Debug Mode Off #define IR\_DEBUG\_ON 1 //IR Debug Mode On //IR Channels #define CH1 0x0 #define CH2 0x1 #define CH3 0x2 #define CH4 0x3 #define TRIGGER\_PIN 8 // Trig pin #define ECHO\_PIN 9 // Echo pin #define NEAREST\_VALUE 50 // Maximun Speed Distance(mm) #define OFFSET 60 // Distance(mm) Between Speed long lDistance = 0; // Distance(mm) long lBeforeValue = 0; // Before measurement distance value long lCurrentValue = 1; // Before measurement distance value int forceStopValue = 0; // Value for periodically sending a stop signal PowerFunctions pf(IR\_TRANS\_IN, CH1, IR\_DEBUG\_ON); void setup() { pinMode(TRIGGER\_PIN, OUTPUT); // Trigger is an output pin pinMode(ECHO\_PIN, INPUT); // Echo is an input pin Serial.begin(9600); // Serial Output } void loop() { lDistance = getDistance(); Serial.print("Distance = "); // Output to serial Serial.print(lDistance); Serial.print("mm"); operateTrain(RED, lDistance); Serial.println(); delay(100); // Wait to do next measurement } // get distance using ultrasonic sensor(HC-SR04) long getDistance() { long distance = 0; long duration = 0; digitalWrite(TRIGGER\_PIN, LOW); delayMicroseconds(2); digitalWrite(TRIGGER\_PIN, HIGH); // Trigger pin to HIGH delayMicroseconds(10); // 10us high digitalWrite(TRIGGER\_PIN, LOW); // Trigger pin to HIGH duration = pulseIn(ECHO\_PIN, HIGH); // Waits for the echo pin to get high // returns the dration in microseconds // Calculates the distance in mm // ((time)\*(Speed of sound))/ toward and backward of object) \* 10 distance = ((duration / 2.9) / 2); // Actual calculation in mm //distance = duration / 74 / 2; // Actual calculation in inches return distance; } // operat train using IR transmitter(Keyes KY-005) void operateTrain(uint8\_t output, long lDistance) { if (lDistance > 0 && lDistance < NEAREST\_VALUE) { lCurrentValue = PWM\_FWD7; } else if (lDistance >= NEAREST\_VALUE && lDistance < NEAREST\_VALUE + OFFSET) { lCurrentValue = PWM\_FWD6; } else if (lDistance >= NEAREST\_VALUE + OFFSET && lDistance < NEAREST\_VALUE + OFFSET \* 2) { lCurrentValue = PWM\_FWD5; } else if (lDistance >= NEAREST\_VALUE + OFFSET \* 2 && lDistance < NEAREST\_VALUE + OFFSET \* 3) { lCurrentValue = PWM\_FWD4; } else if (lDistance >= NEAREST\_VALUE + OFFSET \* 3 && lDistance < NEAREST\_VALUE + OFFSET \* 4) { lCurrentValue = PWM\_FWD3; } else if (lDistance >= NEAREST\_VALUE + OFFSET \* 4 && lDistance < NEAREST\_VALUE + OFFSET \* 5) { lCurrentValue = PWM\_FWD2; } else if (lDistance >= NEAREST\_VALUE + OFFSET \* 5 && lDistance < NEAREST\_VALUE + OFFSET \* 6) { lCurrentValue = PWM\_FWD1; } else if ( lDistance >= NEAREST\_VALUE + OFFSET \* 6) { lCurrentValue = PWM\_BRK; } Serial.print(" PWM\_CODE : "); Serial.print(lCurrentValue); if (lCurrentValue != lBeforeValue) { pf.single\_pwm(output, lCurrentValue); Serial.print(" IR\_SIGNAL\_SENT"); } lBeforeValue = lCurrentValue; forceStopValue++; if (forceStopValue > 100) { if ( lCurrentValue == PWM\_BRK ) { pf.single\_pwm(output, lCurrentValue); Serial.print(" PERIODICALLY\_PWM\_BRK\_SENT"); } forceStopValue = 0; } }
