Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Right and up turn up the temperature on the Heat pads
- Down and left turn it down
- Connect the MOSFET to pin 9 on the Arduino
- The circuit:
- SparkFun Keypad Arduino
- -----------------------------------------
- Wire 1 (NC) ------------- No connection
- Wire 2 (LEDR) ----------------- D3
- Wire 3 (GND) ------------------GND
- Wire 4 (P5.1) ----------------- D5
- Wire 5 (P5.3) ----------------- D6
- Wire 6 (P5.2) ----------------- D7
- */
- #include <Time.h>
- int LEDpin = 3;
- int heatpin = 9;
- int p51 = 5;
- int p53 = 6;
- int p52 = 7;
- int button = 0;
- int heatlevel = 200;
- int ledlevel = 200;
- int onstatus = 1;
- int heatstatus = 1;
- time_t t;
- int begin_s = 61;
- int end_s = 0;
- int prelevel = 200;
- void setup()
- {
- // Initially set up the pins
- pinMode(LEDpin, OUTPUT);
- pinMode(heatpin, OUTPUT);
- pinMode(p51, INPUT);
- pinMode(p53, INPUT);
- pinMode(p52, INPUT);
- digitalWrite(p51, HIGH);
- digitalWrite(p52, HIGH);
- digitalWrite(p53, HIGH);
- analogWrite(heatpin, heatlevel);
- analogWrite(LEDpin, ledlevel);
- Serial.begin(9600);
- }
- void loop()
- {
- button = getButtonState(); // Get button status
- // timeout LED so we don't kill the battery
- if(button)
- { if(begin_s == 61) {t = now(); begin_s = second(t); ledlevel = prelevel;} }
- if(begin_s != 61)
- {
- t = now();
- end_s = second(t);
- if(end_s >= (begin_s + 20) || (end_s >= (begin_s - 10) && end_s < begin_s))
- { delay(100);prelevel = ledlevel; ledlevel = 0; begin_s = 61; end_s = 0; }
- }
- // Serial.println(begin_s);
- // Serial.println(end_s);
- //Serial.println(heatlevel);
- // On Off
- if (button == 0x04)
- {
- onstatus = onstatus ^ 1; // flip on/off status of LED
- delay(10); // for debouncing try longer if you still have problems
- while (getButtonState() == 0x04) { }
- if(onstatus == 0){ heatstatus = 0;}
- }
- else if (button == 0x02)// Up
- {
- ledlevel = ledlevel + 1; // Change LED brightness
- heatlevel = heatlevel + 1; // Change heat level
- delay(10);
- }
- else if (button == 0x01) // Down
- {
- ledlevel = ledlevel - 1;
- heatlevel = heatlevel - 1;
- delay(10);
- }
- else if(button == 0x08) //Right
- {
- // tried to make different multiplication factors
- // for each button but was acting weird
- ledlevel = ledlevel + 1.25;
- heatlevel = heatlevel + 1.25;
- delay(10);
- }
- else if(button == 0x10) // Left
- {
- ledlevel = ledlevel - 1.25;
- heatlevel = heatlevel - 1.25;
- delay(10);
- }
- // Keep LED and levels in range so we don't
- // have crazy things happen
- if(ledlevel >= 256 || heatlevel >= 256)
- { ledlevel = 255; heatlevel = 255; }
- if(ledlevel <= 0)
- { ledlevel = 0;}
- if(heatlevel <= 0)
- { heatlevel = 0;}
- if(onstatus == 1 )
- {
- analogWrite(LEDpin, ledlevel);
- analogWrite(heatpin, heatlevel);
- }
- else if(onstatus == 0)
- {
- digitalWrite(LEDpin, 0);
- }
- if(heatstatus == 0)
- { digitalWrite(heatpin, 0);}
- }
- /* getButtonState() will return a uint8_t representing the status
- of the SparkFun button pad. The meaning of the return value is:
- 0x01: Down
- 0x02: Up
- 0x04: Flame
- 0x08: Right
- 0x10: Left
- */
- uint8_t getButtonState()
- {
- // Initially set all buttons as inputs, and pull them up
- pinMode(p52, INPUT);
- digitalWrite(p52, HIGH);
- pinMode(p51, INPUT);
- digitalWrite(p51, HIGH);
- pinMode(p53, INPUT);
- digitalWrite(p53, HIGH);
- // Read the d/u/flame buttons
- if(!digitalRead(p53))
- { return 0x01; } // Down
- if(!digitalRead(p52))
- { return 0x02; }// Up
- if(!digitalRead(p51))
- { return 0x04; }// Flame
- // Read right button
- pinMode(p52, OUTPUT); // set p52 to output, set low
- digitalWrite(p52, LOW);
- if(!digitalRead(p53))
- { return 0x08; }// Right
- pinMode(p52, INPUT); // set p52 back to input and pull-up
- digitalWrite(p52, HIGH);
- // Read left button
- pinMode(p51, OUTPUT); // Set p51 to output and low
- digitalWrite(p51, LOW);
- if(!digitalRead(p53))
- { return 0x10; } // Left
- pinMode(p51, INPUT); // Set p51 back to input and pull-up
- pinMode(p51, HIGH);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement