Offline
Newbie
Karma: 0
Posts: 19
|
 |
« on: April 11, 2011, 12:00:38 am » |
I've been working on this project for a very short time and I can see the half way point of the whole project. Here is a recap I'm replacing my automotive gauge sensors with an Arduino board I have an LCD and some sensors for testing / POC I've tested the sensors and the LCD are basically working the way I want, I can fine tune later. What I need to do is be able to switch between the sensors (sensorPin0, sensorPin1, sensorPin2) and display the results on the LCD. I've tested all of the sketches individually and their good enough for POC. Below is my sad attempt to join the 3 sketches. Now I want to toggle between the sensors and display on the LCD. At the moment a DPDT push button will have to do as my rotary encoder hasn't arrived yet. All I want to do is hit the button 5 times (somewhat quickly) and it displays the status of the next sensor on the LCD, so it will just keep toggling through the sensors / display endlessly I've included my code and a Fritzing any suggestions would be appreciated. I've looked at the Menu Library but it seems it might be a little much for what I'm wanting to do. I may be wrong. Any examples I can dissect would help, I like learning this stuff but chasing rabbits gets old quick! #include <LiquidCrystal.h> // include the LCD library #include <LcdBarGraph.h> // include the LCDBarGraph library
// DISPLAY VARS LiquidCrystal lcd(7, 8, 9, 10, 11, 12); byte lcdNumCols = 16; // -- number of columns in the LCD LcdBarGraph lbg(&lcd, lcdNumCols); // -- creating
// SENSOR VARS
int sensorPin0 = 0; // -- value for this example int sensorPin1 = 0; // -- value for this example int sensorPin2 = 0; // -- value for this example int potPin0 = A0; //Sensor input pin Oil int potPin1 = A1; //Sensor input pin Fuel int potPin2 = A2; //Sensor input pin Boost int inPin0 = 5; // choose the input pin (for a pushbutton) int val = 0; // variable for reading the pin status
float potVal = 0; int sensorVal = 0;
void setup() { lcd.begin(16, 2); // lcd rows and columns lcd.setCursor(0, 1); // set cursor to second row, first column lcd.print("XYZ oil 0"); // title of sorts delay(500); // time to display title pinMode(inPin0, INPUT); // declare pushbutton as input }
void loop() { // MAKE MEASUREMENTS // potVal = analogRead(potPin) / 10.2; // sensorVal = analogRead(sensorPinX);
// DISPLAY RESULTS lbg.drawValue( analogRead(sensorPin0), 1024);// -- Analog 0 sensor pin for Oil pressor sender lbg.drawValue( analogRead(sensorPin1), 1024);// -- Analog 1 sensor pin for Fuel pressor sender lbg.drawValue( analogRead(sensorPin2), 1024);// -- Analog 2 sensor pin for Boost MAP sender delay(500); // ~ 2Hz display is fast enough }

|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Tesla Member
Karma: 76
Posts: 6852
Arduino rocks
|
 |
« Reply #1 on: April 11, 2011, 05:43:16 am » |
Suggestion: have an integer variable called something like current_sensor that is set by some code that interprets switch presses. Also the code to drive the lcd can then use this variable to determine which sensor to display. In effect you have split the problem into two simpler problems.
Arrays are very useful once you start numbering the sensors BTW.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 19
|
 |
« Reply #2 on: April 11, 2011, 10:00:09 am » |
I spent the morning looking at integers and switching interrupt code but I'm not seeing it. Do you happen to have some examples?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Tesla Member
Karma: 76
Posts: 6852
Arduino rocks
|
 |
« Reply #3 on: April 11, 2011, 02:10:43 pm » |
Interrupts? No I'm just suggesting splitting the problem into (A) detecting button presses and updating the variable (this can be tested in its own right and you will learn by doing) and (B) using an integer variable to selecting what you display - again you can test this separately.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 19
|
 |
« Reply #4 on: April 13, 2011, 09:20:55 pm » |
ok I got a button push counter integrated into the sketch. It now triggers pin 13 (an LED / event) when the button is pushed 4 times (very slowly  got to figure that out also ). I also think i've got 'most' of an integer variable setup right for each of the sensors but I think I'm missing a little bit. Basically I need the LED / event to trigger switching to the next menu item and when it comes to the end, it goes to the beginning. Tie the event (LED high) to switch to one of the sensorVar (I think) Here's my code as it sits now. Of course I'm open to all suggestions. #include <LiquidCrystal.h> // include the LCD library #include <LcdBarGraph.h> // include the LCDBarGraph library
// DISPLAY VARS (setup for the LCD display, ran on startup once) LiquidCrystal lcd(7, 8, 9, 10, 11, 12); byte lcdNumCols = 16; // -- number of columns in the LCD LcdBarGraph lbg(&lcd, lcdNumCols); // -- creating
// SENSOR VARS (Telling the Arduino ports which to activate, ran once on startup) const int buttonPin = 5; // the pin that the wheel switch is attached to const int ledPin = 13; // the pin that the LED is attached to int buttonPushCounter = 0; // counter for the number of button presses int buttonState = 0; // current state of the button int lastButtonState = 0; // previous state of the button int sensorPin0 = 0; // -- value for this example int sensorPin1 = 1; // -- value for this example int sensorPin2 = 2; // -- value for this example int val = 0; // variable for reading the pin 0 status // float maxVoltage = 4.65; // Maximum voltage sent out from the sensors (these values are the same for O&F not for B)(For a later time) // float minVoltage = .46; // Minimum voltage sent out from the sensors (these values are the same for O&F not for B)(For a later time) // float scale = 4.5/1024.0; // convert 10-bit value to 4.5v scale for sensor (will need this for scaleing the sensor)(For a later time)
// float Oil ; ??? // float fuel ; ??? // float Boost ; ??? int sensorVal0 = 0; int sensorVal1 = 1; int sensorVal2 = 2;
void setup() // (setup and starting the activated Arduino ports, runs once) { { // OIL PRESSURE SENDER SETUP lcd.begin(16, 2); // lcd rows and columns lcd.setCursor(0, 1); // set cursor to second row, first column lcd.print("Oil Pressure"); // title of sorts sensorVal0 = analogRead(sensorPin0); // read Analog0 pin sensor oil and mapping it to a value delay(500); // ~ 2Hz display is fast enough } { // FUEL PRESSURE SENDER SETUP lcd.begin(16, 2); // lcd rows and columns lcd.setCursor(0, 1); // set cursor to second row, first column lcd.print("Fuel Pressure"); // title of sorts sensorVal1 = analogRead(sensorPin1); // read Analog1 pin sensor fuel and mapping it to a value delay(500); // ~ 2Hz display is fast enough } { // BOOST PRESSURE SENDER SETUP lcd.begin(16, 2); // lcd rows and columns lcd.setCursor(0, 1); // set cursor to second row, first column lcd.print("Boost"); // title of sorts sensorVal2 = analogRead(sensorPin2); // read Analog1 pin sensor fuel and mapping it to a value delay(500); // ~ 2Hz display is fast enough } // lcd.begin(16, 2); // lcd rows and columns (will be removed once switching is resolved) // lcd.setCursor(0, 1); // set cursor to second row, first column (will be removed once switching is resolved) // lcd.print("XYZ oil 0"); // title of sorts (will be removed once switching is resolved) // delay(500); // time to display title (will be removed once switching is resolved) // initialize the button pin as a input: pinMode(buttonPin, INPUT); // initialize the LED as an output: pinMode(ledPin, OUTPUT); // initialize serial communication: Serial.begin(9600); }
void loop() // (stuff that is constantly looped or monitored) { // BUTTON COUNTS buttonState = digitalRead(buttonPin); // read the pushbutton input pin: if (buttonState != lastButtonState) { // compare the buttonState to its previous state if (buttonState == HIGH) { // if the state has changed, increment the counter // if the current state is HIGH then the button // went from off to on: buttonPushCounter++; // Serial.println("on"); // Serial.print("number of button pushes: "); // Serial.println(buttonPushCounter, DEC); // } // else { // if the current state is LOW then the button // went from on to off: // Serial.println("off"); } } lastButtonState = buttonState; // save the current state as the last state, for next time through the loop
if (buttonPushCounter % 4 == 0) { // turns on the LED every four button pushes by digitalWrite(ledPin, HIGH); // checking the modulo of the button push counter. } else { // the modulo function gives you the remainder of digitalWrite(ledPin, LOW); // the division of two numbers: } { // sensorVal0 = analogRead(sensorPin0); // read Analog0 pin sensor oil and mapping it to a value // sensorVal1 = analogRead(sensorPin1); // read Analog1 pin sensor fuel and mapping it to a value // sensorVal2 = analogRead(sensorPin2); // read Analog2 pin sensor boost and mapping it to a value }
// BAR GRAPH RESULTS FOR EACH SENSOR { lbg.drawValue( analogRead(sensorPin0), 1024); // -- Analog 0 sensor pin for Oil sender, link for the LCDBarGraph library) delay(500); // ~ 2Hz display is fast enough } { lbg.drawValue( analogRead(sensorPin1), 1024); // -- Analog 1 sensor pin for Fuel sender, link for the LCDBarGraph library) delay(500); // ~ 2Hz display is fast enough } { lbg.drawValue( analogRead(sensorPin2), 1024); // -- Analog 2 sensor pin for Boost MAP sender, link for the LCDBarGraph library) delay(500); // ~ 2Hz display is fast enough } }
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 336
Posts: 36486
Seattle, WA USA
|
 |
« Reply #5 on: April 14, 2011, 03:54:19 am » |
int sensorVal0 = 0; int sensorVal1 = 1; int sensorVal2 = 2; Strange initial values... sensorVal0 = analogRead(sensorPin0); // read Analog0 pin sensor oil and mapping it to a value
Why are you reading a value in setup()? You don't use that value. { // sensorVal0 = analogRead(sensorPin0); // read Analog0 pin sensor oil and mapping it to a value // sensorVal1 = analogRead(sensorPin1); // read Analog1 pin sensor fuel and mapping it to a value // sensorVal2 = analogRead(sensorPin2); // read Analog2 pin sensor boost and mapping it to a value } What are the curly braces for? { lbg.drawValue( analogRead(sensorPin0), 1024); // -- Analog 0 sensor pin for Oil sender, link for the LCDBarGraph library) delay(500); // ~ 2Hz display is fast enough } And these? It now triggers pin 13 (an LED / event) when the button is pushed 4 times (very slowly got to figure that out also ). With three 1/2 second delays, you will only read the switch state every 1.5+ seconds. It will take 6+ seconds to read 4 switch presses. Is that the "very slowly" you are referring to?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 19
|
 |
« Reply #6 on: April 14, 2011, 11:19:27 am » |
Ok so I cleaned up the code. Still not able to figure out how to trigger an input #include <LiquidCrystal.h> // include the LCD library #include <LcdBarGraph.h> // include the LCDBarGraph library
// DISPLAY VARS (setup for the LCD display, ran on startup once) LiquidCrystal lcd(7, 8, 9, 10, 11, 12); byte lcdNumCols = 16; // -- number of columns in the LCD LcdBarGraph lbg(&lcd, lcdNumCols); // -- creating
// SENSOR VARS (Telling the Arduino ports which to activate, ran once on startup) const int buttonPin = 5; // the pin that the wheel switch is attached to const int ledPin = 13; // the pin that the LED is attached to int buttonPushCounter = 0; // counter for the number of button presses int buttonState = 0; // current state of the button int lastButtonState = 0; // previous state of the button int sensorPin0 = 0; // -- value for this example int sensorPin1 = 0; // -- value for this example int sensorPin2 = 0; // -- value for this example int val = 0; // variable for reading the pin 0 status // float maxVoltage = 4.65; // Maximum voltage sent out from the sensors (these values are the same for O&F not for B)(For a later time) // float minVoltage = .46; // Minimum voltage sent out from the sensors (these values are the same for O&F not for B)(For a later time) // float scale = 4.5/1024.0; // convert 10-bit value to 4.5v scale for sensor (will need this for scaleing the sensor)(For a later time)
// float Oil ; ??? // float fuel ; ??? // float Boost ; ??? int sensorVal0 = 0; int sensorVal1 = 0; int sensorVal2 = 0;
void setup() // (setup and starting the activated Arduino ports, runs once) { { // OIL PRESSURE SENDER SETUP lcd.begin(16, 2); // lcd rows and columns lcd.setCursor(0, 1); // set cursor to second row, first column lcd.print("Oil Pressure"); // title of sorts sensorVal0 = analogRead(sensorPin0); // read Analog0 pin sensor oil and mapping it to a value // delay(500); // ~ 2Hz display is fast enough } { // FUEL PRESSURE SENDER SETUP lcd.begin(16, 2); // lcd rows and columns lcd.setCursor(0, 1); // set cursor to second row, first column lcd.print("Fuel Pressure"); // title of sorts sensorVal1 = analogRead(sensorPin1); // read Analog1 pin sensor fuel and mapping it to a value // delay(500); // ~ 2Hz display is fast enough } { // PRESSURE SENDER SETUP lcd.begin(16, 2); // lcd rows and columns lcd.setCursor(0, 1); // set cursor to second row, first column lcd.print("Boost"); // title of sorts sensorVal2 = analogRead(sensorPin2); // read Analog1 pin sensor fuel and mapping it to a value // delay(500); // ~ 2Hz display is fast enough } // lcd.begin(16, 2); // lcd rows and columns (will be removed once switching is resolved) // lcd.setCursor(0, 1); // set cursor to second row, first column (will be removed once switching is resolved) // lcd.print("XYZ oil 0"); // title of sorts (will be removed once switching is resolved) // delay(500); // time to display title (will be removed once switching is resolved) // initialize the button pin as a input: pinMode(buttonPin, INPUT); // initialize the LED as an output: pinMode(ledPin, OUTPUT); // initialize serial communication: Serial.begin(9600); }
void loop() // (stuff that is constantly looped or monitored) { // BUTTON COUNTS buttonState = digitalRead(buttonPin); // read the pushbutton input pin: if (buttonState != lastButtonState) { // compare the buttonState to its previous state if (buttonState == HIGH) { // if the state has changed, increment the counter // if the current state is HIGH then the button // went from off to on: buttonPushCounter++; Serial.println("on"); Serial.print("number of button pushes: "); Serial.println(buttonPushCounter, DEC); } else { // if the current state is LOW then the button // went from on to off: Serial.println("off"); } } lastButtonState = buttonState; // save the current state as the last state, for next time through the loop
if (buttonPushCounter % 4 == 0) { // turns on the LED every four button pushes by digitalWrite(ledPin, HIGH); // checking the modulo of the button push counter. } else { // the modulo function gives you the remainder of digitalWrite(ledPin, LOW); // the division of two numbers: } // { // sensorVal0 = analogRead(sensorPin0); // read Analog0 pin sensor oil and mapping it to a value // sensorVal1 = analogRead(sensorPin1); // read Analog1 pin sensor fuel and mapping it to a value // sensorVal2 = analogRead(sensorPin2); // read Analog2 pin sensor boost and mapping it to a value // }
// BAR GRAPH RESULTS FOR EACH SENSOR { lbg.drawValue( analogRead(sensorPin0), 1024); // -- Analog 0 sensor pin for Oil sender, link for the LCDBarGraph library) lbg.drawValue( analogRead(sensorPin1), 1024); // -- Analog 1 sensor pin for Fuel sender, link for the LCDBarGraph library) lbg.drawValue( analogRead(sensorPin2), 1024); // -- Analog 2 sensor pin for Boost MAP sender, link for the LCDBarGraph library) delay(200); // ~ 2Hz display is fast enough } }
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 336
Posts: 36486
Seattle, WA USA
|
 |
« Reply #7 on: April 14, 2011, 12:18:48 pm » |
Still not able to figure out how to trigger an input Push the switch. Or explain the statement/issue a little better.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 19
|
 |
« Reply #8 on: April 14, 2011, 12:27:29 pm » |
Ha ok
If I press the button 4 times a LED lights (pin 13) I need find a sample or get some help on how to convert the event (LED light on when button is pressed 4 times) to display the next sensorpin
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 336
Posts: 36486
Seattle, WA USA
|
 |
« Reply #9 on: April 14, 2011, 12:38:17 pm » |
I need find a sample or get some help on how to convert the event (LED light on when button is pressed 4 times) to display the next sensorpin The "event" is the switch press, not the LED lighting. You can't "display" a "sensorpin". You can display the value read from the pin. But, it seems that want you want to do is display different data depending on the number of switch presses. One switch press, display fuel level; two presses, display oil pressure; etc.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 19
|
 |
« Reply #10 on: April 14, 2011, 12:41:42 pm » |
No not quite. I want 4 presses display oil ...4 presses display fuel... 4 presses display boost etc.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 336
Posts: 36486
Seattle, WA USA
|
 |
« Reply #11 on: April 14, 2011, 01:47:49 pm » |
No not quite. I want 4 presses display oil ...4 presses display fuel... 4 presses display boost etc. OK. Whatever. It's your system. Pushing the switch once or several times is not the issue. The issue is that when some even occurs (one switch press or the 4th switch press), a state change occurs. It is that state that defines what happens. byte state = 0; byte count = 0; void loop() { // See if the switch is pressed, but wasn't before // If so, increment count if(count == 4) { state++; count = 0; }
switch(state) { case 0: // Show something break; case 1; // Show something else break; case 2: // Show dancing girls break; case 3: // Show whatever state = 0; break; } }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 19
|
 |
« Reply #12 on: April 14, 2011, 08:23:40 pm » |
Can I put a switch (state) in void setup?
|
|
|
|
|
Logged
|
|
|
|
|
New Jersey
Online
Edison Member
Karma: 26
Posts: 2455
|
 |
« Reply #13 on: April 14, 2011, 09:08:56 pm » |
Yes, but you probably don't need to. In PaulS' example, state would be zero at that point, so there would be no need to test what it was - you already know. Furthermore, it'll call loop very shortly afterwards, state will still be zero & the switch statement there will take care of putting something on the display.
BTW, out of curiosity, why do you want four button presses from the user to invoke a display change?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 19
|
 |
« Reply #14 on: April 14, 2011, 09:22:26 pm » |
I was looking for a rotary encoder and was about to order one when my GF though away an old USB Mini laptop mouse. Well the scroll wheel is a cheap little switch. I've never seen one before but each click is an open and close. it the perfect wheel color and size for my project, It's simple to code for, the mounting is very simple, and best of all its free. I want to scroll the wheel half a turn which counts to be about 5 open and closings of the switch. I've got some debouncing to do, but getting the stupid state thing to work is killing me. this is my code so far. Its ugly but im trying, But I'm tempted to pull out the checkbook and see is someone wants to make a few bucks to finish the code. UUUGGGHHH #include <LiquidCrystal.h> #include <LcdBarGraph.h> // include the LCDBarGraph library
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); byte lcdNumCols = 16; // -- number of columns in the LCD LcdBarGraph lbg(&lcd, lcdNumCols); // -- creating
long hightimer; long lowtimer; const int buttonPin = 5; // const int LedState = 13;
// int LEDstate = HIGH; int buttonPinState = HIGH; int DownButtonState = HIGH;
int sensorPin0 = 0; // -- value for this example int sensorPin1 = 1; // -- value for this example int sensorPin2 = 2; // -- value for this example
int sensorVal0 = 0; int sensorVal1 = 1; int sensorVal2 = 2;
int buttonState = 0; // current state of the button int lastButtonState = 0; // previous state of the button int buttonPushCounter = 0; // counter for the number of button presses
int timelow = 100; int state = 1;
void setup() { lcd.begin(16, 2); lcd.clear(); } void loop() // (stuff that is constantly looped or monitored)
// BUTTON COUNTS { buttonState = digitalRead(buttonPin); // read the pushbutton input pin: if (buttonState != lastButtonState) { // compare the buttonState to its previous state if (buttonState == HIGH) // if the state has changed, increment the counter // if the current state is HIGH then the button // went from off to on: buttonPushCounter++; } { lastButtonState = buttonState; // save the current state as the last state, for next time through the loop } if (buttonPushCounter % 4 == 0) { // turns on the LED every four button pushes by digitalWrite(buttonPin, LOW); lowtimer = millis(); } if(buttonState != LOW){ digitalWrite(buttonPin, HIGH); if(state == 0); // { state = 0; // { else state = state + 1; }
delay (500); // screen refresh lcd.clear(); switch (state) { case 1: lcd.setCursor(0, 1); lcd.print("oil pressure"); lbg.drawValue( analogRead(sensorPin0), 1024); // -- Analog 0 sensor pin for Oil sender, link for the LCDBarGraph library) break; case 2: lcd.setCursor(0, 1); lcd.print("fuel pressure"); lbg.drawValue( analogRead(sensorPin1), 1024); // -- Analog 1 sensor pin for Fuel sender, link for the LCDBarGraph library) break; case 3: lcd.setCursor(0, 1); lcd.print("boost"); lbg.drawValue( analogRead(sensorPin2), 1024); // -- Analog 2 sensor pin for Boost MAP sender, link for the LCDBarGraph library) break; case 4: lcd.setCursor(0, 1); lcd.print("Menu Item 4"); break; case 5: lcd.setCursor(0, 1); lcd.print("Menu Item 5"); state = 0; break; } }
|
|
|
|
|
Logged
|
|
|
|
|
|