Go Down

Topic: LoveOMeter Compatibility Modification (Read 1 time) previous topic - next topic

BootyBump

Sep 20, 2015, 03:18 am Last Edit: Sep 20, 2015, 03:24 am by BootyBump
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

Code: [Select]
// 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;
 
  }
 
 }
}

rp14

Your Current code looks like this

Code: [Select]
// Global Variables declaration

void setup() {  //Setup code  }

void loop() {
  

  int sensorVal = analogRead(sensorPin);   // get value from sensor

  // Declare variables to store detected levels for users and counter for number of switch1 presses
  // Read switch1 and switch2 and store status in variables - switchState1, switchState2


  if (switchState1 == HIGH) {
   // code for user1 input
  }

  if (switchState2 == HIGH)  {
   // code for user2 input
  }

  if (switchState1 == HIGH) {
    // display total compatibility level of 2 users
  }

  if (switchState2 == HIGH)  {
    //reset all variables
  }
}



pinMode(sensorPin, INPUT); is missing !!!

You are not using "buttonPushCounter" variable at all in "If" conditions !

Also in the code given - a "}" is missing @ Line 200  (which is required for completing if conditional statement on line 181).instead there is extra "}" at the end.
This is creating a big problem!!

In current code undesired If statement block may execute after pressing a button since "buttonPushCounter" variable is not being used in If condition.


rp14

I tried to write a code for this. It's structure is like this

Code: [Select]


/*-----------------------------------------------------------------------
|   press switch2 only for reset
|   press switch1 for reading temperature of a first user
|   then press switch1 again for reading temperature of a second user!
*/---------------------------------------------------------------------- -

//Pin Definations

#define switchPin1    4
#define switchPin2    5
#define RESET         0
#define USER1         1
#define USER2         2
#define COMPATIBILITY 3

int flag;
char state;  // state indicator

void setup() {
  //Initialize variables and state
  state = USER1;
  flag = 0;
  // Initialize pins as inputs/outputs for switches and LEDs
  //write code for :turn off all LEDs
}

void loop() {
  //Read Swicth2, wait and again read switch2 for confirmed reset
  if (digitalRead(switchPin2) == HIGH) {
    delay(100);
    if (digitalRead(switchPin2) == HIGH) {
      state = RESET;
    }
  }

  switch (state) {

    case USER1:
      // Execute if sw1 is pressed and system is ready to take input from user 1
      if (digitalRead(switch1Pin) == HIGH && flag == 0) {
        //write code for : display user1 on LED1
        delay(1000);
        //write code for : scan user1 temp
        //write code for : assign compatibility level to user1
        //write code for : display user1 temp on 3 LEDs
        flag = 1; // flag =1 means input from user1 is taken
        state = USER2; // state = USER2 means system is ready to take input from user 2
        delay(100);
      }
      break;

    case USER2:
      // Execute if sw1 is pressed again and system is ready to take input from user 2
      if (digitalRead(switch1Pin) == HIGH && flag == 1) {
        //write code for : display user2 on LED2
        delay(1000);
        //write code for : scan user2 temp and assign compatibility level
        //write code for : display user2 temp on 3 LEDs
        flag = 2;// flag =1 means input from user2 is taken
        state = COMPATIBILITY; // state = USER2 means system is ready to display compatibility
        delay(100);
      }
      break;

    case COMPATIBILITY:
      // Execute if sw1 is pressed again and system is ready to display compatibility
      if (digitalRead(switch1Pin) == HIGH && flag == 2) {
        //write code for : calculate compatibility
        //write code for : display compatibility
        flag == 3; // flag =3 means compatibility is displayed
        delay(100);
      }
      break;

    case RESET:
      //write code for : flash leds for 2-3 seconds
      //reset variables and state
      state = USER1;
      flag = 0;
      delay(500);
      // turn off all LEDs

      break;
  }
}



rp14

Then completed it and tested it with a potentiometer instead of temperature sensor and it worked! :)
you will need to modify it for temperature sensor if you wish to use this code.

Code: [Select]
/*------------------------------------------------------------------------------------------
|   Love-o-meter compatibility
|          
|   Instructions -
|   press switch2 only for reset
|   (All LEDs will blink fast after pressing switch2 indicating reset)
|   press switch1 for reading temperature of first user
|   press switch1 again for reading temperature of second user
|   then press switch1 again to display compatibility of users
|   NOTE -Potentiometer is used in this code instead of temperature sensore for testing
------------------------------------------------------------------------------------------*/

//Pin Definations

#define RESET         0
#define USER1         1
#define USER2         2
#define COMPATIBILITY 3
#define LED1 5   //mode indicator
#define LED2 10  //mode indicator
#define LED3 A2  //Temperature /compatibility indicator
#define LED4 A3  //Temperature /compatibility indicator
#define LED5 A4  //Temperature /compatibility indicator
#define SensorPin A5   // using Potentiometer instead of temperature sensore for testing
#define switch1Pin A0  // switch1 connected to this pin
#define switch2Pin A1  // switch2 connected to this pin

int flag;
char state;  // state indicator
int compLevel1;
int compLevel2;

void setup() {
  // Serial.begin(9600);
  //Initialize variables and state
  state = USER1; // state = USER2 means system is ready to take input from user 1
  flag = 0; // flag =0 means no input is taken
  pinMode(LED1, OUTPUT);     // Initialize pins as outputs for leds and turn off all LEDs
  digitalWrite(LED1, LOW);
  pinMode(LED2, OUTPUT);
  digitalWrite(LED2, LOW);
  pinMode(LED3, OUTPUT);
  digitalWrite(LED3, LOW);
  pinMode(LED4, OUTPUT);
  digitalWrite(LED4, LOW);
  pinMode(LED5, OUTPUT);
  digitalWrite(LED5, LOW);

  pinMode(switch1Pin, INPUT);// Initialize pins as inputs for switches
  pinMode(switch2Pin, INPUT);
  pinMode(SensorPin, INPUT);// Initialize pin as input for sensor
  // Serial.print("start");
}

