|
| 1 | + |
| 2 | +#include <Adafruit_GFX.h> |
| 3 | +#include <Adafruit_ST7735.h> |
| 4 | +#include <SPI.h> |
| 5 | + |
| 6 | +volatile uint16_t measured_interval = 0; |
| 7 | +volatile uint8_t measured_interval_u = 0; |
| 8 | +volatile uint8_t measured_interval_ready = 0; |
| 9 | + |
| 10 | +volatile uint16_t interval_count = 0; // Maximum interval which can be measured is 8 sec. |
| 11 | +/***************************************************************************************/ |
| 12 | +/* USE PIN 2 TO TRIGGER EXTERNAL INTERRUPTS */ |
| 13 | +/***************************************************************************************/ |
| 14 | + |
| 15 | + |
| 16 | +volatile uint8_t input_value = 0; |
| 17 | + |
| 18 | +SIGNAL(INT0_vect) { |
| 19 | + input_value = digitalRead(2); |
| 20 | + if (input_value) { |
| 21 | + measured_interval_u = TCNT0; |
| 22 | + measured_interval = interval_count; |
| 23 | + measured_interval_ready = 1; |
| 24 | + } |
| 25 | + else { |
| 26 | + TCNT0 = 0; |
| 27 | + interval_count = 0; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +void init_external_interrupt_pin2() { |
| 32 | + pinMode(2, OUTPUT); |
| 33 | + digitalWrite(2, HIGH); |
| 34 | + // Global Enable INT0 interrupt |
| 35 | + EIMSK |= ( 1 << INT0); |
| 36 | + // Signal change triggers interrupt |
| 37 | + EICRA |= ( 1 << ISC00); |
| 38 | + EICRA |= ( 0 << ISC01); |
| 39 | +} |
| 40 | +/***************************************************************************************/ |
| 41 | + |
| 42 | +/***************************************************************************************/ |
| 43 | +/* TIMER TO COUNT MICROSECONDS */ |
| 44 | +/***************************************************************************************/ |
| 45 | + |
| 46 | +SIGNAL(TIMER2_OVF_vect) { // Overflow every 127.5 uS |
| 47 | + interval_count++; |
| 48 | +} |
| 49 | + |
| 50 | +void init_timer2() { |
| 51 | + // Run timer2 interrupt up counting at 2MHz |
| 52 | + TCCR2A = 0; |
| 53 | + TCCR2B = 0<<CS22 | 1<<CS21 | 0<<CS20; |
| 54 | + |
| 55 | + //Timer0 Overflow Interrupt Enable |
| 56 | + TIMSK2 |= 1<<TOIE0; |
| 57 | +} |
| 58 | +/***************************************************************************************/ |
| 59 | + |
| 60 | + |
| 61 | +// TFT display and SD card will share the hardware SPI interface. |
| 62 | +// Hardware SPI pins are specific to the Arduino board type and |
| 63 | +// cannot be remapped to alternate pins. For Arduino Uno, |
| 64 | +// Duemilanove, etc., pin 11 = MOSI, pin 12 = MISO, pin 13 = SCK. |
| 65 | +#define SD_CS 4 // Chip select line for SD card |
| 66 | +#define TFT_CS 10 // Chip select line for TFT display |
| 67 | +#define TFT_DC 8 // Data/command line for TFT |
| 68 | +#define TFT_RST -1 // Reset line for TFT (or connect to +5V) |
| 69 | + |
| 70 | +Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST); |
| 71 | + |
| 72 | +#define BUTTON_NONE 0 |
| 73 | +#define BUTTON_DOWN 1 |
| 74 | +#define BUTTON_RIGHT 2 |
| 75 | +#define BUTTON_SELECT 3 |
| 76 | +#define BUTTON_UP 4 |
| 77 | +#define BUTTON_LEFT 5 |
| 78 | + |
| 79 | +void setup(void) { |
| 80 | + Serial.begin(115200); |
| 81 | + init_timer2(); |
| 82 | + init_external_interrupt_pin2(); |
| 83 | + // Initialize 1.8" TFT |
| 84 | + tft.initR(INITR_BLACKTAB); // initialize a ST7735S chip, black tab |
| 85 | + tft.fillScreen(ST7735_BLACK); |
| 86 | + sei(); |
| 87 | + measured_interval_ready = 0; |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | +uint8_t readButton(void) { |
| 92 | + float a = analogRead(3); |
| 93 | + |
| 94 | + a *= 5.0; |
| 95 | + a /= 1024.0; |
| 96 | + |
| 97 | + // Serial.print("Button read analog = "); |
| 98 | +// Serial.println(a); |
| 99 | + if (a < 0.2) return BUTTON_DOWN; |
| 100 | + if (a < 1.0) return BUTTON_RIGHT; |
| 101 | + if (a < 1.5) return BUTTON_SELECT; |
| 102 | + if (a < 2.0) return BUTTON_UP; |
| 103 | + if (a < 3.2) return BUTTON_LEFT; |
| 104 | + else return BUTTON_NONE; |
| 105 | +} |
| 106 | + |
| 107 | +uint8_t buttonhistory = 0; |
| 108 | + |
| 109 | +void loop() { |
| 110 | + uint8_t b = readButton(); |
| 111 | + tft.setTextSize(3); |
| 112 | + if (b == BUTTON_DOWN) { |
| 113 | + tft.setTextColor(ST7735_RED); |
| 114 | + tft.setCursor(0, 10); |
| 115 | + tft.print("Down "); |
| 116 | + buttonhistory |= 1; |
| 117 | + } |
| 118 | + if (b == BUTTON_LEFT) { |
| 119 | + tft.setTextColor(ST7735_YELLOW); |
| 120 | + tft.setCursor(0, 35); |
| 121 | + tft.print("Left "); |
| 122 | + buttonhistory |= 2; |
| 123 | + } |
| 124 | + if (b == BUTTON_UP) { |
| 125 | + tft.setTextColor(ST7735_GREEN); |
| 126 | + tft.setCursor(0, 60); |
| 127 | + tft.print("Up"); |
| 128 | + buttonhistory |= 4; |
| 129 | + } |
| 130 | + if (b == BUTTON_RIGHT) { |
| 131 | + tft.setTextColor(ST7735_BLUE); |
| 132 | + tft.setCursor(0, 85); |
| 133 | + tft.print("Right"); |
| 134 | + buttonhistory |= 8; |
| 135 | + } |
| 136 | + if ((b == BUTTON_SELECT) && (buttonhistory == 0xF)) { |
| 137 | + tft.setTextColor(ST7735_MAGENTA); |
| 138 | + tft.setCursor(0, 110); |
| 139 | + tft.print("SELECT"); |
| 140 | + buttonhistory |= 8; |
| 141 | + } |
| 142 | + delay(100); |
| 143 | + if (measured_interval_ready != 0) { |
| 144 | + tft.fillScreen(ST7735_BLACK); |
| 145 | + tft.setTextColor(ST7735_GREEN); |
| 146 | + tft.setCursor(0, 60); |
| 147 | + tft.setTextSize(2); |
| 148 | + |
| 149 | + |
| 150 | + Serial.print(measured_interval); Serial.print(" + "); Serial.print(measured_interval_u); |
| 151 | + |
| 152 | + measured_interval_ready = 0; |
| 153 | + |
| 154 | + long usec = measured_interval; |
| 155 | + usec *= 250; |
| 156 | + usec += measured_interval_u; |
| 157 | + usec /= 2; |
| 158 | + Serial.print(" -> "); Serial.print(usec); Serial.println(" us "); |
| 159 | + double msec = usec / 1000.0f; |
| 160 | + //Serial.print(msec); Serial.print("ms "); |
| 161 | + double shutter = 1000.0f / msec; |
| 162 | + uint16_t s = shutter; |
| 163 | + tft.setCursor(0, 0); |
| 164 | + Serial.print("Shutter = "); |
| 165 | + if (s > 6) { |
| 166 | + Serial.print("1/");Serial.print(s);Serial.print("s"); |
| 167 | + tft.print("1/"); tft.print(s); tft.print("s"); |
| 168 | + } |
| 169 | + else { |
| 170 | + Serial.print(msec/1000.0f);Serial.print("s"); |
| 171 | + tft.print(msec/1000.0f); tft.print("s"); |
| 172 | + } |
| 173 | + |
| 174 | + |
| 175 | + |
| 176 | + // Serial.print(" ==> 1/"); Serial.print(shutter); Serial.print("s"); |
| 177 | + Serial.println(); |
| 178 | + } |
| 179 | +} |
0 commit comments