After testing my sensor, and getting a clearer picture of how the four LEDs circuit works, I returned to the breadboard and rewired my circuit.
The diagram for the circuit came from user ‘shinibix2022‘ at Fritzing.org.
As did the code:
const int green = 13;
const int amber = 10;
const int blue = 12;
const int red = 11;
int buttonState = 0;
int brightness = 0;
int fadeAmount = 5;
void setup(){
Serial.begin(9600);
pinMode(green, OUTPUT);
pinMode(amber, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(red, OUTPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
}
void loop(){
if (digitalRead(6) == HIGH && digitalRead(7) == LOW) {
// Green LED on:
digitalWrite(green, HIGH);
digitalWrite(amber, LOW);
}
else {
digitalWrite(green, LOW);
}
if (digitalRead(7) == HIGH && digitalRead(6)==LOW) {
// turn LED on:
digitalWrite(blue, HIGH);
digitalWrite(amber, LOW);
}
else {
digitalWrite(blue,LOW);
}
if (digitalRead(6) && digitalRead(7) == HIGH){
digitalWrite ( red, HIGH);
digitalWrite(amber && blue && green, LOW);
analogWrite(amber, LOW);
}
else {
digitalWrite(red,LOW);
}
if (digitalRead(6) && digitalRead(7) == LOW){
digitalWrite(red && blue && green && amber, LOW);
}
analogWrite(amber,brightness);
brightness = brightness + fadeAmount;
if (brightness == 0 || brightness == 255){
fadeAmount = -fadeAmount;
}
Serial.print(“Photo 1: “);
Serial.print(digitalRead(6), DEC); // Display Out 1 value
Serial.print(“; “);
Serial.print(“Photo 2: “);
Serial.println(digitalRead(7), DEC); // Display Out 2 value
delay(100);
}
My goal was to also save this to my collection at Autodesk 123D Circuits, but unfortunately the components list didn’t include a six pin 4-Direction tilt sensor.