Pages: 1 [2] 3   Go Down
Author Topic: Liquid Usage with a meter from Adafruit  (Read 5476 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 227
Posts: 14007
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Pesh,
Please add code tags to your posts. It is the # button above the smileys.
The code will become more readable and problems are easier detected.

please also modify your existing posts..
Logged

Rob Tillaart

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

uk
Offline Offline
Jr. Member
**
Karma: 3
Posts: 81
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


 I think ive modifyed the post as you asked. Sorry didnt see the hash tag before.

 Another question i want to use the above code a couple of times over on the same arduino can i use the name rims, HLT MLt ect

 Like

 
Code:
byte RIMSsensorInterrupt = 0;  // 0 = pin 2; 1 = pin 3
byte RIMSsensorPin       = 2;


byte HLTsensorInterrupt = 0;  // 0 = pin 2; 1 = pin 3
byte HLTsensorPin       = 2;


byte MLTsensorInterrupt = 0;  // 0 = pin 2; 1 = pin 3
byte MLTsensorPin       = 2;


 And then just declare each naming convention ?

 Pesh
Logged

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3080
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the line with void setup() on it, should not have a semicolon at the end.
Logged

uk
Offline Offline
Jr. Member
**
Karma: 3
Posts: 81
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset



 Thank you


 Pesh
Logged

uk
Offline Offline
Jr. Member
**
Karma: 3
Posts: 81
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


 Where im at so far

Code:
#include <TFT.h>  // Arduino TFT library
#include <SPI.h>

// pin definition for the Uno
#define cs   10
#define dc   9
#define rst  8

// create an instance of the library
TFT TFTscreen = TFT(cs, dc, rst);

// Specify the pins for the two counter reset buttons and indicator LED

byte sensorInterrupt = 0;  // 0 = pin 2; 1 = pin 3
byte sensorPin       = 2;

// The hall-effect flow sensor outputs approximately 4.5 pulses per second per
// litre/minute of flow.
float calibrationFactor = 4.5;

volatile byte pulseCount; 

float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitresA;
unsigned long totalMilliLitresB;

unsigned long oldTime;

void setup()
{
  TFTscreen.begin();
  TFTscreen.background(0,0,0);
  TFTscreen.stroke(255,0,0);
TFTscreen.setTextSize(3);
  //TFTscreen.setCursor(0, 0);
  TFTscreen.text("               ",0,0 );
  //TFTscreen.setCursor(0, 1);
  TFTscreen.text("                ",0,0);
 
  // Initialize a serial connection for reporting values to the host
  Serial.begin(9600);
   
 
  pinMode(sensorPin, INPUT);
  digitalWrite(sensorPin, HIGH);

  pulseCount        = 0;
  flowRate          = 0.0;
  flowMilliLitres   = 0;
  totalMilliLitresA = 0;
  totalMilliLitresB = 0;
  oldTime           = 0;

  // The Hall-effect sensor is connected to pin 2 which uses interrupt 0.
  // Configured to trigger on a FALLING state change (transition from HIGH
  // state to LOW state)
  attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}

/**
 * Main program loop
 */
void loop()
{
  {
    totalMilliLitresA = 0;
    //TFTscreen.setCursor(0, 1);
    //TFTscreen.print("               ");
  }

  {
    totalMilliLitresB = 0;
    //TFTscreen.setCursor(8, 1);
    TFTscreen.text("Flow",0,0);
  }
 
  {
  }
 
  if((millis() - oldTime) > 1000)    // Only process counters once per second
  {
    // Disable the interrupt while calculating flow rate and sending the value to
    // the host
    detachInterrupt(sensorInterrupt);
    //TFTscreen.setCursor(15, 0);
    //TFTscreen.print("*");
   
    // Because this loop may not complete in exactly 1 second intervals we calculate
    // the number of milliseconds that have passed since the last execution and use
    // that to scale the output. We also apply the calibrationFactor to scale the output
    // based on the number of pulses per second per units of measure (litres/minute in
    // this case) coming from the sensor.
    flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
   
    // Note the time this processing pass was executed. Note that because we've
    // disabled interrupts the millis() function won't actually be incrementing right
    // at this point, but it will still return the value it was set to just before
    // interrupts went away.
    oldTime = millis();
   
    // Divide the flow rate in litres/minute by 60 to determine how many litres have
    // passed through the sensor in this 1 second interval, then multiply by 1000 to
    // convert to millilitres.
    flowMilliLitres = (flowRate / 60) * 1000;
   
    // Add the millilitres passed in this second to the cumulative total
    totalMilliLitresA += flowMilliLitres;
    totalMilliLitresB += flowMilliLitres;
 
    // During testing it can be useful to output the literal pulse count value so you
    // can compare that and the calculated flow rate against the data sheets for the
    // flow sensor. Uncomment the following two lines to display the count value.
    //Serial.print(pulseCount, DEC);
    //Serial.print("  ");
   
    // Write the calculated value to the serial port. Because we want to output a
    // floating point value and print() can't handle floats we have to do some trickery
    // to output the whole number part, then a decimal point, then the fractional part.
    unsigned int frac;
   
    // Print the flow rate for this second in litres / minute
    Serial.print(int(flowRate));  // Print the integer part of the variable
    Serial.print(".");             // Print the decimal point
    // Determine the fractional part. The 10 multiplier gives us 1 decimal place.
    frac = (flowRate - int(flowRate)) * 10;
    Serial.print(frac, DEC) ;      // Print the fractional part of the variable

    // Print the number of litres flowed in this second
    Serial.print(" ");             // Output separator
    Serial.print(flowMilliLitres);

    // Print the cumulative total of litres flowed since starting
    Serial.print(" ");             // Output separator
    Serial.print(totalMilliLitresA);
    Serial.print(" ");             // Output separator
    Serial.println(totalMilliLitresB);
   
    TFTscreen.setCursor(0, 0);
    TFTscreen.print("                ");
    TFTscreen.setCursor(0, 0);
    //TFTscreen.print("Flow: ");
    if(int(flowRate) < 10)
    {
      TFTscreen.print("              ");
    }
    TFTscreen.print((int)flowRate);   // Print the integer part of the variable
    TFTscreen.print('.');             // Print the decimal point
    TFTscreen.print(frac, DEC) ;      // Print the fractional part of the variable
    TFTscreen.print(" L");
    TFTscreen.print("/Min");
   
    TFTscreen.setCursor(0, 1);
    TFTscreen.print(int(totalMilliLitresA / 1000));
    TFTscreen.print("L");
    TFTscreen.setCursor(8, 1);
    TFTscreen.print(int(totalMilliLitresB / 1000));
    TFTscreen.print("L");
    TFTscreen.setCursor(0,0);
    TFTscreen.print("                ");
    TFTscreen.setCursor(0,1);
    TFTscreen.print("                ");
    // Reset the pulse counter so we can start incrementing again
    pulseCount = 0;
   
    // Enable the interrupt again now that we've finished sending output
    attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  }
}

/**
 * Invoked by interrupt0 once per rotation of the hall-effect sensor. Interrupt
 * handlers should be kept as small as possible so they return quickly.
 */
void pulseCounter()
{
  // Increment the pulse counter
  pulseCount++;
}

 This wont display properly on a tft lcd display, i think i need to change the ints and floats to strings an use the drawstring command.

 will this work is there an easyer way i cant seam to get the floats converted to ints any way

 Any help appreciated

 Pesh
Logged

Dee Why NSW
Offline Offline
Edison Member
*
Karma: 48
Posts: 2307
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That code looks much the same is that which I use from Freetronics and Jaycar. It might pay to get rid of the comments and anything that you don't need for the exercise. The print section is

Code:
    unsigned int frac;
     frac = (flowRate - int(flowRate)) * 10;

    lcd.setCursor(6, 0);

    if(int(flowRate) < 10)
    {
      lcd.print(" ");
    }
    lcd.print((int)flowRate);   // Print the integer part of the variable
    lcd.print('.');             // Print the decimal point
    lcd.print(frac, DEC) ;      // Print the fractional part of the variable

I bet you don't need to convert to strings but, in the unlikely event, here is a string conversion for three floats together.

Code:

Setup
  stringOne = String("E");
  stringTwo = String(",");
  stringThree = String("\n");

Loop
 dtostrf(InTemp,4, 2, strIn);
dtostrf(OutTemp,4, 2, strOut);
dtostrf(diff,4, 2, strdiff);
stringFour = stringOne + strIn + stringTwo + strOut + stringTwo + strdiff + stringThree;
Serial.println(stringFour);           // output to bluetooth
Logged

uk
Offline Offline
Jr. Member
**
Karma: 3
Posts: 81
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 
 Thank you very much.

 Im finding that the tft screen wort work with comands like setcursor, i think because the tft screen isnt just a 16,2 it can print all over the screen, but what this means is all the text is on top of each other, so im having to use TFTscreen.text("",,) to print any text and this wont print the float.
 
I havent found a way to clear the screen other than to change the text colour to background colour so im guessing there isnt a text buffer to flush.

 Pesh

Logged

Dee Why NSW
Offline Offline
Edison Member
*
Karma: 48
Posts: 2307
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

so im having to use TFTscreen.text("",,) to print any text and this wont print the float.
 
I havent found a way to clear the screen other than to change the text colour to background colour so im guessing there isnt a text buffer to flush.

That doesn't sound right at all. I know of three different types of cursor

1.  Character/line
2. PIxel/line
3. Pixel/pixel

BUT when you print text, the same format methods apply to all - you either overprint with characters, or overprint with blanks, but one or the other will fix anything. If you cannot do this, I can only conclude you are not using fonts and are drawing text by pixel address, but i have never heard of such a thing. It seems to me that you have a rotten library, or no library. You might try Henning Karlsen and see if you can use his. I don't know much about TFT and his is the only stuff I use, but I send my TFT floats essentially just like I would to anything else. I think it is a fair bet that the only time you send strings for data instead of floats is when the receiver specifically demands it, and I only know of one instance of that.
Logged

uk
Offline Offline
Jr. Member
**
Karma: 3
Posts: 81
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


 Yeah sorry im very new to this and im learning as i go, ive found that i can use a variety of text comands so i can use TFTscreen.text("",0,0) (or what ever location) to set the cursor then use TFTscreen.print and the text follows where the cursor was from the last line, so one problem solved, ims till not sure about the text flush but il keep tryin.

   Pesh
Logged

Dee Why NSW
Offline Offline
Edison Member
*
Karma: 48
Posts: 2307
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

im learning as i go,

Yes, we all are...............

Quote
s till not sure about the text flush but il keep tryin.


I believbe that shouldn't be necessary. You should be able to do it all with    TFTscreen.text(variabel,x,y) or the like.
Logged

uk
Offline Offline
Jr. Member
**
Karma: 3
Posts: 81
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


 Ive seen another post by some one having the same problems, they fixed them by using the nightly build, so i started with that but on windows 8 which didnt work so i changed back to windows 7 still using the nightly build and saw the same text over write problem, ive now gone back to the 1.5.8-r2 build but im seeing the same thing one number on top of another, other than that the code looks to be working ok so far .
there is
 
Code:
display.clearDisplay();

 but this doesnt work with the TFT

 The examples ive found all change the text colour (using TFTscreen.stroke(0,0,0) ) to the background colour, im still hopin theres a beter way.
 
 Pesh
Logged

uk
Offline Offline
Jr. Member
**
Karma: 3
Posts: 81
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


 The only way if found to over write the text is to set a delay the change the text colour to match the backgroung colour then set a delay then display the same text then set a delay then change the tect colour back, this caused the screen to flash and only over writes as the screen flashes, so not a good solution.

 If any one has any clues as to why this is happening please shout up, ive double checked the wireing an its correct as far as i can see.

 Pesh
Logged

Dee Why NSW
Offline Offline
Edison Member
*
Karma: 48
Posts: 2307
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm sure this has nothing to do with Windows. It could have everything to do with the library. I now see that you do actually use one but, as I said, you might try ditching it for something better.

I don't have much experience with TFT displays and it is faintly possible that you are simply not using the library you have in the proper manner, but that itself implies that something that should easy and obvious isn't . Maybe it is in some sort of graphics mode, unsuitable for data as text.

The bottom line is this: you have a problem, but I don't. The only difference I can see is the library.
Logged

uk
Offline Offline
Jr. Member
**
Karma: 3
Posts: 81
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


 Sorry ive been tryin to work through this one problem at a time the main one being my lack of knowledge other have said the fixed this problem using other versions of the arduino  build, ive been reluctant to try another library till not because i expected it would use different commands and that means re-writing the sketch from scratch and it hasnt been easy getting this far for every step i take im taking another 2 back, but it  seams but im getting better,

 Do you mean to try the #include<UTFT.h> library ?

 Changing the library does seam like the next logical step, if i use both TFT and UTFT will they conflict ?
 not sure that im gonna need both but best to ask

 Thanks for the help so far

 Pesh
Logged

Dee Why NSW
Offline Offline
Edison Member
*
Karma: 48
Posts: 2307
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you mean to try the #include<UTFT.h> library ?

 Changing the library does seam like the next logical step, if i use both TFT and UTFT will they conflict ?
 not sure that im gonna need both but best to ask


Yes, the UTFT library from Henning Karlsen. The U stands for universal, and particular settings for display type are done in your programme. I only suggest this because, as far as handling text is concerned, it does what I expect it to do, namely, what every other LCD library does, which is definitely not what you are getting.  The Henning Karlsen library is extremely well-documented, including a list of suitable displays. It so happens that my TFT is not included, but I still got it to work.

Both TFT and UTFT can live in the libraries subdir but using them simultaneously can't be a good idea. 
Logged

Pages: 1 [2] 3   Go Up
Jump to: