ASZK

ctr.point_standby

Apr 10th, 2021
775
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Arduino.h>
  2.  
  3. //Definicja pinow
  4.  
  5. //Przyciski
  6. const int Przycisk1pin = 5;
  7. const int Buzzerpin = 4;
  8.  
  9. //LEDY
  10. const int ledpin_red = 8;
  11. const int ledpin_green = 7;
  12. const int ledpin_blue = 6;
  13. const int ledcelownik = 10;
  14.  
  15. //Podczerwien
  16.  
  17. #define txPinIR 3 //IR carrier output
  18.  
  19. #define IRpin_PIN      PINB
  20. #define IRpin          1   // PIN D9 - atmega PB1
  21.  
  22.  
  23. // zmienne zmienne
  24.  
  25. int StanPrzycisk1;
  26. int PoprzedniStanPrzycisk1 = LOW;
  27.  
  28. unsigned long PoprzedniDebounceTime = 0;
  29. unsigned long debounceDelay = 50;
  30.  
  31. unsigned int HoldTime = 1000;
  32. unsigned long StartHold;
  33. unsigned long standbystart;
  34.  
  35. int timesetold;
  36. int timeset=2;
  37. int timeout=10;
  38. bool nowsettingtimer=false;
  39.  
  40. int bit = 0;
  41.  
  42. String Received = "";
  43. String Receivedold = "";
  44.  
  45. // dla obslugi Irki
  46.  
  47. unsigned char carrierFreq = 56; //default
  48. unsigned char period = 0;      //calculated once for each signal sent in initSoftPWM
  49. unsigned char periodHigh = 0;  //calculated once for each signal sent in initSoftPWM
  50. unsigned char periodLow = 0;   //calculated once for each signal sent in initSoftPWM
  51. unsigned long sigTime = 0;     //used in mark & space functions to keep track of time
  52. unsigned long sigStart = 0;    //used to calculate correct length of existing signal, to handle some repeats
  53.  
  54. #define NEC_HEX_VALUE 0x20DF22DDUL //UL makes this an unsigned long
  55. #define NEC_BIT_COUNT 24           // header +24 for message
  56. /////////////////////////////////////////////////////////////////////
  57. // MilesTag command pack  (0x...  ...UL)
  58. #define New_GameIM 0x8305E8UL // New Game (Immediate)
  59. #define Explode 0x830BE8UL    //Explode
  60. //////////////////////////////////////////////////////////////////
  61.  
  62. /////////////////////////////////////////////////
  63. const String New_GameIM_rcv = "100000110000010111101000";
  64. const String Admin_Kill_rcv = "100000110000000011101000";
  65. const String Clear_Scores_rcv = "100000110001010011101000";
  66. ///////////////////////////////////////////////////
  67.  
  68.  
  69. uint8_t currentpulse = 0; // index for pulses we're storing
  70. #define MAXPULSE 30
  71. #define NUMPULSES 30
  72.  
  73. #define RESOLUTION 50
  74. uint16_t pulses[NUMPULSES][2];  // pair is high and low pulse
  75.  
  76.  
  77. class Elapsed
  78. {
  79.   unsigned long startus, startms;
  80.  
  81. public:
  82.   // constructor resets time
  83.   Elapsed()
  84.   {
  85.     reset();
  86.   };
  87.  
  88.   // reset time to now
  89.   void reset()
  90.   {
  91.     startus = micros();
  92.     startms = millis();
  93.   };
  94.  
  95.   // return Elapsed time in milliseconds
  96.   unsigned long intervalMs()
  97.   {
  98.     return millis() - startms;
  99.   };
  100.  
  101.   // return Elapsed time in microseconds
  102.   unsigned long intervalUs()
  103.   {
  104.     return micros() - startus;
  105.   };
  106.  
  107. }; // end of class Elapsed
  108.  
  109.  
  110.  
  111. void setup() {
  112.  
  113.   // Definiowanie Pinow in/out
  114.   pinMode(Przycisk1pin, INPUT);
  115.   pinMode(ledpin_red, OUTPUT);
  116.   pinMode(ledpin_green, OUTPUT);
  117.   pinMode(ledpin_blue, OUTPUT);
  118.   pinMode(Buzzerpin, OUTPUT);
  119.   pinMode(txPinIR, OUTPUT);
  120.   pinMode(ledcelownik, OUTPUT);
  121.  
  122.   // Ledy i buzzer off
  123.   digitalWrite(ledcelownik, HIGH);
  124.   digitalWrite(ledpin_red,HIGH);
  125.   digitalWrite(ledpin_green,HIGH);
  126.   digitalWrite(ledpin_blue,HIGH);
  127.   digitalWrite(Buzzerpin, HIGH);
  128.  
  129.   //Rozpoczecie komunikacji z terminalem
  130.   Serial.begin(2000000); // 2MHz- fastest one
  131.   Serial.println("START");
  132.   digitalWrite(Buzzerpin,0);
  133.   delay(100);
  134.   digitalWrite(Buzzerpin,1);
  135.  
  136. }
  137.  
  138.  
  139. void initSoftPWM(unsigned char carrierFreq)
  140. { // Assumes standard 8-bit Arduino, running at 16Mhz
  141.   //supported values are 30, 33, 36, 38, 40, 56 kHz, any other value defaults to 38kHz
  142.   //we will aim for a  duty cycle of circa 33%
  143.  
  144.   period = (1000 + carrierFreq / 2) / carrierFreq;
  145.   periodHigh = (period + 1) / 3; // 3
  146.   periodLow = period - periodHigh;
  147.  
  148.   switch (carrierFreq)
  149.   {
  150.   case 30:           //delivers a carrier frequency of 29.8kHz & duty cycle of 34.52%
  151.     periodHigh -= 6; //Trim it based on measurementt from Oscilloscope
  152.     periodLow -= 10; //Trim it based on measurementt from Oscilloscope
  153.     break;
  154.  
  155.   case 33:           //delivers a carrier frequency of 32.7kHz & duty cycle of 34.64%
  156.     periodHigh -= 6; //Trim it based on measurementt from Oscilloscope
  157.     periodLow -= 10; //Trim it based on measurementt from Oscilloscope
  158.     break;
  159.  
  160.   case 36:           //delivers a carrier frequency of 36.2kHz & duty cycle of 35.14%
  161.     periodHigh -= 6; //Trim it based on measurementt from Oscilloscope
  162.     periodLow -= 11; //Trim it based on measurementt from Oscilloscope
  163.     break;
  164.  
  165.   case 40:           //delivers a carrier frequency of 40.6kHz & duty cycle of 34.96%
  166.     periodHigh -= 6; //Trim it based on measurementt from Oscilloscope
  167.     periodLow -= 11; //Trim it based on measurementt from Oscilloscope
  168.     break;
  169.  
  170.   case 56:           //delivers a carrier frequency of 53.8kHz & duty cycle of 40.86%
  171.     periodHigh -= 6; //Trim it based on measurementt from Oscilloscope 6
  172.     periodLow -= 12; //Trim it based on measurementt from Oscilloscope 12
  173.     //Serial.println(periodHigh);
  174.     //Serial.println(periodLow);
  175.  
  176.     break;
  177.  
  178.   case 38: //delivers a carrier frequency of 37.6kHz & duty cycle of 36.47%
  179.   default:
  180.     periodHigh -= 6; //Trim it based on measurementt from Oscilloscope
  181.     periodLow -= 11; //Trim it based on measurementt from Oscilloscope
  182.     break;
  183.   }
  184. }
  185.  
  186. void mark(unsigned int mLen)
  187. {                  //uses sigTime as end parameter
  188.   sigTime += mLen; //mark ends at new sigTime
  189.   unsigned long now = micros();
  190.   unsigned long dur = sigTime - now; //allows for rolling time adjustment due to code execution delays
  191.   if (dur == 0)
  192.     return;
  193.   while ((micros() - now) < dur)
  194.   { //just wait here until time is up
  195.     digitalWrite(txPinIR, HIGH);
  196.     if (periodHigh)
  197.       delayMicroseconds(periodHigh);
  198.     digitalWrite(txPinIR, LOW);
  199.     if (periodLow)
  200.       delayMicroseconds(periodLow);
  201.   }
  202. }
  203.  
  204. void space(unsigned int sLen)
  205. {                  //uses sigTime as end parameter
  206.   sigTime += sLen; //space ends at new sigTime
  207.   unsigned long now = micros();
  208.   unsigned long dur = sigTime - now; //allows for rolling time adjustment due to code execution delays
  209.   if (dur == 0)
  210.     return;
  211.   while ((micros() - now) < dur)
  212.     ; //just wait here until time is up
  213. }
  214.  
  215. void sendHexNEC(unsigned long sigCode, byte numBits, unsigned char repeats, unsigned char kHz)
  216. {
  217.  
  218. #define NEC_HEADER_MARK 2400
  219. #define NEC_HEADER_SPACE 600
  220. #define NEC_ONE_MARK 1200
  221. #define NEC_ZERO_MARK 600
  222. #define NEC_ONE_SPACE 600
  223. #define NEC_ZERO_SPACE 600
  224. #define NEC_TRAILER_MARK 560
  225.  
  226.   unsigned long bitMask = (unsigned long)1 << (numBits - 1); //allows for signal from 1 bit up to 32 bits
  227.   //
  228.   if (carrierFreq != kHz)
  229.     initSoftPWM(kHz); //we only need to re-initialise if it has changed from last signal sent
  230.  
  231.   sigTime = micros(); //keeps rolling track of signal time to avoid impact of loop & code execution delays
  232.   sigStart = sigTime; //remember for calculating first repeat gap (space), must end 108ms after signal starts
  233.  
  234.   // First send header Mark & Space
  235.   mark(NEC_HEADER_MARK);
  236.   space(NEC_HEADER_SPACE);
  237.  
  238.   while (bitMask)
  239.   {
  240.     if (bitMask & sigCode)
  241.     { //its a One bit
  242.       mark(NEC_ONE_MARK);
  243.       space(NEC_ONE_SPACE);
  244.     }
  245.     else
  246.     { // its a Zero bit
  247.       mark(NEC_ZERO_MARK);
  248.       space(NEC_ZERO_SPACE);
  249.     }
  250.     bitMask = (unsigned long)bitMask >> 1; // shift the mask bit along until it reaches zero & we exit the while loop
  251.   }
  252.   // Last send NEC Trailer MArk
  253.   // mark(NEC_TRAILER_MARK);
  254.  
  255.   if (repeats == 0)
  256.     return; //finished - no repeats
  257.   else if (repeats > 0)
  258.   {                                       //first repeat must start 108ms after first signal
  259.     space(108000 - (sigTime - sigStart)); //first repeat Header should start 108ms after first signal
  260.     mark(NEC_HEADER_MARK);
  261.     space(NEC_HEADER_SPACE / 2); //half the length for repeats
  262.     mark(NEC_TRAILER_MARK);
  263.   }
  264.  
  265.   while (--repeats > 0)
  266.   {                                                                            //now send any remaining repeats
  267.     space(108000 - NEC_HEADER_MARK - NEC_HEADER_SPACE / 2 - NEC_TRAILER_MARK); //subsequent repeat Header must start 108ms after previous repeat signal
  268.     mark(NEC_HEADER_MARK);
  269.     space(NEC_HEADER_SPACE / 2); //half the length for repeats
  270.     mark(NEC_TRAILER_MARK);
  271.   }
  272. }
  273.  
  274. void printpulses(void) {
  275.   //Serial.println("\n\r\n\rReceived:");
  276.       for (uint8_t i = 0; i < currentpulse; i++) {
  277.         bit = (log(((((pulses[i][1]) * (RESOLUTION)) + 50) / 600)) / (log(2))) ;
  278.         Received.concat(bit);  
  279.       }  
  280. }
  281.  
  282. int listenForIR(void) {
  283.   currentpulse = 0;
  284.   while (1) {
  285.     uint16_t highpulse, lowpulse;  // temporary storage timing
  286.     highpulse = lowpulse = 0; // start out with no pulse length
  287.     while (IRpin_PIN & (1 << IRpin)) {
  288.       highpulse++;
  289.       if (highpulse >= 200)
  290.       {
  291.         return currentpulse;
  292.       }    
  293.       delayMicroseconds(RESOLUTION);
  294.      
  295.       if (((highpulse >= MAXPULSE) && (currentpulse != 0)) || currentpulse == NUMPULSES) {
  296.         //printpulses();   //RAW data
  297.             for (uint8_t i = 0; i < currentpulse; i++) {
  298.             bit = (log(((((pulses[i][1]) * (RESOLUTION)) + 50) / 600)) / (log(2))) ;
  299.             Received.concat(bit);  
  300.           }
  301.       Serial.println(Received);    
  302.         return currentpulse;
  303.       }
  304.     }
  305.     pulses[currentpulse][0] = highpulse;
  306.     while (! (IRpin_PIN & _BV(IRpin))) {
  307.       lowpulse++;
  308.       delayMicroseconds(RESOLUTION);      
  309.       if (((lowpulse >= MAXPULSE)  && (currentpulse != 0)) || currentpulse == NUMPULSES) {
  310.         //printpulses(); //RAW data          
  311.             for (uint8_t i = 0; i < currentpulse; i++)
  312.               {
  313.               bit = (log(((((pulses[i][1]) * (RESOLUTION)) + 50) / 600)) / (log(2))) ;
  314.               Received.concat(bit);  
  315.               }
  316.             return currentpulse;
  317.       }
  318.     }
  319.     pulses[currentpulse][1] = lowpulse;
  320.     currentpulse++;
  321.   }
  322. }
  323.  
  324.  
  325. void red()
  326. {
  327.   digitalWrite(ledpin_red, 0);
  328.   digitalWrite(ledpin_blue, 1);
  329.   digitalWrite(ledpin_green, 1);
  330. }
  331. void blue()
  332. {
  333.   digitalWrite(ledpin_red, 1);
  334.   digitalWrite(ledpin_blue, 0);
  335.   digitalWrite(ledpin_green, 1);
  336. }
  337. void green()
  338. {
  339.   digitalWrite(ledpin_red, 1);
  340.   digitalWrite(ledpin_blue, 1);
  341.   digitalWrite(ledpin_green, 0);
  342. }
  343. void white()
  344. {
  345.   digitalWrite(ledpin_red, 0);
  346.   digitalWrite(ledpin_blue, 0);
  347.   digitalWrite(ledpin_green, 0);
  348. }
  349. void cyan()
  350. {
  351.   digitalWrite(ledpin_red, 1);
  352.   digitalWrite(ledpin_blue, 0);
  353.   digitalWrite(ledpin_green, 0);
  354. }
  355. void magenta()
  356. {
  357.   digitalWrite(ledpin_red, 0);
  358.   digitalWrite(ledpin_blue, 0);
  359.   digitalWrite(ledpin_green, 1);
  360. }
  361. void yellow()
  362. {
  363.   digitalWrite(ledpin_red, 0);
  364.   digitalWrite(ledpin_blue, 1);
  365.   digitalWrite(ledpin_green, 0);
  366. }
  367. void led_off()
  368. {
  369.   digitalWrite(ledpin_red, 1);
  370.   digitalWrite(ledpin_blue, 1);
  371.   digitalWrite(ledpin_green, 1);
  372. }
  373.  
  374.  
  375.  
  376. void Timeseting(){
  377.  
  378.   if (nowsettingtimer)
  379.   {
  380.     if(timesetold!=timeset){
  381.   Serial.println("nowsettingtimer");
  382.  
  383.   switch (timeset)
  384.   // ustawianie minutnika dla slupka czas podawany w sekundach
  385.     {
  386.     case 0: // set dla testow 10s
  387.       led_off();
  388.       timeout = 10;
  389.       break;      
  390.     case 1: // 2minuty
  391.       red();
  392.       timeout = 120;
  393.       break;      
  394.     case 2: // 4minuty
  395.       yellow();
  396.       timeout = 240;
  397.       break;
  398.     case 3: // 6minut
  399.       green();
  400.       timeout = 360;
  401.       break;
  402.     case 4: // 8minut
  403.       blue();
  404.       timeout = 480;
  405.       break;
  406.     case 5: //10minut
  407.       cyan();
  408.       timeout = 600;
  409.       break;
  410.     case 6: //20minut
  411.       magenta();
  412.       timeout = 1200;
  413.       break;  
  414.     case 7: //30minut
  415.       white();
  416.       timeout = 1800;
  417.       break;
  418.     case 8:
  419.       timeset=0;
  420.       break;
  421.    
  422.     default:
  423.     Serial.println("default");
  424.       break;
  425.     }
  426.     timesetold=timeset;
  427.  
  428.  
  429.     Serial.println("timeout is set to");
  430.     Serial.print(timeout);
  431.   }
  432.   }
  433. }
  434.  
  435. void Przycisk(){
  436.  
  437.   int odczytprzycisk = digitalRead(Przycisk1pin);
  438.  
  439.     if (odczytprzycisk != PoprzedniStanPrzycisk1 )
  440.         {
  441.           PoprzedniDebounceTime = millis();
  442.         }
  443.    
  444.     if ((millis() - PoprzedniDebounceTime) > debounceDelay)
  445.     {
  446.       if (odczytprzycisk != StanPrzycisk1)
  447.         {
  448.         StanPrzycisk1 = odczytprzycisk;
  449.           if (StanPrzycisk1 == HIGH)
  450.             {
  451.             // Serial.println("PRESS");            
  452.             StartHold = millis();
  453.              }
  454.           if (StanPrzycisk1 == LOW)
  455.           {
  456.             if ((millis() - StartHold) > HoldTime)
  457.                {
  458.                  if(nowsettingtimer){
  459.                       digitalWrite(Buzzerpin,0);
  460.                       delay(500);
  461.                       digitalWrite(Buzzerpin,1);
  462.                       standbystart=millis();
  463.                       nowsettingtimer=false;
  464.                    }
  465.                 if (millis()<3000)
  466.                   {
  467.                     //aktywacja trybu ustawiania minutnika
  468.                     digitalWrite(Buzzerpin,0);
  469.                     delay(500);
  470.                     digitalWrite(Buzzerpin,1);
  471.                     nowsettingtimer=true;
  472.                   }  
  473.                }
  474.                else{
  475.                  if (nowsettingtimer)
  476.                  {
  477.                    timeset++;
  478.                     digitalWrite(Buzzerpin,0);
  479.                     delay(100);
  480.                     digitalWrite(Buzzerpin,1);
  481.                  }
  482.                }
  483.           }          
  484.       }        
  485.     }
  486.     PoprzedniStanPrzycisk1 = odczytprzycisk;    
  487. }
  488.  
  489. void standby(){
  490.   if(!nowsettingtimer){
  491.       if(millis() - standbystart>800){
  492.         white();
  493.         }
  494.       if(millis() - standbystart>1000){
  495.         led_off();
  496.         //sendHexNEC(New_GameIM, NEC_BIT_COUNT, 1, 56);
  497.         standbystart=millis();
  498.       }
  499.       }
  500.  
  501. }
  502.  
  503. // main oska
  504. void loop() {
  505.  
  506.   Received = "";
  507.   listenForIR();
  508.  
  509.   Przycisk();
  510.   Timeseting();
  511.   standby();
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×