void loop() {
  //Read Swicth2, wait and again read switch2 for confirmed reset
  if (digitalRead(switch2Pin) == HIGH) {
    delay(100);
    if (digitalRead(switch2Pin) == HIGH) {
      state = RESET;
      //Serial.println("RESET");
    }
  }

  switch (state) {

    case USER1:  // code for user1
      // Execute if sw1 is pressed and system is ready to to take input from user 1
      if (digitalRead(switch1Pin) == HIGH && flag == 0)
      {
        digitalWrite(LED1, HIGH); // user 1 mode
        digitalWrite(LED2, LOW); // user 1 mode
        delay(1000);
        int sensorValue = analogRead(SensorPin);    // get value from sensor
        //Serial.println("temp-");
        // Serial.println(sensorValue);

        //Display temperature using LEDs
        if (sensorValue < 100)  {
          digitalWrite(LED3, LOW);
          digitalWrite(LED4, LOW);
          digitalWrite(LED5, LOW);
          compLevel1 = 0;
        }

        else if (sensorValue >= 100 && sensorValue < 200)  {
          digitalWrite(LED3, HIGH);
          digitalWrite(LED4, LOW);
          digitalWrite(LED5, LOW);
          compLevel1 = 1;
        }

        else if (sensorValue >= 200 && sensorValue < 300)  {
          digitalWrite(LED3, HIGH);
          digitalWrite(LED4, HIGH);
          digitalWrite(LED5, LOW);
          compLevel1 = 2;
        }

        else if (sensorValue >= 300) {
          digitalWrite(LED3, HIGH);
          digitalWrite(LED4, HIGH);  
          digitalWrite(LED5, HIGH);
          compLevel1 = 3;
        }
        flag = 1; // flag =1 means input from user1 is taken
        state = USER2; // state = USER2 means system is ready to take input from user 2
        delay(100);
      }
      break;

    case USER2:  // code for user2
      // Execute if sw1 is pressed again and system is ready to to take input from user 2
      if (digitalRead(switch1Pin) == HIGH && flag == 1)
      {
        digitalWrite(LED1, LOW); // user 2 mode
        digitalWrite(LED2, HIGH); // user 2 mode
        delay(1000);
        int sensorValue = analogRead(SensorPin);    // get value from sensor
        // Serial.println("temp-");
        // Serial.println(sensorValue);
        //Display Temperature using LEDs
        if (sensorValue < 100)  {
          digitalWrite(LED3, LOW);
          digitalWrite(LED4, LOW);
          digitalWrite(LED5, LOW);
          compLevel1 = 0;
        }

        else if (sensorValue >= 100 && sensorValue < 200)  {
          digitalWrite(LED3, HIGH);  
          digitalWrite(LED4, LOW);
          digitalWrite(LED5, LOW);
          compLevel1 = 1;
        }

        else if (sensorValue >= 200 && sensorValue < 300)  {
          digitalWrite(LED3, HIGH);  
          digitalWrite(LED4, HIGH);  
          digitalWrite(LED5, LOW);
          compLevel1 = 2;
        }

        else if (sensorValue >= 300) {
          digitalWrite(LED3, HIGH);  
          digitalWrite(LED4, HIGH);  
          digitalWrite(LED5, HIGH);  
          compLevel1 = 3;
        }
        flag = 2;// flag =1 means input from user2 is taken
        state = COMPATIBILITY; // state = USER2 means system is ready to display compatibility
        delay(1000);
      }
      break;

    case COMPATIBILITY:
      // Execute if sw1 is pressed again and system is ready to display compatibility
      if (digitalRead(switch1Pin) == HIGH && flag == 2)
      {
        digitalWrite(LED1, HIGH); // Compatibility mode
        digitalWrite(LED2, HIGH); // compatibility mode
        Serial.println("comp-");
        Serial.println(compLevel1 + compLevel2);
        if (compLevel1 + compLevel2 <= 2)  {
          digitalWrite(LED3, HIGH);
          digitalWrite(LED4, LOW);
          digitalWrite(LED5, LOW);
        }
        else if (compLevel1 + compLevel2 >= 3 && compLevel1 + compLevel2 < 5) {
          digitalWrite(LED3, HIGH);
          digitalWrite(LED4, HIGH);
          digitalWrite(LED5, LOW);
        }
        else if (compLevel1 + compLevel2 >= 5) {
          digitalWrite(LED3, HIGH);
          digitalWrite(LED4, HIGH);
          digitalWrite(LED5, HIGH);
        }
        flag == 3; // flag =3 means compatibility is displayed
        delay(1000);
      }
      break;

    case RESET:
      //reset variables and state
      state = USER1;
      flag = 0;
      bool value = HIGH;
      while (flag < 10)
      {
        digitalWrite(LED1, value);
        digitalWrite(LED2, value);
        digitalWrite(LED3, value);
        digitalWrite(LED4, value);
        digitalWrite(LED5, value);
        delay(50);
        value = !value;
        flag++;
      }
      flag = 0;
      // Serial.println("RESET");
      delay(500);
      // turn off all LEDs
      break;
  }
}


In this code
controller continuously scans for Switch2 which is exclusive for reset function,
Switch1 is provided for switching states which defines which input is to be taken (User1, User2 or none)!

Hope this helps!  ;D  

Go Up