Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- N(erf)-OS V 1.0
- May 2012
- nerf@nilshaack.de
- Free to use, modify, distribute for non-commercial use
- */
- /*
- The Graphics LCD Library that was used for this project ist located here:
- https://github.com/TCWORLD/gLCD-Library/downloads
- The Version used is v2.3
- */
- #include <gLCD.h>
- /*
- ************************************************************
- ************************************************************
- BLASTER COFNFIGURATION
- ************************************************************
- ************************************************************
- */
- int buttonDelay = 350;
- const double irGateDistance = 9.8;
- const int triggerPause = 0;
- const int burst = 3;
- int burstSize = 3;
- long blasterCycleMax = 750000 ; // timeout value for fire cycle
- /*
- ********************************
- ********************************
- PIN CONFIGURATION
- ********************************
- ********************************
- */
- const int ledRed = A0;
- const int ledYellow = A1;
- const int ledGreen = A2;
- //int unassignedOut1 = A3 (mit Vorwiederstand für LED beschaltet)
- //int unassignedIn1 = A4 (mit PullUp Resistor beschaltet)
- const int relais = A5;
- const int button1 = 0;
- const int button2 = 1;
- const int button3 = 2;
- const int irGateOne = 5;
- const int checkLoaded = 6;
- const int tubePos = 3;
- const int trigger = 4;
- const int clip = 7;
- const char RST = 8; // Display
- const char CS = 9; // Display
- int irGateTwo = 10;
- const char Dat = 11; // Display
- //int unassignedIn2 = 12 (unbeschaltet)
- const char Clk = 13; // Display
- /*
- ********************************
- ********************************
- Initialize Variables
- ********************************
- ********************************
- */
- gLCD graphic(RST,CS,Clk,Dat,1);
- String displaycontent, resulttext;
- String blasterMessage[3];
- char s[32];
- int messagePosition = 0;
- int checkLoadedValue = 0;
- int checkLoadedValueLast = 0;
- int checkClipValue = 0;
- int checkClipValueLast = 0;
- int checkTubePosValue = 0;
- int checkTubePosValueLast = 0;
- int irGateOneValue = 0;
- int irGateTwoValue = 0;
- int blasterClipSize = 0;
- int blasterClipRemain = 0;
- int triggerCountRunning = 0;
- int triggerCountSinceClipChange = 0;
- int triggerCountSinceCycleStart = 0;
- int triggerValue = 0;
- int triggerValueLast = 0;
- long blasterCycleStart ; // storage for start timestamp of fire cycle
- long blasterCycleElapsed ; // storage for "lap-time" of fire cycle
- long blasterCycleSingleTime = 0;
- long blasterCycleMode = 1 ; // Which mode is the blaster running in
- boolean blasterCycleRunning = false; // storage for current fire cycle status
- boolean blasterCycleDriver = 0; // storage for current fire cycle status
- double dartspeed = 0;
- long blasterGateOne ; // storage for timestamp of passing dart at Gate 1
- long blasterGateTwo ; // storage for timestamp of passing dart at Gate 1
- void setup() {
- //Serial.begin(9600);
- pinMode(button1, INPUT);
- pinMode(button2, INPUT);
- pinMode(button3, INPUT);
- pinMode(clip, INPUT);
- pinMode(tubePos, INPUT);
- pinMode(trigger, INPUT);
- pinMode(checkLoaded, INPUT);
- pinMode(irGateOne, INPUT);
- pinMode(irGateTwo, INPUT);
- pinMode(ledRed, OUTPUT);
- pinMode(ledYellow, OUTPUT);
- pinMode(ledGreen, OUTPUT);
- pinMode(relais, OUTPUT);
- // Initialize Display
- graphic.Init(0,0,0,1,3); //Reversed X, Revesed Y direction.
- graphic.Contrast(0x3C); // slightly stronger contrast
- // Do selfcheck if specific event on startup
- if (digitalRead(button3) == LOW) {
- selftest();
- }
- digitalWrite(ledGreen,HIGH);
- blasterCycleRunning = false;
- blasterDisplayUpdate();
- checkLoadedValue = digitalRead(checkLoaded);
- if (check_TubeQuick() == false || check_ClipQuick() == false || check_DartQuick() == false) {
- if (check_ClipQuick() == false) {blasterDisplayClip(1);} else {blasterDisplayClip(0);}
- if (check_DartQuick() == false) {blasterDisplayLoad(1);} else {blasterDisplayClip(0);}
- blasterManageMessages("I'm not happy!",2);
- } else { blasterDisplayClip(0);blasterDisplayLoad(0);
- blasterManageMessages("Welcome Player",3);
- }
- }
- /*
- ************************************************************
- Blaster Function: Blaster Cycle Manager
- ************************************************************
- */
- void blasterSTOP() {
- digitalWrite(relais, LOW);
- blasterCycleRunning = false;
- triggerCountSinceCycleStart = 0;
- }
- void blasterManagerCycleSemi () {
- triggerValue = digitalRead(trigger);
- checkTubePosValue = digitalRead(tubePos);
- }
- void blasterManagerCycleBurst () {
- triggerValue = digitalRead(trigger);
- checkTubePosValue = digitalRead(tubePos);
- }
- void blasterManagerCycleAuto () {
- triggerValue = digitalRead(trigger);
- checkTubePosValue = digitalRead(tubePos);
- }
- void blasterManageCycle () {
- triggerValue = digitalRead(trigger);
- checkTubePosValue = digitalRead(tubePos);
- /* if (checkTubePosValue == HIGH) {
- digitalWrite(relais,LOW);
- }
- */
- // Blaster not running
- // Änderung State Trigger ... wird jetzt gedrückt
- // The blaster is not within a shooting cycle (it is resting)
- if (blasterCycleRunning == false) {
- if ((triggerValue == LOW && triggerValueLast == HIGH) || (triggerValue == LOW && triggerValueLast == LOW) ) {
- //NEUE SCHUSS-SERIE
- if ( blasterCycleMode == -1 || (blasterCycleMode == 1 && triggerCountSinceCycleStart == 0) || (blasterCycleMode == burstSize && triggerCountSinceCycleStart <= burstSize-1)) {
- if (check_DartQuick() == true && check_ClipQuick() == true && check_BarrelFree() == true && check_BlasterSafety() == true) {
- blasterCycleStart = micros();
- digitalWrite(relais, HIGH);
- digitalWrite(ledGreen,LOW);
- blasterCycleRunning = true;
- } else if (check_DartQuick() == false) {
- blasterManageMessages("Check Dart",2);
- } else if (check_ClipQuick() == false) {
- blasterManageMessages("Load Status Confusing",0);
- }
- }
- triggerValueLast = triggerValue;
- } else if (triggerValue == HIGH && triggerValueLast == LOW) {
- blasterSTOP();
- digitalWrite(ledGreen, HIGH);
- triggerValueLast = triggerValue;
- } else if (triggerValue == LOW) {
- }
- }
- // The blaster is within a shooting cycle
- if (blasterCycleRunning == true) {
- // Do the following, when the blaster cycle is controlles event driven
- if (blasterCycleDriver == 1) {
- // if more than 150ms have passed and the tube is in resting position, close cycle
- if ( micros() - blasterCycleStart >= 150000 && digitalRead(tubePos) == HIGH) {
- digitalWrite(relais, LOW);
- }
- }
- if (digitalRead(irGateOne) == LOW) {
- blasterGateOne = micros();
- dartspeed = 0;
- }
- if (digitalRead(irGateTwo) == LOW) {
- blasterGateTwo = micros();
- blasterCycleElapsed = (blasterGateTwo - blasterGateOne);
- dartspeed = (irGateDistance/blasterCycleElapsed)*14400;
- blasterCycleSingleTime = blasterGateTwo - blasterCycleStart;
- blasterGateOne = 0;
- blasterGateTwo = 0;
- triggerCountSinceCycleStart++;
- triggerCountSinceClipChange++;
- triggerCountRunning++;
- blasterCycleRunning = false;
- digitalWrite(ledGreen,HIGH);
- digitalWrite(relais, LOW);
- digitalWrite(ledYellow,LOW);
- digitalWrite(ledRed,LOW);
- blasterDisplayTriggerTime(dartspeed,blasterCycleSingleTime);
- blasterDisplayLoadMessage();
- delay(triggerPause);
- }
- // Spätestens mit Timeout des Blasters
- if ( micros() - blasterCycleStart >= blasterCycleMax && blasterCycleRunning == true) {
- blasterSTOP();
- blasterDisplayTriggerTime(0,0);
- blasterGateOne = 0;
- blasterGateTwo = 0;
- blasterManageMessages("TIMEOUT / SERVICE",0);
- }
- }
- }
- /*
- ************************************************************
- The central system loop
- ************************************************************
- */
- void loop() {
- check_ClipSystem();
- if (blasterCycleRunning == false) {
- check_DartLoad();
- blasterMenuQuick();
- }
- blasterManageCycle();
- }
- /*
- ************************************************************
- ************************************************************
- Check Functions
- ************************************************************
- ************************************************************
- */
- boolean check_TubeQuick() {
- checkTubePosValue = digitalRead(clip);
- checkTubePosValueLast = checkTubePosValue;
- if (checkTubePosValue == HIGH) {return true;} else {return false;}
- }
- boolean check_ClipQuick() {
- checkClipValue = digitalRead(clip);
- checkClipValueLast = checkClipValue;
- if (checkClipValue == HIGH) {return true;} else {return false;}
- }
- boolean check_ClipSystem() {
- checkClipValue = digitalRead(clip);
- if (checkClipValue == HIGH && checkClipValueLast == LOW) {
- blasterManageMessages("CLIP SOLVED",3);
- blasterDisplayClip(0);
- blasterClipSize = -1;
- blasterClipRemain = -1;
- triggerCountSinceClipChange = 0;
- checkClipValueLast = checkClipValue;
- return true;
- } else
- if(checkClipValue == LOW && checkClipValueLast == HIGH ) {
- blasterManageMessages("CLIP Missing",1);
- checkClipValueLast = checkClipValue;
- blasterDisplayClip(1);
- return false;
- }
- }
- boolean check_DartQuick() {
- checkLoadedValue = digitalRead(checkLoaded);
- checkLoadedValueLast = checkLoadedValue;
- if (checkLoadedValue == LOW) {return true;} else {return false;}
- }
- boolean check_DartLoad() {
- checkLoadedValue = digitalRead(checkLoaded);
- if (checkLoadedValue == LOW && checkLoadedValueLast == HIGH) {
- blasterManageMessages("DART FOUND",3);
- blasterDisplayLoad(0);
- checkLoadedValueLast = checkLoadedValue;
- return true;
- } else
- if(checkLoadedValue == HIGH && checkLoadedValueLast == LOW) {
- blasterManageMessages("DART MISSING",0);
- blasterDisplayLoad(1);
- checkLoadedValueLast = checkLoadedValue;
- return false;
- }
- return false;
- }
- boolean check_BarrelFree() {
- irGateOneValue = digitalRead(irGateOne);
- irGateTwoValue = digitalRead(irGateTwo);
- if (irGateOneValue == HIGH && irGateTwoValue == HIGH) {
- // blasterManageMessages("DART SOLVED",3);
- //checkLoadedValueLast = checkLoadedValue;
- return true;
- } else {
- displaycontent = "Barrel-Safety: " + String(irGateOneValue) + " - " + String(irGateTwoValue);
- blasterManageMessages(displaycontent,0);
- // checkLoadedValueLast = checkLoadedValue;
- return false;
- }
- return false;
- }
- boolean check_BlasterSafety() {
- return true;
- }
- /*
- ************************************************************
- Blaster Function: Blaster Message Manager
- ************************************************************
- */
- void blasterManageMessages(String incommingMessage,int priority) {
- if (incommingMessage != blasterMessage[0]) {
- if (priority == 0) {
- // blasterSTOP();
- graphic.SetForeColour(15,0,0);
- digitalWrite(ledRed, HIGH);delay(100);digitalWrite(ledRed, LOW);delay(100);
- digitalWrite(ledRed, HIGH);delay(100);digitalWrite(ledRed, LOW);delay(100);
- digitalWrite(ledRed, HIGH);delay(100);digitalWrite(ledRed, LOW);delay(100);
- } else if (priority == 1) {
- graphic.SetForeColour(15,0,0);
- digitalWrite(ledRed, HIGH);
- } else if (priority == 2) {
- digitalWrite(ledYellow, HIGH);
- graphic.SetForeColour(8,8,0);
- } else if (priority == 3) {
- graphic.SetForeColour(0,15,0);
- digitalWrite(ledRed, LOW);
- digitalWrite(ledYellow, LOW);
- }
- blasterMessage[0] = incommingMessage;
- graphic.SetBackColour(0,0,0);
- graphic.Box(0,50,132,67,0);
- graphic.Line(0,49,132,49,1);
- graphic.Line(0,69,132,69,1);
- messagePosition=(132-(incommingMessage.length()*6))/2;
- graphic.Print(incommingMessage,messagePosition,52,2);
- }
- if (priority == 0) {
- digitalWrite(ledRed, HIGH); delay(300);digitalWrite(ledRed, LOW); delay(150); digitalWrite(ledRed, HIGH); delay(300);digitalWrite(ledRed, LOW);delay(150);digitalWrite(ledRed, HIGH); delay(300);digitalWrite(ledRed, LOW);
- }
- }
- void blasterDisplayUpdate() {
- graphic.SetBackColour(0,0,0);
- graphic.Clear();
- graphic.SetBackColour(15,15,15);
- graphic.Box(0,11,132,49,0);
- //graphic.SetBackColour(10,10,0);
- //graphic.Box(0,50,132,68,0);
- //Print out the test pattern.
- graphic.SetForeColour(15,15,15);
- graphic.Print("Nerf Operating System",3,2,0);
- graphic.SetForeColour(0,0,0);
- graphic.Print("Remain: not known",3,15,2);
- graphic.Print("Mode : Semi Auto",3,32,2);
- graphic.SetForeColour(0,10,0);
- graphic.Print("Clip Status : ---",3,72,0);
- graphic.Print("Load Status : ---",3,82,0);
- graphic.Print("Last Velocity : ---",3,92,0);
- graphic.Print("Cycle Time : ---",3,102,0);
- graphic.Print("Trigger Sleep : 0.00",3,112,0);
- graphic.SetForeColour(0,7,0);
- graphic.Print("Cycle Mode : time",3,121,0);
- graphic.SetBackColour(15,15,15);
- }
- void blasterDisplayClip(int toggle) {
- graphic.SetBackColour(0,0,0);
- graphic.Box(104,72,131,80,0);
- if (toggle == 1) {
- graphic.SetForeColour(15,0,0);
- graphic.Print("miss",105,72,0);
- } else {
- graphic.SetForeColour(0,8,0);
- graphic.Print(" ok",105,72,0);
- }
- }
- void blasterDisplayLoad(int toggle) {
- graphic.SetBackColour(0,0,0);
- graphic.Box(104,82,131,90,0);
- if (toggle == 1) {
- graphic.SetForeColour(15,0,0);
- graphic.Print("miss",105,82,0);
- } else {
- graphic.SetForeColour(0,8,0);
- graphic.Print(" ok",105,82,0);
- }
- }
- void blasterDisplayLoadMessage () {
- graphic.SetBackColour(15,15,15); // white
- graphic.SetForeColour(0,0,0); // black
- graphic.Box(63,14,131,32,0);
- String clipSizeString;
- if (blasterClipRemain < 10) {displaycontent = "__";} else if (blasterClipRemain < 100) {displaycontent = "_";}
- if (blasterClipSize < 10) {clipSizeString = "_";} else {clipSizeString = "";}
- if (blasterClipRemain > 0) {
- blasterClipRemain--;
- displaycontent = displaycontent + String(blasterClipRemain);
- graphic.Print(displaycontent + " of " + clipSizeString + String(blasterClipSize),63,15,2);
- if (blasterClipRemain <= 4 && blasterClipRemain > 1) {
- blasterManageMessages("Prepare Reload",2);
- } else if(blasterClipRemain <=1) {
- blasterManageMessages("! RELOAD SOON !",2);
- }
- } else if (blasterClipRemain == 0) {
- displaycontent = displaycontent + String(triggerCountSinceClipChange);
- graphic.Print(displaycontent + " total",63,15,2);
- blasterManageMessages("AMMO DEPLETED",2);
- } else if (triggerCountSinceClipChange <= 35) {
- displaycontent = displaycontent + String(triggerCountSinceClipChange);
- graphic.Print(displaycontent + " total",63,15,2);
- blasterManageMessages("~ SMOOTH ~",3);
- } else {
- displaycontent = displaycontent + String(triggerCountSinceClipChange);
- graphic.Print(displaycontent + " total",63,15,2);
- blasterManageMessages("GREEDY BASTARD",3);
- }
- }
- void blasterDisplayTriggerTime(double velocity, double time) {
- time = time/1000000;
- graphic.SetBackColour(0,0,0);
- graphic.Box(98,92,131,110,0);
- graphic.SetForeColour(15,15,0);
- if (velocity < 10) {
- graphic.Print(dtostrf(velocity,3,2,s),105,92,0);
- } else if (velocity < 100) {
- graphic.Print(dtostrf(velocity,3,2,s),99,92,0);
- } else {
- graphic.Print(dtostrf(velocity,3,2,s),93,92,0);
- }
- graphic.Print(dtostrf(time,3,2,s),105,102,0);
- }
- /*
- ************************************************************
- QUICK NENU
- ************************************************************
- */
- void blasterMenuQuick() {
- if (blasterCycleRunning == false) {
- // Quickbutton für Clipsize nach Clipwechsel
- if (digitalRead(button1) == LOW ) {
- if (blasterCycleMode == -1) {
- displaycontent = "Semi Auto";
- blasterCycleMode = 1;
- triggerCountSinceCycleStart = 0;
- } else if(blasterCycleMode == 1) {
- blasterCycleMode = burstSize;
- displaycontent = String(burstSize) + "x - Burst";
- triggerCountSinceCycleStart = 0;
- }else if(blasterCycleMode == burstSize) {
- displaycontent = "Full Auto";
- blasterCycleMode = -1;
- triggerCountSinceCycleStart = 0;
- }
- //graphic.Print(displaycontent,3,70,0);
- graphic.SetBackColour(15,15,15); // white
- graphic.SetForeColour(0,0,0); // white
- graphic.Box(62,32,131,48,0);
- graphic.Print(displaycontent,63,32,2);
- delay(buttonDelay);
- }
- if (digitalRead(button3) == LOW ) {
- if (blasterCycleDriver == 1) {
- displaycontent = " time";
- blasterCycleDriver = 0;
- } else if(blasterCycleDriver == 0) {
- displaycontent = "event";
- blasterCycleDriver = 1;
- }
- graphic.SetBackColour(0,0,0);
- graphic.Box(98,121,131,129,0);
- graphic.SetForeColour(15,15,0);
- graphic.Print(displaycontent,99,121,0);
- delay(buttonDelay);
- }
- // Quickbutton für Clipsize nach Clipwechsel
- if (digitalRead(button2) == LOW && triggerCountSinceClipChange <= 1) {
- if (blasterClipSize == -1) {
- blasterClipSize = 0;
- blasterClipRemain = triggerCountSinceClipChange;
- }
- if (blasterClipSize == 0) {
- blasterClipSize = 6;
- blasterClipRemain = blasterClipSize - triggerCountSinceClipChange;
- } else if(blasterClipSize == 6) {
- blasterClipSize = 18;
- blasterClipRemain = blasterClipSize - triggerCountSinceClipChange;
- }else if(blasterClipSize ==18) {
- blasterClipSize = 36;
- blasterClipRemain = blasterClipSize - triggerCountSinceClipChange;
- }else if(blasterClipSize ==36) {
- blasterClipSize = 6;
- blasterClipRemain = blasterClipSize - triggerCountSinceClipChange;
- }
- graphic.SetBackColour(15,15,15); // white
- graphic.SetForeColour(0,0,0); //black
- if (blasterClipRemain < 10) {displaycontent = "__";} else
- if (blasterClipRemain < 100) {displaycontent = "_";}
- String clipSizeString;
- if (blasterClipSize < 10) {clipSizeString = "_";} else {clipSizeString = "";}
- displaycontent = displaycontent + String(blasterClipRemain);
- graphic.Box(63,15,131,32,0);
- graphic.Print(displaycontent + " of " + clipSizeString + String(blasterClipSize),63,15,2);
- delay(buttonDelay);
- }
- }
- }
- /*
- ************************************************************
- Blaster Function: Selftest
- ************************************************************
- */
- void selftest() {
- graphic.SetBackColour(0,0,0);
- graphic.Clear();
- graphic.SetForeColour(15,15,15);
- graphic.Print("Initial Blaster Status Test",3,2,0);
- if (digitalRead(clip) == LOW) {
- graphic.Print("ClipLoad: MISSING",3,12,0);
- } else {
- graphic.Print("ClipLoad: ------",3,12,0);
- }
- if (digitalRead(checkLoaded) == HIGH) {
- graphic.Print("DartLoad: MISSING",3,22,0);
- } else {
- graphic.Print("DartLoad: ------",3,22,0);
- }
- if (digitalRead(irGateOne) == HIGH) {
- graphic.Print("irGate1 : ------",3,32,0);
- } else {
- graphic.Print("irGate1 : BLOCKED",3,32,0);
- }
- if (digitalRead(irGateTwo) == HIGH) {
- graphic.Print("irGate2 : ------",3,42,0);
- } else {
- graphic.Print("irGate2 : BLOCKED",3,42,0);
- }
- if (digitalRead(button1) == HIGH) {
- graphic.Print("Button1 : ------",3,52,0);
- } else {
- graphic.Print("Button1 : ACTIVE",3,52,0);
- }
- if (digitalRead(button2) == HIGH) {
- graphic.Print("Button2 : ------",3,62,0);
- } else {
- graphic.Print("Button2 : ACTIVE",3,62,0);
- }
- if (digitalRead(button3) == HIGH) {
- graphic.Print("Button3 : ------",3,72,0);
- } else {
- graphic.Print("Button3 : ACTIVE",3,72,0);
- }
- if (digitalRead(trigger) == LOW) {
- graphic.Print("Trigger : ACTIVE",3,82,0);
- } else {
- graphic.Print("Trigger : ------",3,82,0);
- }
- if (digitalRead(tubePos) == LOW) {
- graphic.Print("Tube Pos: ACTIVE",3,92,0);
- } else {
- graphic.Print("Tube Pos: ------",3,92,0);
- }
- graphic.Print("System : v1.0",3,120,0);
- digitalWrite(ledGreen,HIGH);
- digitalWrite(ledYellow,HIGH);
- digitalWrite(ledRed,HIGH);
- delay(buttonDelay);
- digitalWrite(ledYellow,LOW);
- digitalWrite(ledRed,LOW);
- delay(1500);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement