Advertisement
DragNfLy

Stepper DS18B20 V2

May 16th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | None | 0 0
  1. /*
  2.  * Stepper DS18B20 V2
  3.  */
  4.  
  5. #include <OneWire.h>
  6.  
  7. int DS18S20_Pin = 12; // DS18S20 on digital 12
  8.  
  9. // Temperature chip i/o
  10. OneWire ds(DS18S20_Pin); // on digital pin 12
  11.  
  12. // Variables
  13. float temperature = 25.0; //sensor updates this
  14.  
  15. int substeps = 16;
  16.  
  17. int    stepperApinDir  = 7;
  18. int    stepperApinStep = 4;
  19. double stepperAcurrPosition = 0;
  20. double stepperAdestPosition = 0;
  21. double stepperAtimer = 0;
  22. int    stepperAtoggle = 0;
  23. double stepperAspeed = 4800 / substeps; //higher is slower
  24.  
  25. void setup(void) {
  26.  
  27.   Serial.begin(9600);
  28.   Serial.println("Initializing...");
  29.   Serial.println(temperature);
  30.  
  31.   //READY!
  32.   delay(1000);
  33. }
  34.  
  35. void loop(void) {
  36.  
  37.   temperature = getTemp();
  38.   Serial.println(temperature);
  39.  
  40.   if (temperature < 27.0) {
  41.     //Close vents.
  42.     stepperAdestPosition = 100;
  43.  
  44.     Serial.println("CHECKPOINT 001");
  45.   }
  46.  
  47.   if (temperature > 28.0) {
  48.     //Open vents.
  49.     stepperAdestPosition = 200; //calibrate these!
  50.  
  51.     Serial.println("CHECKPOINT 002");
  52.   }
  53.  
  54.   while ((stepperAdistanceToGo() != 0) ) {
  55.     stepperArun();
  56.   }
  57.   delay(1000);
  58. }
  59.  
  60. // THESE CONTROL THE STEPPER MOTORS
  61.  
  62. double stepperAdistanceToGo() {
  63.   return abs(stepperAdestPosition - stepperAcurrPosition);
  64. }
  65.  
  66. void stepperArun() {
  67.   double dir = 0;
  68.  
  69.   if (stepperAdistanceToGo() >= 1) {
  70.     //SET DIRECTION
  71.     if (stepperAdestPosition > stepperAcurrPosition) {
  72.       digitalWrite(stepperApinDir, 1);
  73.       dir = 1;
  74.     } else {
  75.       digitalWrite(stepperApinDir, 0);
  76.       dir = -1;
  77.     }
  78.     if (micros() < (stepperAtimer - stepperAspeed)) {
  79.       //rollover bug... fix
  80.       stepperAtimer = micros();
  81.     }
  82.     //PULSE AT SPEED LIMIT
  83.     if (micros() >= (stepperAtimer + stepperAspeed)) {
  84.       digitalWrite(stepperApinStep, stepperAtoggle);
  85.       stepperAtimer = micros();
  86.       stepperAtoggle = !stepperAtoggle;
  87.       stepperAcurrPosition += dir;
  88.     }
  89.   }
  90.   Serial.println("STEPPER RUN COMPLETE");
  91. }
  92.  
  93. float getTemp() {
  94.   //DS18S20 temp in DEG Celsius
  95.  
  96.   byte data[12];
  97.   byte addr[8];
  98.  
  99.   if ( !ds.search(addr)) {
  100.     // no more sensors on chain, reset search
  101.     ds.reset_search();
  102.     return -1000;
  103.   }
  104.  
  105.   if ( OneWire::crc8( addr, 7) != addr[7]) {
  106.     Serial.println("CRC is not valid!");
  107.     return -1000;
  108.   }
  109.  
  110.   if ( addr[0] != 0x10 && addr[0] != 0x28) {
  111.     Serial.print("Device is not recognized");
  112.     return -1000;
  113.   }
  114.  
  115.   ds.reset();
  116.   ds.select(addr);
  117.   ds.write(0x44, 1); // start conversion, with parasite power on at the end
  118.  
  119.   byte present = ds.reset();
  120.   ds.select(addr);
  121.   ds.write(0xBE); // Read Scratchpad
  122.  
  123.  
  124.   for (int i = 0; i < 9; i++) { // we need 9 bytes
  125.     data[i] = ds.read();
  126.   }
  127.  
  128.   ds.reset_search();
  129.  
  130.   byte MSB = data[1];
  131.   byte LSB = data[0];
  132.  
  133.   float tempRead = ((MSB << 8) | LSB); // using two's compliment
  134.   float TemperatureSum = tempRead / 16;
  135.  
  136.   return TemperatureSum;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement