Offline
Newbie
Karma: 0
Posts: 10
|
 |
« on: November 06, 2012, 12:03:38 am » |
Hey Guys, Basically I have an Arduino UNO, a 20x4 LCD screen, 4 push buttons and some temp sensors.
Now this project is going in my car to show battery voltages and a few temps.
I had the idea of pushing one button and all my voltages will show, (input via an analog input - 22k resistor on the neg, 68k on the pos) I have attached code for that bit.
Now I just cant Figure out how to tell the audrino to switch what it displays once a button is pushed.
(to show the arduino what button is pushed I have 5V going into the push-buttons, Then each has its own different value of restance. I used this code to see what each value was.) int sensePin =0;
void setup(){ analogReference(DEFAULT); Serial.begin(9600); }
void loop (){ Serial.println(analogRead(sensePin)); delay(500); }
When button 2 is pushed I would like the temps to show, I have Dallas 1 wire digital temp sensors (ill ask about them later)
So My question is how can i tell the Arduino to swap what it displays?
Thanks a heap guys.
|
|
|
|
|
Logged
|
|
|
|
|
Norfolk UK
Offline
Edison Member
Karma: 23
Posts: 1320
|
 |
« Reply #1 on: November 06, 2012, 04:00:29 am » |
Something like this const int sensePin = A0;
void setup(){ Serial.begin(9600); }
void loop (){ int buttonValue = analogRead(sensePin); // buttonValue should be between 0 and 1023 int displayMode = -1; // Assume no change in display mode if (buttonValue > 1000) { displayMode = 3; } if (buttonValue > 700) { displayMode = 2; } if (buttonValue > 400) { displayMode = 1; } if (buttonValue > 100) { displayMode = 0; } Serial.print(buttonValue); Serial.print(" = "); Serial.println(displayMode); delay(500); } You would need to adjust the values to suit your buttons.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 1
Posts: 43
|
 |
« Reply #2 on: November 06, 2012, 05:40:37 am » |
If you have the inputs, I would just run each button to a seperate pin. If you want to use an analog read, look at getting a rotary or other multi-position switch - that way you can't have more than one button pressed at a time, and you (shouldn't) need to debounce.
For the multi-input, store which pin was pushed, and run a switch statement.
For the rotary or multi-position switch, something similar to Riva's code should work.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #3 on: November 07, 2012, 12:22:02 am » |
Thanks, Where do i specify to The Ardunino what code is connected to each display mode.
|
|
|
|
|
Logged
|
|
|
|
|
Norfolk UK
Offline
Edison Member
Karma: 23
Posts: 1320
|
 |
« Reply #4 on: November 07, 2012, 03:42:43 am » |
Thanks, Where do i specify to The Ardunino what code is connected to each display mode. After the code I wrote you have a variable called displayMode that will be the desired display mode required or -1 if no button pressed. Just use something like a switch case http://arduino.cc/en/Reference/SwitchCase to act out the desired mode.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #5 on: November 08, 2012, 01:19:37 am » |
Hey, Ive had a good try at it, hows it look? I'm getting an Error saying " error: expected initializer before 'if' " On this line. if (val == val2) { // make sure we got 2 consistant readings! #include <LiquidCrystal.h> //import the Class for the LCD LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //pin numbers for the LCD
int switchPin = 0; // Select button is connected to pin 0 int val; // variable for reading the pin status int val2; // variable for reading the delayed status int buttonState; // variable to hold the select button state int DisplayMode = 0; // Display Mode
int primary_battPin = 1; // Input from primary Battery int secondary_battPin = 2; // Input from secondary Battery int primary_val = 0; //temporarily store the primary input int secondary_val = 0; //temporarily store the secondary input
float primary_pinVoltage = 0; // variable to hold the calculated voltage float secondary_pinVoltage = 0; // variable to hold the calculated voltage float primary_batteryVoltage = 0; //Temporarily store the primary Voltage float secondary_batteryVoltage = 0; //Temporarily store the secondary Voltage
float ratio = 4.039; // The multiplier depending on which resistors used // I used 68K and 22K 1/2 watt resistors. void setup() { pinMode(switchPin, INPUT); // Set the switch pin as input buttonState = digitalRead(switchPin); // read the initial state lcd.begin(20, 4); // initialise the LCD screen (20 characters long, by 4 lines high) }
void loop()
if (val == val2) { // make sure we got 2 consistant readings! if (val != buttonState) { // the button state has changed! if (val == LOW) { // check if the button is pressed if (DisplayMode == 0) {DisplayMode = 1;} // Change to Mode 1 else { if (DisplayMode == 1) {DisplayMode = 2;} // Change to Mode 2 else { if (DisplayMode == 2) {DisplayMode = 3;} // Change to Mode 3 else { if (DisplayMode == 3) {DisplayMode = 0;} // Change to Mode 0
if (value > (750) { screen(1); } else if (value > 550) { screen(2); } else if (value > 410) { screen(3); } else if (value > 340) { screen(4); } else if (value > 300) { screen(5); }
}}}}}} buttonState = val; // save the new state in our variable
// Now do whatever the DisplayMode Choosen
if (DisplayMode == 0) { // Battery Monitor Selection primary_val = analogRead(primary_battPin); //Read the primary Battery input per loop secondary_val = analogRead(secondary_battPin); //Read the secondary Battery input per loop
primary_pinVoltage = primary_val * 0.00488; //0.00488mV per 1024 steps for 5 Volts (ie; 5 / 1024 = 0.00488) secondary_pinVoltage = secondary_val * 0.00488; primary_batteryVoltage = primary_pinVoltage * ratio; //grab the calculated under 5Volt input and multiply it by the ratio to .......... secondary_batteryVoltage = secondary_pinVoltage * ratio; // .............take it back to the 11~12~13Volt Range.
;lcd.setCursor(0, 0);// Place the Cursor on the Fist line first character position on the LCD screen ;lcd.print ("Batteries:")// Heading ;lcd.setCursor(0, 1); // First Line ;lcd.print("Primary : "); // Second Line ;lcd.print(primary_batteryVoltage); //The Voltage of Primary Battery.. S ;lcd.setCursor(0, 2);// Place the Cursor on the Second line first character position on the LCD screen ;lcd.print("Secondary: "); // Third Line ;lcd.print(secondary_batteryVoltage); //The Voltage of Secondary Battery.
delay(2000); //Pause for two seconds } if (DisplayMode == 1) { // Tempreture Selection lcd.clear(); lcd.setCursor(0, 0); lcd.print("Air Temp : "); // Air Temp Workings lcd.print(" C"); lcd.setCursor(0, 1); lcd.print("Water Temp : "); // Water Temp Workings lcd.print(" C"); delay(2000); }
if (DisplayMode == 2) { // Spare 1 Selection lcd.clear(); lcd.setCursor(0, 0); lcd.print("Spare 1-1 : "); // Spare 1-1 Workings lcd.print(" ?"); lcd.setCursor(0, 1); lcd.print("Spare 1-2 : "); // Temp2 lcd.print(" ?"); delay(2000); }
if (DisplayMode == 3) { // Spare 2 Selection lcd.clear(); lcd.setCursor(0, 0); lcd.print("Spare 2-1 : "); // Spare 2-1 Workings lcd.print(" ?"); lcd.setCursor(0, 1); lcd.print("Spare 2-2 : "); // Spare 2-2 Workings lcd.print(" ?"); delay(2000); } } Thank you.
|
|
|
|
|
Logged
|
|
|
|
|
Norfolk UK
Offline
Edison Member
Karma: 23
Posts: 1320
|
 |
« Reply #6 on: November 08, 2012, 03:13:17 am » |
Several problems with your code I'm sorry to say. You have several variables that are either not declared or not used (value, screen, val2 + many more) You read the button state in setup but no more after that in loop so no amount of button pressing will do anything. You have changed to using a digital pin (initial problem was analogue) but still have code to work with analogue pins. Assuming this is the full code I can see now you will need to think about using a different structure (see BlinkWithoutDelay example) else the button press will be very unresponsive (button reading and long delays don't work well together) I have attached the core of how your program could/should work and you just need to add in your display code where needed. const int switchPin = 2; // Select button is connected to pin 2 const long refreshDelay = 2000; // Duration between display updates
byte displayMode = 0; //Default display mode
void setup() { pinMode(switchPin, INPUT); // Set the switch pin as input }
void loop(){ long currentTime = millis(); // Get current time while ((millis() - currentTime) < refreshDelay){ if (digitalRead(switchPin) == HIGH) { // Alter HIGH/LOW depending on how switch is wired displayMode++; // Increment display mode if (displayMode > 2) { // Reset display mode if beyond max mode displayMode = 0; } break; // Exit while loop early as button pressed } switch (displayMode) { case 0: // Put display mode 0 code here break; case 1: // Put display mode 1 code here break; case 2: // Put display mode 2 code here break; } } }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #7 on: November 08, 2012, 03:53:33 am » |
Thank you so much for your help! With the digital & analog pins, I'm using the Analog pins. Pin 0 is the input for the buttons. So does this mean I can just go const int switchPin = 0; Or Because it says; while ((millis() - currentTime) < refreshDelay){ if (digitalRead(switchPin) == HIGH) I should find an available Digital pin. Not sure if I mentioned the button set up im going for is a voltage divider type set up so all 4 buttons goto the one pin. I have obtained each value each button sends to the Arduino 0- 890 1- 695 2- 363 3- 318 Now sorry for being such a pest Im really trying my best to understand all this, I just copied and pasted some of the old code in. // include the library code: #include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int DisplayMode = 0; // Display Mode
int primary_battPin = 1; // Input from primary Battery int secondary_battPin = 2; // Input from secondary Battery int primary_val = 0; //temporarily store the primary input int secondary_val = 0; //temporarily store the secondary input
float primary_pinVoltage = 0; // variable to hold the calculated voltage float secondary_pinVoltage = 0; // variable to hold the calculated voltage float primary_batteryVoltage = 0; //Temporarily store the primary Voltage float secondary_batteryVoltage = 0; //Temporarily store the secondary Voltage
float ratio = 4.039; // The multiplier depending on which resistors used // I used 68K and 22K 1/2 watt resistors.
const int switchPin = 6; // Select button is connected to pin 2 const long refreshDelay = 2000; // Duration between display updates
byte displayMode = 0; //Default display mode
void setup() { pinMode(switchPin, INPUT); // Set the switch pin as input }
void loop(){ long currentTime = millis(); // Get current time while ((millis() - currentTime) < refreshDelay){ if (digitalRead(switchPin) == HIGH) { // Alter HIGH/LOW depending on how switch is wired displayMode++; // Increment display mode if (displayMode > 2) { // Reset display mode if beyond max mode displayMode = 0; } break; // Exit while loop early as button pressed } switch (displayMode) { case 0: if (DisplayMode == 0) { // Battery Monitor Selection primary_val = analogRead(primary_battPin); //Read the primary Battery input per loop secondary_val = analogRead(secondary_battPin); //Read the secondary Battery input per loop
primary_pinVoltage = primary_val * 0.00488; //0.00488mV per 1024 steps for 5 Volts (ie; 5 / 1024 = 0.00488) secondary_pinVoltage = secondary_val * 0.00488; primary_batteryVoltage = primary_pinVoltage * ratio; //grab the calculated under 5Volt input and multiply it by the ratio to .......... secondary_batteryVoltage = secondary_pinVoltage * ratio; // .............take it back to the 11~12~13Volt Range.
;lcd.setCursor(0, 0);// Place the Cursor on the Fist line first character position on the LCD screen ;lcd.print ("Batteries:")// Heading ;lcd.setCursor(0, 1); // First Line ;lcd.print("Primary : "); // Second Line ;lcd.print(primary_batteryVoltage); //The Voltage of Primary Battery.. S ;lcd.setCursor(0, 2);// Place the Cursor on the Second line first character position on the LCD screen ;lcd.print("Secondary: "); // Third Line ;lcd.print(secondary_batteryVoltage); //The Voltage of Secondary Battery.
delay(2000); //Pause for two seconds } break; case 1: // Put display mode 1 code here break; case 2: // Put display mode 2 code here break; } } } Thanks for your Input 
|
|
|
|
|
Logged
|
|
|
|
|
Norfolk UK
Offline
Edison Member
Karma: 23
Posts: 1320
|
 |
« Reply #8 on: November 08, 2012, 07:49:14 am » |
At least one of us us getting confused as original post and example code used analogRead but the next version you sent used digital (buttonState = digitalRead(switchPin); // read the initial state) Not sure if I mentioned the button set up im going for is a voltage divider type set up so all 4 buttons goto the one pin. I have obtained each value each button sends to the Arduino
0- 890 1- 695 2- 363 3- 318 What value do you get with no button pressed, is it 0 or 1023 as this will effect the below code. const int switchPin = A0; // Select button is connected to pin const long refreshDelay = 2000; // Duration between display updates
byte displayMode = 0; //Default display mode byte oldDisplayMode = 0; //Old display mode
void setup() { pinMode(switchPin, INPUT); // Set the switch pin as input }
void loop(){ long currentTime = millis(); // Get current time while ((millis() - currentTime) < refreshDelay){ int buttonValue = analogRead(switchPin); // buttonValue should be between 0 and 1023 if (buttonValue > 792) { displayMode = 0; } if (buttonValue > 529) { displayMode = 1; } if (buttonValue > 340) { displayMode = 2; } if (buttonValue > 159) { displayMode = 3; } if (displayMode != oldDisplayMode) { // Has display mode changed? oldDisplayMode = displayMode; break; // Exit while loop early as button pressed } } switch (displayMode) { case 0: // Put display mode 0 code here break; case 1: // Put display mode 1 code here break; case 2: // Put display mode 2 code here break; case 3: // Put display mode 3 code here break; } } This code will not work as is as you need to know what value is returned for no button pressed. I'm also not able to test it as I'm at work (no arduino here)
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #9 on: November 09, 2012, 01:29:03 am » |
Hey, What value do you get with no button pressed, is it 0 or 1023 as this will effect the below code. I'm getting 0. I'm sorry for the confusion, I had 2 versions of this going (mistake as it lead to so much confusion) I think Ill make the input pin an analog one. This is what I have at the minute and Ive uploaded it to the Arduino. #include <LiquidCrystal.h> //import the Class for the LCD LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //pin numbers for the LCD
const int switchPin = A0; // Select button is connected to pin const long refreshDelay = 2000; // Duration between display updates
byte displayMode = 0; //Default display mode byte oldDisplayMode = 0; //Old display mode byte buttonValue = 0;
int val; // variable for reading the pin status int val2; // variable for reading the delayed status int primary_battPin = 1; // Input from primary Battery int secondary_battPin = 2; // Input from secondary Battery int primary_val = 0; //temporarily store the primary input int secondary_val = 0; //temporarily store the secondary input
float primary_pinVoltage = 0; // variable to hold the calculated voltage float secondary_pinVoltage = 0; // variable to hold the calculated voltage float primary_batteryVoltage = 0; //Temporarily store the primary Voltage float secondary_batteryVoltage = 0; //Temporarily store the secondary Voltage
float ratio = 4.039; // The multiplier depending on which resistors used // I used 68K and 22K 1/2 watt resistors.
void setup() { pinMode(switchPin, INPUT); // Set the switch pin as input }
void loop(){ long currentTime = millis(); // Get current time while ((millis() - currentTime) < refreshDelay){ int buttonValue = analogRead(switchPin); // buttonValue should be between 0 and 1023 if (buttonValue > 792) { displayMode = 0; } if (buttonValue > 529) { displayMode = 1; } if (buttonValue > 340) { displayMode = 2; } if (buttonValue > 159) { displayMode = 3; } if (displayMode != oldDisplayMode) { // Has display mode changed? oldDisplayMode = displayMode; break; // Exit while loop early as button pressed } } switch (displayMode) { case 0: primary_val = analogRead(primary_battPin); //Read the primary Battery input per loop secondary_val = analogRead(secondary_battPin); //Read the secondary Battery input per loop
primary_pinVoltage = primary_val * 0.00488; //0.00488mV per 1024 steps for 5 Volts (ie; 5 / 1024 = 0.00488) secondary_pinVoltage = secondary_val * 0.00488; primary_batteryVoltage = primary_pinVoltage * ratio; //grab the calculated under 5Volt input and multiply it by the ratio to .......... secondary_batteryVoltage = secondary_pinVoltage * ratio; // .............take it back to the 11~12~13Volt Range.
;lcd.setCursor(0, 0);// Place the Cursor on the Fist line first character position on the LCD screen ;lcd.print ("Batteries:")// Heading ;lcd.setCursor(0, 1); // First Line ;lcd.print("Primary : "); // Second Line ;lcd.print(primary_batteryVoltage); //The Voltage of Primary Battery.. S ;lcd.setCursor(0, 2);// Place the Cursor on the Second line first character position on the LCD screen ;lcd.print("Secondary: "); // Third Line ;lcd.print(secondary_batteryVoltage); //The Voltage of Secondary Battery.
delay(2000); //Pause for two seconds break; case 1: // Put display mode 1 code here break; case 2: // Put display mode 2 code here break; case 3: // Put display mode 3 code here break; } } Now When I push the button that send the value 890 To pin A0..... Nothing happens  I cant believe how Frustrating this is, Maybe ive tried a project that's beyond my level of coding ability,:(
|
|
|
|
|
Logged
|
|
|
|
|
Norfolk UK
Offline
Edison Member
Karma: 23
Posts: 1320
|
 |
« Reply #10 on: November 09, 2012, 03:45:33 am » |
Now When I push the button that send the value 890 To pin A0..... Nothing happens smiley-sad I cant believe how Frustrating this is, Maybe ive tried a project that's beyond my level of coding ability,:( More likely it's beyond my ability  I was cascading the button reading the wrong way so it was always coming out as mode 3 Try this. #include <LiquidCrystal.h> //import the Class for the LCD LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //pin numbers for the LCD
const int switchPin = A0; // Select button is connected to pin const long refreshDelay = 2000; // Duration between display updates
byte oldDisplayMode = 0; // Old display mode
int val; // variable for reading the pin status int val2; // variable for reading the delayed status int primary_battPin = 1; // Input from primary Battery int secondary_battPin = 2; // Input from secondary Battery int primary_val = 0; //temporarily store the primary input int secondary_val = 0; //temporarily store the secondary input
float primary_pinVoltage = 0; // variable to hold the calculated voltage float secondary_pinVoltage = 0; // variable to hold the calculated voltage float primary_batteryVoltage = 0; //Temporarily store the primary Voltage float secondary_batteryVoltage = 0; //Temporarily store the secondary Voltage
float ratio = 4.039; // The multiplier depending on which resistors used // I used 68K and 22K 1/2 watt resistors.
void setup() { pinMode(switchPin, INPUT); // Set the switch pin as input }
void loop(){ long currentTime = millis(); // Get current time while ((millis() - currentTime) < refreshDelay){ int buttonValue = analogRead(switchPin); // buttonValue should be between 0 and 1023 byte displayMode = -1; // Assume no change in display mode if (buttonValue > 159) { displayMode = 3; } if (buttonValue > 340) { displayMode = 2; } if (buttonValue > 529) { displayMode = 1; } if (buttonValue > 792) { displayMode = 0; } if (displayMode != -1) { // Was button pressed? if (displayMode != oldDisplayMode) { // Has display mode changed? oldDisplayMode = displayMode; // Set new display mode break; // Exit while loop early as button pressed } } } switch (oldDisplayMode) { case 0: primary_val = analogRead(primary_battPin); //Read the primary Battery input per loop secondary_val = analogRead(secondary_battPin); //Read the secondary Battery input per loop primary_pinVoltage = primary_val * 0.00488; //0.00488mV per 1024 steps for 5 Volts (ie; 5 / 1024 = 0.00488) secondary_pinVoltage = secondary_val * 0.00488; primary_batteryVoltage = primary_pinVoltage * ratio; //grab the calculated under 5Volt input and multiply it by the ratio to .......... secondary_batteryVoltage = secondary_pinVoltage * ratio; // .............take it back to the 11~12~13Volt Range. lcd.setCursor(0, 0);// Place the Cursor on the Fist line first character position on the LCD screen lcd.print ("Batteries:");// Heading lcd.setCursor(0, 1); // First Line lcd.print("Primary : "); // Second Line lcd.print(primary_batteryVoltage); //The Voltage of Primary Battery.. S lcd.setCursor(0, 2);// Place the Cursor on the Second line first character position on the LCD screen lcd.print("Secondary: "); // Third Line lcd.print(secondary_batteryVoltage); //The Voltage of Secondary Battery. break; case 1: // Put display mode 1 code here break; case 2: // Put display mode 2 code here break; case 3: // Put display mode 3 code here break; } }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #11 on: November 09, 2012, 05:51:27 am » |
I cant thank you enough!!! It well on the way, Now yet another problem but im hoping its an easy fix, Now when I press the button, the screen only reads, Secondary: (insert voltage) Not the other bits like it should. Now Ive double checked and the screen does work with this code // include the library code: #include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() { // set up the LCD's number of columns and rows: lcd.begin(20, 4); // Print a message to the LCD. lcd.print("hello, world!"); }
void loop() { // set the cursor to column 0, line 1 // (note: line 1 is the second row, since counting begins with 0): lcd.setCursor(0, 1); // print the number of seconds since reset: lcd.print(millis()/1000); } (I have made sure every line works) Ideas on that one, It has to be in the code somewhere 
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #12 on: November 09, 2012, 05:53:53 am » |
I found the problem!!! I forgot to tell the Arduino how Big the screen was lcd.begin(20, 4); . Now Im onto my next adventure, The temp sensors Ill post what im come up with here, Probally tomorrow its getting late here is Australia! Riva, Thank you so much for your help !!!
|
|
|
|
|
Logged
|
|
|
|
|
Norfolk UK
Offline
Edison Member
Karma: 23
Posts: 1320
|
 |
« Reply #13 on: November 09, 2012, 06:27:52 am » |
Riva, Thank you so much for your help !!! No problem 
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #14 on: November 09, 2012, 06:35:54 am » |
Despite my tired eyes i want to get a little more done.
Now as i press the button the values change, This is all good until I release then all values freeze, Is there a way to make it keep reading without holding the button in or just change switch types?
2- Not Really a big thing, but when i turn the Arduino on, its just a blank screen until i press a button, In this section is it possible to put a message in, I tried putting it in a few spots but It didn't work properly , IE- the text i put in never went away.
Maybe just wire a resistor in so on start it gives a high value and then put that in the code?
|
|
|
|
|
Logged
|
|
|
|
|
|