Kushagra Keshari
Published © CC BY-NC-ND

Keyboard Controlled Model Train | PS/2 Interface

A variation of the keyboard controlled model train, controlled by a PS/2 keyboard.

IntermediateFull instructions provided2 hours1,258
Keyboard Controlled Model Train | PS/2 Interface

Things used in this project

Hardware components

Arduino UNO & Genuino UNO
Arduino UNO & Genuino UNO
×1
Jumper wires (generic)
Jumper wires (generic)
Always keep some extra, they might be needed.
×8
Male/Female Jumper Wires
Male/Female Jumper Wires
×3
Adafruit 12V DC 1000mA (1A) regulated switching power adapter
Use this for small N-scale layouts.
×1
Adafruit 12V 5A switching power supply
Use this for larger scales and/or larger layouts.
×1
Female PS/2 connector
Use a connector like this one. You might find one for cheap from a computer repair shop.
×1
PS/2 keyboard
You can use almost any keyboard as long as it follows the set standards and works.
×1
L298N Dual H Bridge
×1

Story

Read more

Code

PS2_keyboard_controlled_model_train.ino

Arduino
/*
 * Arduino program to control a model train with a PS/2 keyboard.
 * Made by Tech Build: https://www.youtube.com/channel/UCNy7DyfhSD9jsQEgNwETp9g?sub_confirmation=1
 * Watch this project in action:https://youtu.be/94yJJhv92OE
 */
#include <PS2Keyboard.h>

const int DataPin = 3;
const int IRQpin =  2;

PS2Keyboard keyboard;


int s=0,i=125;

void motor_go(){
 if(s>=1&&s<=255){
  digitalWrite(9,LOW);
  digitalWrite(8,HIGH);
  analogWrite(10,s);
 }
 if(s<=-1&&s>=-255){
  digitalWrite(8,LOW);
  digitalWrite(9,HIGH);
  analogWrite(10,-s);
 }
 if(s==0){
  digitalWrite(9,LOW);
  digitalWrite(8,LOW);
  analogWrite(10,s);
 }
}

void motor_halt(int i){ //Function to stop and/or turn off the locomotive.
  if(s>30){
    for(s=s;s>30;s--){
    motor_go();
    delay(i);
    }
 }
  if(s<=30&&s>0&&i<=60){
    for(s=s;s!=0;s--){
    motor_go();
    delay(60);
    }
  }
  if(s<-30){
    for(s=s;s!=-30;s++){
    motor_go();
    delay(i);
    }
  }
  if(s>=-30&&s<0){
    for(s=s;s!=0;s++){
    motor_go();
    delay(60);
    }
  }
}
void setup() {
  // put your setup code here, to run once:
  keyboard.begin(DataPin, IRQpin);
  Serial.begin(9600);
  pinMode(10,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(8,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (keyboard.available()) {
    
    // read the next key
    char c = keyboard.read();
    
    // check for some of the special keys
    switch(c){
      case PS2_UPARROW:
      if(s<255){
        s++;
      }
      break;
    

      case PS2_DOWNARROW:
      if(s>-255){
        s--;
      }
      break;

      case PS2_TAB:
      if(s<30&&s>=0){
        for(s=s;s<30;s++){
          motor_go();
          delay(60);
        }
        break;
      }
      if(s==30){
        for(s=s;s!=0;s--){
          motor_go();
          delay(60);
        }
      }
      break; 
      
      case ' ':
      motor_halt(125);
      break;

      case PS2_ENTER:
      motor_halt(60);
      break;
     
    }
    
    
}
    //Serial.println(s);
    motor_go();
}

Credits

Kushagra Keshari

Kushagra Keshari

14 projects • 28 followers
A casual electronics and a model railway hobbyist.

Comments