-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask1.3C
More file actions
55 lines (49 loc) · 1.48 KB
/
Task1.3C
File metadata and controls
55 lines (49 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// In order to convert an analog input to an LOW/HIGH value and utilise
// interrupts I have set an additional Arduino board to perform the conversion.
// The second Arduino board simply takes the value from the photoresistor and
// changes it to a HIGH state value if over 100 and LOW state if under
// Set labels for pins
const uint8_t SEN_PIN1 = 2;
const uint8_t SEN_PIN2 = 3;
const uint8_t LED_PIN1 = 12;
const uint8_t LED_PIN2 = 13;
// Set variable to record input state
volatile uint8_t sensor1State = LOW;
volatile uint8_t sensor2State = LOW;
void setup()
{
// Set pins as input or output
pinMode(SEN_PIN1, INPUT);
pinMode(SEN_PIN2, INPUT);
pinMode(LED_PIN1, OUTPUT);
pinMode(LED_PIN2, OUTPUT);
// Set an interrupt to occur when a change is detected
attachInterrupt(digitalPinToInterrupt(SEN_PIN1), pin1_ISR, CHANGE);
attachInterrupt(digitalPinToInterrupt(SEN_PIN2), pin2_ISR, CHANGE);
// Begin recording serial
Serial.begin(9600);
}
void loop()
{
// Display variable in serial monitor
Serial.print("Sensor 1 state: ");
Serial.println(sensor1State);
Serial.print("Sensor 2 state: ");
Serial.println(sensor2State);
Serial.println();
delay(10);
}
void pin1_ISR()
{
// Set input value to variable
sensor1State = digitalRead(SEN_PIN1);
// Change LED based on state
digitalWrite(LED_PIN2, sensor1State);
}
void pin2_ISR()
{
// Set input value to variable
sensor2State = digitalRead(SEN_PIN2);
// Change LED based on state
digitalWrite(LED_PIN1, sensor2State);
}