I have tried to modify the LoveOMeter code to do a comparison of 2 users and then give the compatibility - as is suggested in the extra exercise.
I added 2 switches (to select the user, also a reset) and 2 blue LEDs to indicate whether it is user 1 or 2.
I have done some Java before but I have not used multiple methods in the code as I am trying to follow as a beginner; instead just leaving the 2 setup/loop methods and hoping the program will run sequentially..although this meant I had to repeat a lot of code.
Can someone please suggest where I may be going wrong or should I just scrap it as I've made it too complicated etc?
I should mention that it does not work on the board (noting happens at all) although does compile with a few warnings regarding 'statements with no effects' and' functions can be used uninitialised'...the breadboard looks a mess! lol
// Love-O-Meter2 Compatibility
const int sensorPin = A0; // which pin (analog) temperature sensor is connected to
const float baselineTemp = 20; // the baseline temp
int switchState1 = 0; // switch 1 set to 0
int switchState2 = 0; // switch 2 set to 0 (reset)
void setup() {
Serial.begin(9600); // open a serial port with baud rate 9600
// setting traffic light LED pins as outputs
for (int pinNumber = 2; pinNumber<5; pinNumber++) {
pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, LOW);
}
// declare turn LED (blue) as outputs
pinMode(9, OUTPUT); // 1st
pinMode(8, OUTPUT); // 2nd
// declare switch1 pin as input
pinMode(6, INPUT); // btm (reset)
// declare switch2 (reset) pin as input
pinMode(7, INPUT); // top (user selection)
}
void loop() {
int sensorVal = analogRead(sensorPin); // get value from sensor
// variables to store detected levels for users
int compLevel1;
int compLevel2;
// counter for number of switch1 presses
int buttonPushCounter = 0;
// checks volt state of selected pin and stores value
// pin 9 is connected to switch1, pin 8 connected switch2
switchState1 = digitalRead(9); // turn
switchState2 = digitalRead(8); // reset
// default state of blue turn LEDS (off)
digitalWrite(9, LOW); // blue 1 off
digitalWrite(8, LOW); // blue 1 off
// turn switch pressed to user 1
if (switchState1 == HIGH) {
buttonPushCounter +1;
delay(150);
digitalWrite(9, HIGH); // blue 1 on
digitalWrite(8, LOW); // blue 2 off
Serial.print("Sensor Value: ");
Serial.print(sensorVal); // output sensor value
// convert the ADC reading to voltage
float voltage = (sensorVal/1024.0) * 5.0;
Serial.print(", Volts: ");
Serial.print(voltage);
Serial.print(", Degrees C: ");
// convert the voltage to temperature, then from degrees F to C
float temperature = ((((voltage - 0.5) * 100) - 32) * 5) /9;
Serial.println(temperature);
// Serial.println("----------------");
// turning on 1 LED for every 2c temperature rise above baseline
if(temperature < baselineTemp + 2) {
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
compLevel1 = 0;
}
// if temp 2c > baseline AND 4c above baseline
else if(temperature >= baselineTemp + 2
&& temperature < baselineTemp + 4) {
digitalWrite(2, HIGH); // red on
digitalWrite(3, LOW);
digitalWrite(4, LOW);
compLevel1 = 1;
}
// if temp between 2 - 4c above baseline
else if (temperature >= baselineTemp + 4
&& temperature < baselineTemp + 6) {
digitalWrite(2, HIGH); // red on
digitalWrite(3, HIGH); // yellow on
digitalWrite(4, LOW);
compLevel1 = 2;
}
// if temp if 6c over baseline
else if (temperature >= baselineTemp + 6) {
digitalWrite(2, HIGH); // red on
digitalWrite(3, HIGH); // yellow on
digitalWrite(4, HIGH); // green on
compLevel1 = 3;
}
// delay introduced as ADC can only read so fast
delay(100);
}
// turn switch pressed to user 2 (2 blue lights on)
if (switchState2 == HIGH) {
buttonPushCounter +1;
delay(150);
digitalWrite(9, HIGH); // blue 1 on
digitalWrite(8, HIGH); // blue 2 on
Serial.print("Sensor Value: ");
Serial.print(sensorVal); // output sensor value
// convert the ADC reading to voltage
float voltage = (sensorVal/1024.0) * 5.0;
Serial.print(", Volts: ");
Serial.print(voltage);
Serial.print(", Degrees C: ");
// convert the voltage to temperature, then from degrees F to C
float temperature = ((((voltage - 0.5) * 100) - 32) * 5) /9;
Serial.println(temperature);
// Serial.println("----------------");
// turning on 1 LED for every 2c temperature rise above baseline
if(temperature < baselineTemp + 2) {
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
compLevel2 = 0;
}
// if temp 2c > baseline AND 4c above baseline
else if(temperature >= baselineTemp + 2
&& temperature < baselineTemp + 4) {
digitalWrite(2, HIGH); // red on
digitalWrite(3, LOW);
digitalWrite(4, LOW);
compLevel2 = 1;
}
// if temp between 2 - 4c above baseline
else if (temperature >= baselineTemp + 4
&& temperature < baselineTemp + 6) {
digitalWrite(2, HIGH); // red on
digitalWrite(3, HIGH); // yellow on
digitalWrite(4, LOW);
compLevel2 = 2;
}
// if temp if 6c over baseline
else if (temperature >= baselineTemp + 6) {
digitalWrite(2, HIGH); // red on
digitalWrite(3, HIGH); // yellow on
digitalWrite(4, HIGH); // green on
compLevel2 = 3;
}
// delay introduced as ADC can only read so fast
delay(100);
}
// indicating total compatibility level of 2 users
if (switchState1 == HIGH) {
buttonPushCounter +1;
Serial.println(buttonPushCounter);
if (compLevel1 + compLevel2 <= 2) {
digitalWrite(2, HIGH); // red on
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}
else if (compLevel1 + compLevel2 >= 3 && compLevel1 + compLevel2 <5) {
digitalWrite(2, HIGH); // red on
digitalWrite(3, HIGH); // yellow on
digitalWrite(4, LOW);
}
else if (compLevel1 + compLevel2 >= 5) {
digitalWrite(2, HIGH); // red on
digitalWrite(3, HIGH); // yellow on
digitalWrite(4, HIGH); // green on
}
// reset through switch 2
if (switchState2 == HIGH) {
// all LEDs to off state
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(9, LOW);
digitalWrite(8, LOW);
// all level values to 0
compLevel1 = 0;
compLevel2 = 0;
buttonPushCounter = 0;
}
}
}