//Control data: #define selbit(x) (1 << x) //#define wissel selbit(0) #define select selbit(0) #define forward selbit(5) #define backwards selbit(6) #define left selbit(8) #define right selbit(7) #define liftUp selbit(10) #define liftDown selbit(9) #define grapClose selbit(11) #define grapOpen selbit(12) unsigned int mask = select; //pinout: int vccIn = 3; int sync = 2; int clk = 1; int data = 0; int led = 13; //global vars: int pulseCount = 0; unsigned long millisOld = 0; bool startClk = false; bool rokenBokVCCOn = false; //=================================================================================== //=================================================================================== void setup() { delay(2000); //delay so we can upload, when stuck in loop.(arduino leonardo bug.) pinMode(7, INPUT); pinMode(6, INPUT); pinMode(5, INPUT); pinMode(4, INPUT); pinMode(led, OUTPUT); pinMode(vccIn, INPUT); pinMode(sync, INPUT); pinMode(clk, INPUT); pinMode(data, OUTPUT); Serial.begin(250000); digitalWrite(data,LOW); attachInterrupt(digitalPinToInterrupt(sync), syncInterrupt, FALLING); attachInterrupt(digitalPinToInterrupt(clk), clkInterrupt, RISING); attachInterrupt(digitalPinToInterrupt(vccIn), rokenBokPowerCheck, CHANGE ); } //=================================================================================== //=================================================================================== void loop() { readFromPC(); } //=================================================================================== //gives information on connection with rokenbok control station and its power state: //=================================================================================== void rokenBokPowerCheck() { int readRokenBokVCC = digitalRead(vccIn); digitalWrite(led,readRokenBokVCC); if(readRokenBokVCC == 1) { rokenBokVCCOn = true; Serial.print("Rokenbok control station online!"); }else if(readRokenBokVCC == 0) { rokenBokVCCOn = false; Serial.print("Rokenbok control station not online!"); } } //=================================================================================== //Is called when sync pin detects interrupt, this wil reset the pulsecount: //=================================================================================== void syncInterrupt() { pulseCount = 0; digitalWrite(data,LOW); startClk = rokenBokVCCOn; //if control station is offline, block sending data. } //========================================================================================================== //Is called when clk pin detects interrupt, this wil start counting pulses and send data on the right pulse: //========================================================================================================== void clkInterrupt() { if(startClk == true) { pulseCount++; digitalWrite(data,LOW); if(mask & (1 << pulseCount - 1) && mask != 0xff) { digitalWrite(data,HIGH); } if(pulseCount == 17) { startClk = false; //block sending multiple select commands in a row: if(mask == select) { mask = 0xff; } } } } //=================================================================================== //This is called each loop to receive command from pc: //=================================================================================== void readFromPC() { while(Serial.available() == 0); while(Serial.available() > 0) { char chr = Serial.read(); if(chr == '1') { //mask = wissel; //does nothing yet! mask = 0x00; } if(chr == '0') { mask = select; } if(chr == 'z') { mask = forward; } if(chr == 's') { mask = backwards; } if(chr == 'q') { mask = left; } if(chr == 'd') { mask = right; } if(chr == '8') { mask = liftUp; } if(chr == '2') { mask = liftDown; } if(chr == '4') { mask = grapClose; } if(chr == '6') { mask = grapOpen; } if(chr == '!') { mask = 0xff; } } }