Go Down

Topic: Switch toggle -> LED -> LCD Message... Must be simple right...? T_T (Read 379 times) previous topic - next topic

ClaraL

Hi all,

I've been struggling with this for the past 4 hours and thought I'd come here begging for help...

So I'm basically trying to set up two different sets of messages based on the moisture level reading ("avg") which are to be toggled back and forth by a momentary switch. My method right now is to have the switches toggle an LED which in turn toggles a set of messages... This is proving to be incredibly frustrating as nothing I've been trying is working.

Do you guys have any ideas for an easier way to do this? I know I'm not doing this the right way... Thank you so much ahead of time to anyone who lends an eye!    :*

Code: [Select]


#define voltageFlipPin1 2
#define voltageFlipPin2 4
#define sensorPin 0

#include <SoftwareSerial.h>            // we need to include this library !

#define rxPin 30  // rxPin is immaterial - not used - just make this an unused Arduino pin number
#define txPin A2 // txPin is the one needed for the LCD - we'll use Digital Pin A0/14 to send serial data to the LCD
// mySerial is connected to the TX pin so mySerial.print commands are used
// one could just as well use the software mySerial library to communicate on another pin
// we're using SoftwareSerial so that we can save pins 0&1 for the BUB

SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);    // let SoftwareSerial know what pins we'll use

int flipTimer = 500;

const int SWITCH = 6; //pin 2 for switch input
const int LED = 12; //pin 13 for led output

//create variables
boolean toggle;
int switchState;

void setup(){

  //false means the led is off (boolean are either 0 or 1)
  toggle = false;

  //set pin modes
  pinMode(SWITCH, INPUT);
  pinMode(LED, OUTPUT);
 
 
    Serial.begin(9600);
    pinMode(voltageFlipPin1, OUTPUT);
    pinMode(voltageFlipPin2, OUTPUT);
    pinMode(sensorPin, INPUT);
       
    pinMode(txPin, OUTPUT);
    mySerial.begin(9600);      // 9600 baud is chip communications speed
    delay(80);                //                 pause to allow LCD EEPROM to
    mySerial.print("?G216");   // set to LCD display geometry,  2 x 16 characters in this case
    delay(80);                //                 pause to allow LCD EEPROM to program
    mySerial.print("?Bff");    // set backlight to ff hex, maximum brightness (you can change this!)
    delay(80);                // pause to allow LCD EEPROM to program
}



void setSensorPolarity(boolean flip){
  if(flip){
    digitalWrite(voltageFlipPin1, HIGH);
    digitalWrite(voltageFlipPin2, LOW);
  }else{
    digitalWrite(voltageFlipPin1, LOW);
    digitalWrite(voltageFlipPin2, HIGH);
  }
}




void loop(){


    //
    setSensorPolarity(true);
    delay(flipTimer);
    int val1 = analogRead(sensorPin);
    delay(flipTimer); 
    setSensorPolarity(false);
    delay(flipTimer);
    // invert the reading
    int val2 = 1023 - analogRead(sensorPin);
    //
    reportLevels(val1,val2);
   
   
   
}



void reportLevels(int val1,int val2){
 
    int avg = (val1 + val2) / 2;
 
    String msg = "avg: ";
    msg += avg;
    Serial.println(msg);
   
    switchState = digitalRead(SWITCH); //check switch

    while(switchState == HIGH){
      switchState = digitalRead(SWITCH);//double check
        if(switchState == LOW){
          toggle = !toggle;
          digitalWrite(LED, toggle);
          break; //exit
        }
    }
   
   
   
   mySerial.print("?f");                 
   delay(30);
     
     
   while ( switchState == HIGH ){
   if ( avg <= 50 ){
        mySerial.print("AAAAA");             
        delay(5000);                             
        mySerial.print("?f");                 
        delay(30);
    }   
   
    else if ( avg <= 150 && avg > 50 ){
        mySerial.print("BBBBB");               
        delay(5000);                           
        mySerial.print("?f");                 
        delay(30);
    }
   
    else if ( avg <= 350 && avg > 150 ){
        mySerial.print("CCCCC");               
        delay(5000);                               
        mySerial.print("?f");                 
        delay(30);
    }
   }
   
   
      while ( switchState == LOW ){
   if ( avg <= 50 ){
        mySerial.print("11111");             
        delay(5000);                             
        mySerial.print("?f");                 
        delay(30);
    }   
   
    else if ( avg <= 150 && avg > 50 ){
        mySerial.print("22222");               
        delay(5000);                           
        mySerial.print("?f");                 
        delay(30);
    }
   
    else if ( avg <= 350 && avg > 150 ){
        mySerial.print("33333");               
        delay(5000);                               
        mySerial.print("?f");                 
        delay(30);
    }
   }
   
   
   
   
}


PaulS

Code: [Select]
      while ( switchState == LOW ){
   if ( avg <= 50 ){
        mySerial.print("11111");             
        delay(5000);                             
        mySerial.print("?f");                 
        delay(30);
    }   
   
    else if ( avg <= 150 && avg > 50 ){
        mySerial.print("22222");               
        delay(5000);                           
        mySerial.print("?f");                 
        delay(30);
    }
   
    else if ( avg <= 350 && avg > 150 ){
        mySerial.print("33333");               
        delay(5000);                               
        mySerial.print("?f");                 
        delay(30);
    }
   }

If the switch state is LOW when this code is encountered, it will never exit the while loop.

Which switch is supposed to control what is displayed? Is it supposed to display one thing when pressed and another when not, or is each press supposed to change what is displayed?

It would be simple to implement such a feature if you had two functions that could be called, depending on the switch state (or number of presses).

Go Up