Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SimpleTimer.h>
- SimpleTimer timer;
- const int buttonPin = 2; // the button is connected to pin 2
- const int Led0 = 3; // first LED is connected to pin 3
- const int NumLeds = 4; // we have four LEDs (at the moment)
- const int Temp = A3;
- // struct for buttons
- enum b_p {P_NONE, P_SHORT, P_LONG, P_DOUBLE};
- // data type takes three values.
- typedef enum b_p b_press;
- //now we can just use b_press as a type.
- struct B_struct {
- int port; //the port on which this button operates
- int state = HIGH; //the state of the port (high = unpressed)
- unsigned long last_change = 0; //time of thelast press
- bool d_1;
- b_press response = P_NONE,P_SHORT, P_LONG, P_DOUBLE;
- };
- typedef struct B_struct Button;
- Button * buttonReg[20] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
- NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
- void pciSetup(byte pin)
- {
- *digitalPinToPCMSK(pin) |= bit (digitalPinToPCMSKbit(pin)); // enable pin
- PCIFR |= bit (digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
- PCICR |= bit (digitalPinToPCICRbit(pin)); // enable interrupt for the group
- }
- // ISR sets up an interrupt service routine for the specified interrupt vector
- ISR (PCINT0_vect) // handle pin change interrupt for pins 8 to 13
- {
- // here we will loop through the required part of buttonReg
- // if a button is registered (non-null) we call its poll() method
- // that checks if button has actually changed
- for (int i=8; i<=13; i++) {
- if (buttonReg[i] != NULL) poll(buttonReg[i]);
- }
- }
- ISR (PCINT1_vect) // handle pin change interrupt for pins 8 to 13
- {
- // here we will loop through the required part of buttonReg
- // if a button is registered (non-null) we call its poll() method
- // that checks if button has actually changed
- for (int i=14; i<=19; i++) {
- if (buttonReg[i] != NULL) poll(buttonReg[i]);
- }
- }
- ISR (PCINT2_vect) // handle pin change interrupt for pins 8 to 13
- {
- // here we will loop through the required part of buttonReg
- // if a button is registered (non-null) we call its poll() method
- // that checks if button has actually changed
- for (int i=0; i<=7; i++) {
- if (buttonReg[i] != NULL) poll(buttonReg[i]);
- }
- }
- void buttonInit(Button * B, int p) { //initializes a Button working on pin p
- B->port = p;
- pinMode(p, INPUT);
- buttonReg[p] = B;
- pciSetup(p);
- }
- // check the button, to be called each time round the loop
- void poll(Button * B) {
- //Serial.println("poll"); // Debuffing code
- if (millis()-B->last_change<10) {
- return;
- }
- int b = digitalRead(B->port);
- if (b!= B->state) {
- if (b==HIGH) {
- unsigned long p_time = millis()-B->last_change;
- if (p_time<150) {
- if (B->d_1) { //had one press, this is the second
- B->response = P_DOUBLE;
- B->d_1 = false;
- } else {
- B->d_1 = true;
- int timeout = timer.setTimeout(160, buttonTimeout);
- }
- } else { // longer than 150ms
- if (p_time<250) B->response = P_SHORT;
- else B->response = P_LONG;
- }
- B->state = HIGH;
- B->last_change = millis(); //to allow timing of the gap
- } else { //b==LOW
- B->last_change = millis();
- B->state = LOW;
- }
- } else { // no change but check for timeout in middle of double
- if (B->d_1) { // button up looking for double
- unsigned long p_time = millis()-B->last_change;
- if (p_time>150) { // too long, cancel double press
- B->d_1 =false; // no longer looking for double press
- B->response = P_SHORT; // declare first press a short press
- }
- }
- }
- }
- // called to find state of button. Post: response = P_NONE
- b_press ask(Button * B) {
- b_press bp = B->response;
- B->response = P_NONE;
- return bp;
- }
- // count button presses and return the number
- int buttonState = HIGH; // assume button starts off unpressed
- int buttonCount = 0; // start counting at zero
- int countButton() {
- int bs = digitalRead(buttonPin);
- if ((bs == LOW) && (bs != buttonState)) {
- buttonState = bs;
- buttonCount++;
- } else if ((bs == HIGH) && (bs != buttonState)) buttonState = bs;
- return buttonCount;
- }
- void buttonTimeout() {
- for (int a=0; a < 20; a++) {
- if (buttonReg[a]->d_1) poll(buttonReg[a]);
- }
- }
- // control LEDs
- void controlLEDs(int value) {
- //int bitCount = 1;
- // Program a loop, where:
- // the loop counter (call it i) runs from Led0 to Led0+Numleds-1
- // each time through the loop digitalWrite(i,value) is called
- for (int i=Led0; i<Led0+NumLeds; i++) {
- if (i==Led0 + value) {
- digitalWrite(i,HIGH); }
- else digitalWrite(i,LOW);
- }
- }
- // Hint: you already have a similar loop in the program.
- Button B;
- int LEDs[4];
- void setup() {
- pinMode(LED_BUILTIN, OUTPUT);
- // set up the button as an input
- buttonInit(&B,buttonPin);
- for (int i=Led0; i<Led0+NumLeds; i++) {
- pinMode(i,OUTPUT);
- LEDs[i-Led0] = LOW;
- Serial.begin(9600);
- Serial.println("serial initialised");
- }
- analogReference(INTERNAL);
- }
- void valuetest (bool v) {
- if (v) Serial.println("Recieving value");
- else Serial.println("Not recieving value");
- }
- float cel(int input) {
- return ((float)input)*110.0/1023.0;
- }
- bool valuesend = false;
- float tempval = 0;
- void loop() {
- int ctrlVal = analogRead(A0);
- b_press bp = ask(&B);
- switch (bp) {
- case P_DOUBLE:
- valuesend = false;
- valuetest(valuesend);
- //if (LEDs[2] == LOW) {
- //LEDs[2] = HIGH;
- //digitalWrite(Led0+2,HIGH);
- //} else {
- //LEDs[2] = LOW;
- // digitalWrite(Led0+2,LOW);
- // }
- // break;
- case P_LONG:
- valuesend = true;
- valuetest(valuesend);
- //if (LEDs[1] == LOW) {
- //LEDs[1] = HIGH;
- //digitalWrite(Led0+1,HIGH);
- //} else {
- //LEDs[1] = LOW;
- //digitalWrite(Led0+1,LOW);
- //}
- break;
- case P_SHORT:
- valuesend = !valuesend;
- valuetest(valuesend);
- //if (LEDs[0] == LOW) {
- //LEDs[0] = HIGH;
- //digitalWrite(Led0,HIGH);
- //} else {
- //LEDs[0] = LOW;
- //digitalWrite(Led0,LOW);
- //}
- break;
- default: ;
- //int Temp = analogRead(A2);
- //Serial.println(Temp);
- tempval = cel(analogRead(A1));
- if (valuesend) {
- Serial.println("Control = ");
- Serial.println(cel(ctrlVal));
- Serial.println("Temperture = ");
- Serial.println(tempval);
- if (tempval < ctrlVal - 0.5) {
- controlLEDs(0);
- } else if (tempval > ctrlVal + 0.5) {
- controlLEDs(2);
- } else {
- controlLEDs(1);
- }
- }
- }
- delay(100);
- }
- // wait for 1/20 second before looping
- //timer.run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement