Go Down

Topic: First post and first project advice (Read 969 times) previous topic - next topic

surfer_crx

I have been reading the forums for a few weeks now and having bought an Uno decided that I should sign up, as at some point I know I will need some help. I have built a few of the projects in the learning section and read through several more. I now feel like I should have a go at starting my own project, so I will be trying to build a character LCD that displays a reading from a manifold pressure sensor that I had lying about in my garage. The sensor is basically a potentiometer and when on the car would operate with a 0-5v range so I thought this would be ideal, that way when it is fully functional I can put it on the car and properly test its operation.

I guess the reason for my first post is because there are a lot of people that have managed something to this effect but there is very little that explains the way it works to a level that I understand, I could probably limp along by chopping code from other projects but I'm not sure that would teach me anything. So I'll post what I have written so far and if someone could have a look at it and maybe nudge me in the right direction I would be grateful, I would like to make it clear that I don't want spoon feeding, just some feedback would be good.

The part that has been written so far has yet to be tested on the Arduino, what I want it to do is to display on the top line the text, manifold pressure, and on the line below that display a reading from the potentiometer of between 0 and 5 with a resolution of two decimal places as at the moment that is all I require.

Thanks in advance.


Code: [Select]
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int sensorPin = A0; // the input for the potentiometer
int sensorValue = 0; // store the potentiometer value here

void setup () {
lcd.begin (20,4);// my lcd screen is 20 characters per line by 4 lines
lcd.print ("Manifold Pressure");// This is what I wand to be permanently on line 0
lcd.setCursor (0,1)// I put this line in so that the loop would not write to line 0
}

void loop(){
sensorValue = analogRead (sensorPin);//read value from potentiometer
outputValue = map (sensorValue, 0, 1023, 0, 5.00) // this line is probably wrong but am trying to work it out, I would like the reading to be from 0 to 5 with 2 decimal places on the display
lcd.print (outputValue);// print the output value to the lcd screen
delay (100);//wait one tenth of a second before repeating the loop

}

CrossRoads

I believe you need to add declaration of  outputValue and use data type 'float' for this to work

outputValue = map (sensorValue, 0, 1023, 0, 5.00) // this line is probably wrong but am trying to work it out, I would like the reading to be from 0 to 5 with 2 decimal places on the display

and maybe something more like:
outputValue = 0.00488 * outputValue; // convert analog reading into Volts, 1 bit of analogRead = 4.88mV
or
outputValue = (1/1023)*outputValue;
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

robtillaart


refactored your code
Code: [Select]

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int sensorPin = A0;
int sensorValue = 0;

void setup ()
{
  lcd.begin (20,4);
  lcd.print ("Manifold Pressure");

}

void loop()
{
  sensorValue = analogRead (sensorPin);
  float outputValue =  5.0 * sensorValue/ 1023.0;
  lcd.setCursor (0,1);
  lcd.print (outputValue, 2);
  delay (100);
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

surfer_crx

Thanks for the help guys.

I copied and pasted that code and it worked as I was wanting it to. I noiced that you move the lcd.setCursor instruction from the setup to the loop, why did you do this? I know your way was the right way of doing it as I moved it back to see what would happen and it just printed a figure to every character space on the LCD.

strykeroz


I noiced that you move the lcd.setCursor instruction from the setup to the loop, why did you do this? I know your way was the right way of doing it as I moved it back to see what would happen and it just printed a figure to every character space on the LCD.

The reason for this is after writing the value to the LCD the first time around the loop, the LCD cursor will be at the end of that, rather than at the start of the 2nd row where you need it, so this resets the print location each loop() iteration or your values will simply be printed off to the right out of view on the LCD.

It's a bit of a shame "Manifold Pressure" is a character too long too.  Though I can't think of a worthy abbreviation :)

Geoff
"There is no problem so bad you can't make it worse"
- retired astronaut Chris Hadfield

surfer_crx

So that is why it was filling the entire screen with a reading.

Would I be right in thinking then that if you write something to the screen, then write something else without specifying a cursor position that it would write to the first position after the last character that was printed?

As for the text "Manifold Pressure" I'm using a 20 character display so I have 3 spare character spaces.

strykeroz

#6
Mar 20, 2012, 02:51 pm Last Edit: Mar 20, 2012, 02:55 pm by strykeroz Reason: 1

Would I be right in thinking then that if you write something to the screen, then write something else without specifying a cursor position that it would write to the first position after the last character that was printed?
Yes. You're right on the money.  The only other thing to watch therefore, is if your new value is not as long as a previous one you can end up not overwriting it entirely.  Simply including a clear() and printing the headings in the loop() function would ensure that is avoided.

As for the text "Manifold Pressure" I'm using a 20 character display so I have 3 spare character spaces.
Of course! Sorry, my presumption was 16x2.  :smiley-roll:
"There is no problem so bad you can't make it worse"
- retired astronaut Chris Hadfield

surfer_crx

I'm looking to add multiple layers to the display so that when a button is pressed it will change from displaying the information that is currently on the screen, to something else for example temperature. I am now quite confident that I can write the instructions for the display, but I am struggling to figure out how to write the program to allow me to achieve this.

I was thinking that I would need to use the following (shamelessly stolen from the learning section) to monitor how many times the button has been pressed so the arduino will know which part of the routine to run.

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



I will also include a debounce as there is likely to be some noise where this will be used on completion. Would I be right in thinking that a switch statement is the one to use to get the screen to display the information from different sensors?

strykeroz

#8
Mar 22, 2012, 10:34 pm Last Edit: Mar 22, 2012, 10:37 pm by strykeroz Reason: 1


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



I will also include a debounce as there is likely to be some noise where this will be used on completion. Would I be right in thinking that a switch statement is the one to use to get the screen to display the information from different sensors?
Hi,

Yes you could use a switch statement, it certainly will keep your sketch modular and easy to read.
Code: [Select]
switch (buttonPushCounter) {
   case 0:
     //display default screen
     break;
   case 1:
     //display screen for if the button has been pressed once
     break;
   case 1:
     //display screen for if the button has been pressed twice
     break;
   default: // if it's not one of the specific values you have screens for, reset to default
     buttonPushCounter = 0;
     // and then probably you'd need to do something to tell it to re-test
     // the value of buttonPushCounter so it refreshes the display, or just
     // let it catch up in the next iteration of loop() if there's no lengthy
     // delay() in the loop.
     break;
 }

If you are planning on extending the project in future you just insert an additional 'known' state for buttonPushCounter to test for and it won't upset any of your other code.  

Note that unless you reset the buttonPushCounter value to zero each time it runs through this switch statement, it's not really storing the number of buttons clicks in a row, but the menu state, so it will allow the user to navigate a screen at a time and then loop back to the first one.  Not sure if that's what you're after...

Cheers !
Geoff
"There is no problem so bad you can't make it worse"
- retired astronaut Chris Hadfield

surfer_crx

Thanks for the advice, your explanation is exactly what I was looking for. I have been trying to write/run a switch statement for a couple of hours now with no joy, think I may be in need of switching off and on again  :)

Go Up