Advertisement
ZaynerTech

Arduino Code for Heated Hoodie

Jan 17th, 2013
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.23 KB | None | 0 0
  1. /*
  2.  
  3. Right and up turn up the temperature on the Heat pads
  4. Down and left turn it down
  5.  
  6. Connect the MOSFET to pin 9 on the Arduino
  7.  
  8.   The circuit:
  9.  
  10.   SparkFun Keypad                Arduino
  11.  
  12.   -----------------------------------------
  13.  
  14.     Wire 1 (NC) ------------- No connection
  15.  
  16.    Wire 2 (LEDR) ----------------- D3
  17.  
  18.     Wire 3 (GND) ------------------GND
  19.  
  20.    Wire 4 (P5.1) ----------------- D5
  21.  
  22.    Wire 5 (P5.3) ----------------- D6
  23.  
  24.    Wire 6 (P5.2) ----------------- D7
  25.  
  26. */
  27.  
  28. #include <Time.h>
  29.  
  30. int LEDpin = 3;
  31. int heatpin = 9;
  32. int p51 = 5;
  33. int p53 = 6;
  34. int p52 = 7;
  35.  
  36. int button = 0;
  37. int heatlevel = 200;
  38. int ledlevel = 200;
  39. int onstatus = 1;
  40. int heatstatus = 1;
  41. time_t t;
  42. int begin_s = 61;
  43. int end_s = 0;
  44. int prelevel = 200;
  45.  
  46. void setup()
  47. {
  48.  
  49.   // Initially set up the pins
  50.   pinMode(LEDpin, OUTPUT);
  51.   pinMode(heatpin, OUTPUT);
  52.   pinMode(p51, INPUT);
  53.   pinMode(p53, INPUT);
  54.   pinMode(p52, INPUT);
  55.   digitalWrite(p51, HIGH);
  56.   digitalWrite(p52, HIGH);
  57.   digitalWrite(p53, HIGH);
  58.   analogWrite(heatpin, heatlevel);
  59.   analogWrite(LEDpin, ledlevel);
  60.  
  61.  
  62.  
  63.   Serial.begin(9600);
  64. }
  65.  
  66. void loop()
  67. {
  68.  
  69.   button = getButtonState();  // Get button status
  70.  
  71.  // timeout LED  so we don't kill the battery
  72.   if(button)
  73.   {  if(begin_s == 61) {t = now(); begin_s = second(t); ledlevel = prelevel;}   }
  74.  
  75.   if(begin_s != 61)
  76.   {
  77.     t = now();
  78.     end_s = second(t);
  79.     if(end_s >= (begin_s + 20) || (end_s >= (begin_s - 10) && end_s < begin_s))
  80.     { delay(100);prelevel = ledlevel; ledlevel = 0; begin_s = 61; end_s = 0; }
  81.   }  
  82.  
  83. // Serial.println(begin_s);
  84. // Serial.println(end_s);
  85.  //Serial.println(heatlevel);
  86.   // On Off
  87.   if (button == 0x04)  
  88.   {
  89.     onstatus = onstatus ^ 1;  // flip on/off status of LED
  90.     delay(10);  // for debouncing try longer if you still have problems
  91.     while (getButtonState() == 0x04) { }
  92.     if(onstatus == 0){ heatstatus = 0;}  
  93.  
  94.   }
  95.   else if (button == 0x02)// Up  
  96.   {
  97.     ledlevel = ledlevel + 1;  // Change LED brightness
  98.     heatlevel = heatlevel + 1; // Change heat level
  99.     delay(10);
  100.   }
  101.   else if (button == 0x01)  // Down
  102.   {
  103.     ledlevel = ledlevel - 1;
  104.     heatlevel = heatlevel - 1;
  105.     delay(10);
  106.   }
  107.   else if(button == 0x08)   //Right
  108.   {
  109.       // tried to make different multiplication factors
  110.      // for each button but was acting weird
  111.     ledlevel = ledlevel + 1.25;
  112.     heatlevel = heatlevel + 1.25;
  113.     delay(10);
  114.   }
  115.   else if(button == 0x10) // Left
  116.   {
  117.     ledlevel = ledlevel - 1.25;
  118.     heatlevel = heatlevel - 1.25;
  119.     delay(10);
  120.   }
  121.  
  122.    // Keep LED and levels in range so we don't
  123.    // have crazy things happen
  124.   if(ledlevel >= 256 || heatlevel >= 256)
  125.   { ledlevel = 255; heatlevel = 255; }
  126.  
  127.  
  128.   if(ledlevel <= 0)
  129.   { ledlevel = 0;}
  130.  
  131.   if(heatlevel <= 0)
  132.   { heatlevel = 0;}
  133.  
  134.  
  135.  
  136.  
  137.  
  138.   if(onstatus == 1 )
  139.   {
  140.     analogWrite(LEDpin, ledlevel);
  141.     analogWrite(heatpin, heatlevel);
  142.   }
  143.   else if(onstatus == 0)
  144.   {
  145.     digitalWrite(LEDpin, 0);
  146.   }
  147.  
  148.   if(heatstatus == 0)
  149.   { digitalWrite(heatpin, 0);}
  150.  
  151. }
  152.  
  153. /* getButtonState() will return a uint8_t representing the status
  154.  
  155.   of the SparkFun button pad. The meaning of the return value is:
  156.  
  157.   0x01: Down
  158.  
  159.   0x02: Up
  160.  
  161.   0x04: Flame
  162.  
  163.   0x08: Right
  164.  
  165.   0x10: Left
  166.  
  167. */
  168.  
  169. uint8_t getButtonState()
  170.  
  171. {
  172.  
  173.   // Initially set all buttons as inputs, and pull them up
  174.   pinMode(p52, INPUT);
  175.   digitalWrite(p52, HIGH);
  176.   pinMode(p51, INPUT);
  177.   digitalWrite(p51, HIGH);
  178.   pinMode(p53, INPUT);
  179.   digitalWrite(p53, HIGH);
  180.  
  181.  
  182.   // Read the d/u/flame buttons
  183.  
  184.   if(!digitalRead(p53))
  185.   {  return 0x01; } // Down
  186.  
  187.   if(!digitalRead(p52))
  188.   { return 0x02;  }// Up
  189.  
  190.   if(!digitalRead(p51))
  191.   { return 0x04;  }// Flame
  192.  
  193.   // Read right button
  194.   pinMode(p52, OUTPUT);  // set p52 to output, set low
  195.   digitalWrite(p52, LOW);
  196.  
  197.   if(!digitalRead(p53))
  198.   {  return 0x08;  }// Right
  199.  
  200.   pinMode(p52, INPUT);  // set p52 back to input and pull-up
  201.   digitalWrite(p52, HIGH);
  202.  
  203.   // Read left button
  204.   pinMode(p51, OUTPUT);  // Set p51 to output and low
  205.   digitalWrite(p51, LOW);
  206.  
  207.   if(!digitalRead(p53))
  208.   {  return 0x10; } // Left
  209.  
  210.   pinMode(p51, INPUT);  // Set p51 back to input and pull-up
  211.   pinMode(p51, HIGH);
  212.  
  213.  
  214.  
  215.   return 0;
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement