SHARE
TWEET

Arduino Nixie Temperature box

Badjer1983 Dec 6th, 2017 296 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <OneWire.h>
  2. #include <math.h>
  3. #include <DallasTemperature.h>
  4.  
  5. OneWire  ds(19);  // on pin 19 (a 4.7K resistor is necessary)
  6. int digOne = 0, digTwo = 0, digThree = 0; //digOne - Tens, digTwo - Ones, digThree - Decimal
  7. //Declaration of Arduino pins for layout with Arduino Leonardo. Set these variable names as unchanging constants to drive IC Chip BCD patterns.
  8. //Tens
  9. const int tenC=13; //yellow
  10. const int tenB=12; //blue
  11. const int tenD=11; //green
  12. const int tenA=10; //orange
  13. //Ones
  14. const int oneC=9; //yellow
  15. const int oneB=8; //blue
  16. const int oneD=7; //green
  17. const int oneA=6; //orange
  18. //Decimal
  19. const int decC=5; //blue
  20. const int decB=4; //green
  21. const int decD=3; //yellow
  22. const int decA=2; //orange
  23. //int count = 0;
  24. //initialize variables, pin modes, start using libraries
  25.  
  26.  
  27. void setup(void) {
  28.   Serial.begin(9600);
  29.  
  30. //Establish the pins as outputs.
  31. pinMode(tenA, OUTPUT); //LSB
  32. pinMode(tenB, OUTPUT);
  33. pinMode(tenC, OUTPUT);
  34. pinMode(tenD, OUTPUT); //MSB
  35.  
  36. pinMode(oneA, OUTPUT); //LSB
  37. pinMode(oneB, OUTPUT);        
  38. pinMode(oneC, OUTPUT);      
  39. pinMode(oneD, OUTPUT); //MSB
  40.  
  41. pinMode(decA, OUTPUT); //LSB
  42. pinMode(decB, OUTPUT);
  43. pinMode(decC, OUTPUT);
  44. pinMode(decD, OUTPUT); //MSB
  45.  
  46. // Run through the digits on all nixies to prevent cathode poisoning - on start up only.
  47. //This code cycles through the digits of a Nixie Tube.
  48. for (int i = 0; i < 5; i++)
  49.  {//0
  50.  digitalWrite(decD, LOW);  //D1 MSB
  51.  digitalWrite(decC, LOW);  //C1 MSB -1
  52.  digitalWrite(decB, LOW);  //B1 MSB -2
  53.  digitalWrite(decA, LOW);  //A1 LSB
  54.  
  55.  digitalWrite(oneD, LOW);  //D2
  56.  digitalWrite(oneC, LOW);  //C2
  57.  digitalWrite(oneB, LOW);  //B2
  58.  digitalWrite(oneA, LOW);  //A2
  59.  
  60.  digitalWrite(tenD, LOW);  //D3
  61.  digitalWrite(tenC, LOW);  //C3
  62.  digitalWrite(tenB, LOW);  //B3
  63.  digitalWrite(tenA, LOW);  //A3
  64.  
  65.  delay(500);
  66.  
  67.  //1
  68.  digitalWrite(decD, LOW);  //D1
  69.  digitalWrite(decC, LOW);  //C1
  70.  digitalWrite(decB, LOW);  //B1
  71.  digitalWrite(decA, HIGH); //A1
  72.  
  73.  digitalWrite(oneD, LOW);  //D2
  74.  digitalWrite(oneC, LOW);  //C2
  75.  digitalWrite(oneB, LOW);  //B2
  76.  digitalWrite(oneA, HIGH);  //A2
  77.  
  78.  digitalWrite(tenD, LOW);  //D3
  79.  digitalWrite(tenC, LOW);  //C3
  80.  digitalWrite(tenB, LOW);  //B3
  81.  digitalWrite(tenA, HIGH);  //A3
  82.  
  83.  delay(500);
  84.  
  85.  //2
  86.  digitalWrite(decD, LOW);  //D1
  87.  digitalWrite(decC, LOW);  //C1
  88.  digitalWrite(decB, HIGH); //B1
  89.  digitalWrite(decA, LOW);  //A1
  90.  
  91.  digitalWrite(oneD, LOW);  //D2
  92.  digitalWrite(oneC, LOW);  //C2
  93.  digitalWrite(oneB, HIGH);  //B2
  94.  digitalWrite(oneA, LOW);  //A2
  95.  
  96.  digitalWrite(tenD, LOW);  //D3
  97.  digitalWrite(tenC, LOW);  //C3
  98.  digitalWrite(tenB, HIGH);  //B3
  99.  digitalWrite(tenA, LOW);  //A3
  100.  
  101.  delay(500);
  102.  
  103.  //3
  104.  digitalWrite(decD, LOW);  //D1
  105.  digitalWrite(decC, LOW);  //C1
  106.  digitalWrite(decB, HIGH); //B1
  107.  digitalWrite(decA, HIGH); //A1
  108.  
  109.  digitalWrite(oneD, LOW);  //D2
  110.  digitalWrite(oneC, LOW);  //C2
  111.  digitalWrite(oneB, HIGH);  //B2
  112.  digitalWrite(oneA, HIGH);  //A2
  113.  
  114.  digitalWrite(tenD, LOW);  //D3
  115.  digitalWrite(tenC, LOW);  //C3
  116.  digitalWrite(tenB, HIGH);  //B3
  117.  digitalWrite(tenA, HIGH);  //A3
  118.  
  119.  delay(500);
  120.  
  121.  //4
  122.  digitalWrite(decD, LOW);  //D1
  123.  digitalWrite(decC, HIGH); //C1
  124.  digitalWrite(decB, LOW);  //B1
  125.  digitalWrite(decA, LOW);  //A1
  126.  
  127.  digitalWrite(oneD, LOW);  //D2
  128.  digitalWrite(oneC, HIGH);  //C2
  129.  digitalWrite(oneB, LOW);  //B2
  130.  digitalWrite(oneA, LOW);  //A2
  131.  
  132.  digitalWrite(tenD, LOW);  //D3
  133.  digitalWrite(tenC, HIGH);  //C3
  134.  digitalWrite(tenB, LOW);  //B3
  135.  digitalWrite(tenA, LOW);  //A3
  136.  
  137.  delay(500);
  138.  
  139.  //5
  140.  digitalWrite(decD, LOW);  //D1
  141.  digitalWrite(decC, HIGH); //C1
  142.  digitalWrite(decB, LOW);  //B1
  143.  digitalWrite(decA, HIGH); //A1
  144.  
  145.  digitalWrite(oneD, LOW);  //D2
  146.  digitalWrite(oneC, HIGH);  //C2
  147.  digitalWrite(oneB, LOW);  //B2
  148.  digitalWrite(oneA, HIGH);  //A2
  149.  
  150.  digitalWrite(tenD, LOW);  //D3
  151.  digitalWrite(tenC, HIGH);  //C3
  152.  digitalWrite(tenB, LOW);  //B3
  153.  digitalWrite(tenA, HIGH);  //A3
  154.  
  155.  delay(500);
  156.  
  157.  //6
  158.  digitalWrite(decD, LOW);  //D1
  159.  digitalWrite(decC, HIGH); //C1
  160.  digitalWrite(decB, HIGH); //B1
  161.  digitalWrite(decA, LOW);  //A1
  162.  
  163.  digitalWrite(oneD, LOW);  //D2
  164.  digitalWrite(oneC, HIGH);  //C2
  165.  digitalWrite(oneB, HIGH);  //B2
  166.  digitalWrite(oneA, LOW);  //A2
  167.  
  168.  digitalWrite(tenD, LOW);  //D3
  169.  digitalWrite(tenC, HIGH);  //C3
  170.  digitalWrite(tenB, HIGH);  //B3
  171.  digitalWrite(tenA, LOW);  //A3
  172.  
  173.  delay(500);
  174.  
  175.  //7
  176.  digitalWrite(decD, LOW);  //D1
  177.  digitalWrite(decC, HIGH); //C1
  178.  digitalWrite(decB, HIGH); //B1
  179.  digitalWrite(decA, HIGH); //A1
  180.  
  181.  digitalWrite(oneD, LOW);  //D2
  182.  digitalWrite(oneC, HIGH);  //C2
  183.  digitalWrite(oneB, HIGH);  //B2
  184.  digitalWrite(oneA, HIGH);  //A2
  185.  digitalWrite(decC, LOW);  //C1
  186.  digitalWrite(decB, LOW);  //B1
  187.  
  188.  digitalWrite(tenD, LOW);  //D3
  189.  digitalWrite(tenC, HIGH);  //C3
  190.  digitalWrite(tenB, HIGH);  //B3
  191.  digitalWrite(tenA, HIGH);  //A3
  192.  
  193.  delay(500);
  194.  
  195.  //8
  196.  digitalWrite(decD, HIGH); //D1
  197.  digitalWrite(decA, LOW);  //A1
  198.  
  199.  digitalWrite(oneD, HIGH);  //D2
  200.  digitalWrite(oneC, LOW);  //C2
  201.  digitalWrite(oneB, LOW);  //B2
  202.  digitalWrite(oneA, LOW);  //A2
  203.  
  204.  digitalWrite(tenD, HIGH);  //D3
  205.  digitalWrite(tenC, LOW);  //C3
  206.  digitalWrite(tenB, LOW);  //B3
  207.  digitalWrite(tenA, LOW);  //A3
  208.  
  209.  delay(500);
  210.  
  211.  //9
  212.  digitalWrite(decD, HIGH); //D1
  213.  digitalWrite(decC, LOW);  //C1
  214.  digitalWrite(decB, LOW); //B1
  215.  digitalWrite(decA, HIGH); //A1
  216.  
  217.  digitalWrite(oneD, HIGH);  //D2
  218.  digitalWrite(oneC, LOW);  //C2
  219.  digitalWrite(oneB, LOW);  //B2
  220.  digitalWrite(oneA, HIGH);  //A2
  221.  
  222.  digitalWrite(tenD, HIGH);  //D3
  223.  digitalWrite(tenC, LOW);  //C3
  224.  digitalWrite(tenB, LOW);  //B3
  225.  digitalWrite(tenA, HIGH);  //A3
  226.  
  227.  delay(500);}
  228. }
  229.  
  230. void loop(void) {
  231.   byte i;
  232.   byte present = 0;
  233.   byte type_s;
  234.   byte data[12];
  235.   byte addr[8];
  236.   float celsius, fahrenheit;
  237.  
  238.   if ( !ds.search(addr)) {
  239.     Serial.println("No more addresses.");
  240.     Serial.println();
  241.     ds.reset_search();
  242.     delay(250);
  243.     return;
  244.   }
  245.  
  246.   Serial.print("ROM =");
  247.   for( i = 0; i < 8; i++) {
  248.     Serial.write(' ');
  249.     Serial.print(addr[i], HEX);
  250.   }
  251.  
  252.   if (OneWire::crc8(addr, 7) != addr[7]) {
  253.       Serial.println("CRC is not valid!");
  254.       return;
  255.   }
  256.   Serial.println();
  257.  
  258.   // the first ROM byte indicates which chip
  259.   switch (addr[0]) {
  260.     case 0x10:
  261.       Serial.println("  Chip = DS18S20");  // or old DS1820
  262.       type_s = 1;
  263.       break;
  264.     case 0x28:
  265.       Serial.println("  Chip = DS18B20");
  266.       type_s = 0;
  267.       break;
  268.     case 0x22:
  269.       Serial.println("  Chip = DS1822");
  270.       type_s = 0;
  271.       break;
  272.     default:
  273.       Serial.println("Device is not a DS18x20 family device.");
  274.       return;
  275.   }
  276.  
  277.   ds.reset();
  278.   ds.select(addr);
  279.   ds.write(0x44, 1);        // start conversion, with parasite power on at the end
  280.  
  281.   delay(1000);     // maybe 750ms is enough, maybe not
  282.   // we might do a ds.depower() here, but the reset will take care of it.
  283.  
  284.   present = ds.reset();
  285.   ds.select(addr);    
  286.   ds.write(0xBE);         // Read Scratchpad
  287.  
  288.   Serial.print("  Data = ");
  289.   Serial.print(present, HEX);
  290.   Serial.print(" ");
  291.   for ( i = 0; i < 9; i++) {           // we need 9 bytes
  292.     data[i] = ds.read();
  293.     Serial.print(data[i], HEX);
  294.     Serial.print(" ");
  295.   }
  296.   Serial.print(" CRC=");
  297.   Serial.print(OneWire::crc8(data, 8), HEX);
  298.   Serial.println();
  299.  
  300.   // Convert the data to actual temperature
  301.   // because the result is a 16 bit signed integer, it should
  302.   // be stored to an "int16_t" type, which is always 16 bits
  303.   // even when compiled on a 32 bit processor.
  304.   int16_t raw = (data[1] << 8) | data[0];
  305.   if (type_s) {
  306.     raw = raw << 3; // 9 bit resolution default
  307.     if (data[7] == 0x10) {
  308.       // "count remain" gives full 12 bit resolution
  309.       raw = (raw & 0xFFF0) + 12 - data[6];
  310.     }
  311.   } else {
  312.     byte cfg = (data[4] & 0x60);
  313.     // at lower res, the low bits are undefined, so let's zero them
  314.     if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
  315.     else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
  316.     else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
  317.     //// default is 12 bit resolution, 750 ms conversion time
  318.   }
  319.   /*celsius = (float)raw / 16.0;
  320.   fahrenheit = celsius * 1.8 + 32.0;
  321.   Serial.print("  Temperature = ");
  322.   Serial.print(celsius);
  323.   Serial.print(" Celsius, ");
  324.   Serial.print(fahrenheit);
  325.   Serial.println(" Fahrenheit");*/
  326.   celsius = (float)raw / 16.0;
  327.   fahrenheit = celsius * 1.8 + 32.0;
  328.   Serial.print("  Temperature = ");
  329.   Serial.print(fahrenheit);
  330.   Serial.print("     ");
  331.   Serial.print(fahrenheit,1);
  332.   Serial.print("     ");
  333.  int c1 = (fahrenheit +.05) * 10;
  334.  Serial.print(c1);
  335.  Serial.print(" c1 fahrenheit x10 ");
  336.  
  337.  
  338.   double temperature = (fahrenheit + 0.05);
  339.  
  340.   //digOne - Tens
  341.   temperature/=10;
  342.   digOne = temperature;
  343.   to_tenBCD();
  344.  
  345.   //digTwo - Ones
  346.   temperature = (temperature-digOne)*10;
  347.   digTwo = temperature;
  348.   to_oneBCD();
  349.  
  350.   //digThree - Decimal
  351.   temperature = (temperature-digTwo)*10;
  352.   digThree = temperature;
  353.   to_decBCD();        
  354.  
  355. }
  356.  
  357. void to_decBCD() //Evaluate and display decimal
  358. {
  359.     if (digThree == 0) //write 0000
  360.     {
  361.      digitalWrite(decA, LOW);
  362.      digitalWrite(decB, LOW);
  363.      digitalWrite(decC, LOW);
  364.      digitalWrite(decD, LOW);
  365.     }
  366.     if (digThree == 1) //write 0001
  367.     {
  368.      digitalWrite(decA, HIGH);
  369.      digitalWrite(decB, LOW);
  370.      digitalWrite(decC, LOW);
  371.      digitalWrite(decD, LOW);
  372.     }
  373.     if (digThree == 2) //write 0010
  374.     {
  375.      digitalWrite(decA, LOW);
  376.      digitalWrite(decB, HIGH);
  377.      digitalWrite(decC, LOW);
  378.      digitalWrite(decD, LOW);
  379.     }
  380.     if (digThree == 3) //write 0011
  381.     {
  382.      digitalWrite(decA, HIGH);
  383.      digitalWrite(decB, HIGH);
  384.      digitalWrite(decC, LOW);
  385.      digitalWrite(decD, LOW);
  386.     }
  387.     if (digThree == 4) //write 0100
  388.     {
  389.      digitalWrite(decA, LOW);
  390.      digitalWrite(decB, LOW);
  391.      digitalWrite(decC, HIGH);
  392.      digitalWrite(decD, LOW);
  393.     }
  394.     if (digThree == 5) //write 0101
  395.     {
  396.      digitalWrite(decA, HIGH);
  397.      digitalWrite(decB, LOW);
  398.      digitalWrite(decC, HIGH);
  399.      digitalWrite(decD, LOW);
  400.     }
  401.     if (digThree == 6) //write 0110
  402.     {
  403.      digitalWrite(decA, LOW);
  404.      digitalWrite(decB, HIGH);
  405.      digitalWrite(decC, HIGH);
  406.      digitalWrite(decD, LOW);
  407.     }
  408.     if (digThree == 7) //write 0111
  409.     {
  410.      digitalWrite(decA, HIGH);
  411.      digitalWrite(decB, HIGH);
  412.      digitalWrite(decC, HIGH);
  413.      digitalWrite(decD, LOW);
  414.     }
  415.     if (digThree == 8) //write 1000
  416.     {
  417.      digitalWrite(decA, LOW);
  418.      digitalWrite(decB, LOW);
  419.      digitalWrite(decC, LOW);
  420.      digitalWrite(decD, HIGH);
  421.     }
  422.     if (digThree == 9) //write 1001
  423.     {
  424.      digitalWrite(decA, HIGH);
  425.      digitalWrite(decB, LOW);
  426.      digitalWrite(decC, LOW);
  427.      digitalWrite(decD, HIGH);
  428.     }
  429. }
  430. void to_oneBCD()  //Evaluate and display Ones
  431. {
  432.     if (digTwo == 0) //write 0000
  433.     {
  434.      digitalWrite(oneA, LOW);
  435.      digitalWrite(oneB, LOW);
  436.      digitalWrite(oneC, LOW);
  437.      digitalWrite(oneD, LOW);
  438.     }
  439.     if (digTwo == 1) //write 0001
  440.     {
  441.      digitalWrite(oneA, HIGH);
  442.      digitalWrite(oneB, LOW);
  443.      digitalWrite(oneC, LOW);
  444.      digitalWrite(oneD, LOW);
  445.     }
  446.     if (digTwo == 2) //write 0010
  447.     {
  448.      digitalWrite(oneA, LOW);
  449.      digitalWrite(oneB, HIGH);
  450.      digitalWrite(oneC, LOW);
  451.      digitalWrite(oneD, LOW);
  452.     }
  453.     if (digTwo == 3) //write 0011
  454.     {
  455.      digitalWrite(oneA, HIGH);
  456.      digitalWrite(oneB, HIGH);
  457.      digitalWrite(oneC, LOW);
  458.      digitalWrite(oneD, LOW);
  459.     }
  460.     if (digTwo == 4) //write 0100
  461.     {
  462.      digitalWrite(oneA, LOW);
  463.      digitalWrite(oneB, LOW);
  464.      digitalWrite(oneC, HIGH);
  465.      digitalWrite(oneD, LOW);
  466.     }
  467.     if (digTwo == 5) //write 0101
  468.     {
  469.      digitalWrite(oneA, HIGH);
  470.      digitalWrite(oneB, LOW);
  471.      digitalWrite(oneC, HIGH);
  472.      digitalWrite(oneD, LOW);
  473.     }
  474.     if (digTwo == 6) //write 0110
  475.     {
  476.      digitalWrite(oneA, LOW);
  477.      digitalWrite(oneB, HIGH);
  478.      digitalWrite(oneC, HIGH);
  479.      digitalWrite(oneD, LOW);
  480.     }
  481.     if (digTwo == 7) //write 0111
  482.     {
  483.      digitalWrite(oneA, HIGH);
  484.      digitalWrite(oneB, HIGH);
  485.      digitalWrite(oneC, HIGH);
  486.      digitalWrite(oneD, LOW);
  487.     }
  488.     if (digTwo == 8) //write 1000
  489.     {
  490.      digitalWrite(oneA, LOW);
  491.      digitalWrite(oneB, LOW);
  492.      digitalWrite(oneC, LOW);
  493.      digitalWrite(oneD, HIGH);
  494.     }
  495.     if (digTwo == 9) //write 1001
  496.     {
  497.      digitalWrite(oneA, HIGH);
  498.      digitalWrite(oneB, LOW);
  499.      digitalWrite(oneC, LOW);
  500.      digitalWrite(oneD, HIGH);
  501.     }
  502. }
  503. void to_tenBCD()  //Evaluate and display Tens
  504. {
  505.     if (digOne == 0) //write 0000
  506.     {
  507.      digitalWrite(tenA, LOW);
  508.      digitalWrite(tenB, LOW);
  509.      digitalWrite(tenC, LOW);
  510.      digitalWrite(tenD, LOW);
  511.     }
  512.     if (digOne == 1) //write 0001
  513.     {
  514.      digitalWrite(tenA, HIGH);
  515.      digitalWrite(tenB, LOW);
  516.      digitalWrite(tenC, LOW);
  517.      digitalWrite(tenD, LOW);
  518.     }
  519.     if (digOne == 2) //write 0010
  520.     {
  521.      digitalWrite(tenA, LOW);
  522.      digitalWrite(tenB, HIGH);
  523.      digitalWrite(tenC, LOW);
  524.      digitalWrite(tenD, LOW);
  525.     }
  526.     if (digOne == 3) //write 0011
  527.     {
  528.      digitalWrite(tenA, HIGH);
  529.      digitalWrite(tenB, HIGH);
  530.      digitalWrite(tenC, LOW);
  531.      digitalWrite(tenD, LOW);
  532.     }
  533.     if (digOne == 4) //write 0100
  534.     {
  535.      digitalWrite(tenA, LOW);
  536.      digitalWrite(tenB, LOW);
  537.      digitalWrite(tenC, HIGH);
  538.      digitalWrite(tenD, LOW);
  539.     }
  540.     if (digOne == 5) //write 0101
  541.     {
  542.      digitalWrite(tenA, HIGH);
  543.      digitalWrite(tenB, LOW);
  544.      digitalWrite(tenC, HIGH);
  545.      digitalWrite(tenD, LOW);
  546.     }
  547.     if (digOne == 6) //write 0110
  548.     {
  549.      digitalWrite(tenA, LOW);
  550.      digitalWrite(tenB, HIGH);
  551.      digitalWrite(tenC, HIGH);
  552.      digitalWrite(tenD, LOW);
  553.     }
  554.     if (digOne == 7) //write 0111
  555.     {
  556.      digitalWrite(tenA, HIGH);
  557.      digitalWrite(tenB, HIGH);
  558.      digitalWrite(tenC, HIGH);
  559.      digitalWrite(tenD, LOW);
  560.     }
  561.     if (digOne == 8) //write 1000
  562.     {
  563.      digitalWrite(tenA, LOW);
  564.      digitalWrite(tenB, LOW);
  565.      digitalWrite(tenC, LOW);
  566.      digitalWrite(tenD, HIGH);
  567.     }
  568.     if (digOne == 9) //write 1001
  569.     {
  570.      digitalWrite(tenA, HIGH);
  571.      digitalWrite(tenB, LOW);
  572.      digitalWrite(tenC, LOW);
  573.      digitalWrite(tenD, HIGH);
  574.     }  
  575. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top