Cap sensor Help

Hi All. I have come accross a very nice project to read fuel level using a cap . sensor. i have build the unit and works fine. can any assist me with the following : I want to measure the drop in fuel say in 60 sec eg if the level was say half full the the drop in 60 sec was greater than 1l then i want a led to go on. Can this be done by adapting the ff code. if so can any one point me in the right direction

Regards
Ivan

//Capacitive Liquid Sensor

#include <CapacitiveSensor.h>
#include <LiquidCrystal.h>

//This is to set the size of the lcd
const int numRows =4;
const int numCols = 20;

//This sets the pins for the lcd (RS, Enable, data 0-7)
LiquidCrystal lcd (53, 52, 51, 50, 49, 48,47,46,45,44);
#define Tempin 0x48
#define Tempout 0x49
CapacitiveSensor cs_22_23 = CapacitiveSensor(22, 23);


uint8_t block[8] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};

uint8_t tl[8] = {0x0F,0x08,0x08,0x08,0x08,0x08,0x0F,0x0F};
uint8_t tr[8] = {0x16,0x11,0x11,0x11,0x11,0x11,0x1D,0x15};
uint8_t bl[8] = {0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x1F};
uint8_t br[8] = {0x15,0x15,0x15,0x15,0x15,0x15,0x12,0x18};
void setup() {
  lcd.begin(numRows, numCols);
  lcd.createChar(4, tl);
  lcd.createChar(5, tr);
  lcd.createChar(6, bl);
  lcd.createChar(7, br);
 
  lcd.setCursor(18,0);
  lcd.write(4 );
  lcd.setCursor(19,0);
  lcd.write(5);
  lcd.setCursor(18,1);
  lcd.write(6);
  lcd.setCursor(19,1);
  lcd.write(7);

  lcd.setCursor(0,2);
  lcd.write("Fuel ");
  lcd.setCursor(0,3);
  lcd.write("E");
}


void loop() {
  long fuel;
  lcd.createChar(2, block);
 
  long start = millis();
  fuel = cs_22_23.capacitiveSensorRaw(200);
 
 
  //Temratue makes a bit of a difrence so let it run for 5 min before tuning.
  //Adjust this number so that the output is as close to zero as posible.
  fuel = fuel - 7200;
  //Then fill up the conataner
  //Un-comment and adjust this so that the output, when the container is full,
  //is as close to 100 as possible.
  //fuel = fuel / 93;
 
  lcd.setCursor(0,0);
  lcd.print("      ");
  lcd.setCursor(0,0);
  lcd.write(fuel);
 
  if (fuel >= 6) {
    lcd.setCursor(1,3);
    lcd.write(2);
  } else {
    lcd.setCursor(1,3);
    lcd.print(" ");
  }
  if (fuel >= 12) {
    lcd.setCursor(2,3);
    lcd.write(2);
  } else {
    lcd.setCursor(2,3);
    lcd.write(" ");
  }
  if (fuel >= 17) {
    lcd.setCursor(3,3);
    lcd.write(2);
  } else {
    lcd.setCursor(3,3);
    lcd.write(" ");
  }
  if (fuel >= 23) {
    lcd.setCursor(4,3);
    lcd.write(2);
  } else {
    lcd.setCursor(4,3);
    lcd.write(" ");
  }
  if (fuel >= 28) {
    lcd.setCursor(5,3);
    lcd.write(2);
  } else {
    lcd.setCursor(5,3);
    lcd.write(" ");
  }
  if (fuel >= 34) {
    lcd.setCursor(6,3);
    lcd.write(2);
  } else {
    lcd.setCursor(6,3);
    lcd.write(" ");
  }
  if (fuel >= 39) {
    lcd.setCursor(7,3);
    lcd.write(2);
  } else {
    lcd.setCursor(7,3);
    lcd.write(" ");
  }
  if (fuel >= 44) {
    lcd.setCursor(8,3);
    lcd.write(2);
  } else {
    lcd.setCursor(8,3);
    lcd.write(" ");
  }
  if (fuel >= 50) {
    lcd.setCursor(9,3);
    lcd.write(2);
  } else {
    lcd.setCursor(9,3);
    lcd.write(" ");
  }
  if (fuel >= 55) {
    lcd.setCursor(10,3);
    lcd.write(2);
  } else {
    lcd.setCursor(10,3);
    lcd.write(" ");
  }
  if (fuel >= 60) {
    lcd.setCursor(11,3);
    lcd.write(2);
  } else {
    lcd.setCursor(11,3);
    lcd.write(" ");
  }
  if (fuel >= 64) {
    lcd.setCursor(12,3);
    lcd.write(2);
  } else {
    lcd.setCursor(12,3);
    lcd.write(" ");
  }
  if (fuel >= 69) {
    lcd.setCursor(13,3);
    lcd.write(2);
  } else {
    lcd.setCursor(13,3);
    lcd.write(" ");
  }
  if (fuel >= 74) {
    lcd.setCursor(14,3);
    lcd.write(2);
  } else {
    lcd.setCursor(14,3);
    lcd.write(" ");
  }
  if (fuel >= 78) {
    lcd.setCursor(15,3);
    lcd.write(2);
  } else {
    lcd.setCursor(15,3);
    lcd.write(" ");
  }
  if (fuel >= 83) {
    lcd.setCursor(16,3);
    lcd.write(2);
  } else {
    lcd.setCursor(16,3);
    lcd.write(" ");
  }
  if (fuel >= 87) {
    lcd.setCursor(17,3);
    lcd.write(2);
  } else {
    lcd.setCursor(17,3);
    lcd.write(" ");
  }
  if (fuel >= 92) {
    lcd.setCursor(18,3);
    lcd.write(2);
  } else {
    lcd.setCursor(18,3);
    lcd.write(" ");
  }
  if (fuel >= 96) {
    lcd.setCursor(19,3);
    lcd.write("F");
  } else {
    lcd.setCursor(19,3);
    lcd.write(" ");
  }
 
  delay (50);
}

The basic logic might go something like this

// read the fuel level

if (millis()-last_time>=60000) {
    if (last_fuel-fuel >11) { 
      // turn the LED on
       digitalWrite(LED_PIN,ON);
 
    } else
       digitalWrite(LED_PIN,OFF);
    };
    last_time=millis();
    last_fuel=fuel;
};
// go on with the rest of the loop

Of course you have to declare the variables and initialize them to something sensible as part of setup().

Hi rw950431 Thank you for your reply. I can see what you are trying to say but i cant seem to get it to work .
ICan you please tell me if i set up the code right Thank you

Set up

CapacitiveSensor cs_22_23 = CapacitiveSensor(22, 23);
long fuel;
long start = millis();
long last_time = millis();
long last_fuel=fuel;

For the loop i tried like
[/code]

void loop() {
long fuel;
long start = millis();
long last_time = millis();

fuel = cs_22_23.capacitiveSensorRaw(200);

fuel = fuel-9200;
fuel = fuel / 100;
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,0);
lcd.print(fuel);
// lcd.createChar(2, block);

last_time=millis();
last_fuel=fuel;

if (millis()-last_time>=60000) {
if (last_fuel-fuel >11) {
// turn the LED on
digitalWrite(Led_PIN,LOW);

} else
digitalWrite(Led_PIN,HIGH);
}

//**************************bl1
if (fuel>=1) { //6
lcd.setCursor(1,1);
lcd.write(2);
}
else {
lcd.setCursor(1,1);
lcd.write(" ");
}
//

[/code]

you should not initialise last_fuel and last_time within the loop() function- what you have done at the start of the program looks good but is getting overridden by the stuff at the top of loop().

Try something like

CapacitiveSensor cs_22_23 = CapacitiveSensor(22, 23);
long fuel;
long start = millis();
long last_time = millis();
long last_fuel=-1;

//etc etc

void loop() {
 
  fuel = cs_22_23.capacitiveSensorRaw(200);
   
 fuel = fuel-9200;    
 fuel = fuel / 100;
 lcd.setCursor(0,0);
  lcd.print("      ");
  lcd.setCursor(0,0);
  lcd.print(fuel);
// lcd.createChar(2, block);

if (last_fuel<0) { last_fuel=fuel; };
 
if (millis()-last_time>=60000) {
  if (last_fuel-fuel >11) {
  // turn the LED on
  digitalWrite(Led_PIN,LOW);  

  } else
  digitalWrite(Led_PIN,HIGH);
  }
 last_time=millis();
 last_fuel=fuel;
}

// etc

Hi rw950431 Thank you for trying to help me i really appreciate this. I removed most of the code and am just trying to run the code you gave me yesterday. the code compiles but i can get it to do what it is supposed to do.m
the modified code looks like this

#include <CapacitiveSensor.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd (53, 52, 51, 50, 49, 48,47,46,45,44);

const int numRows =4;
const int numCols = 20;
uint8_t block[8] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
uint8_t tl[8] = {0x0F,0x08,0x08,0x08,0x08,0x08,0x0F,0x0F};
uint8_t tr[8] = {0x16,0x11,0x11,0x11,0x11,0x11,0x1D,0x15};
uint8_t bl[8] = {0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x1F};
uint8_t br[8] = {0x15,0x15,0x15,0x15,0x15,0x15,0x12,0x18};


CapacitiveSensor cs_22_23 = CapacitiveSensor(22, 23);
long fuel;
long start = millis();
long last_time = millis();
long last_fuel=-1;

const int Led_PIN = 12;
 
void setup() {
	
	
   pinMode(Led_PIN,OUTPUT);
     
  lcd.begin(20, 4);
  lcd.begin(numRows, numCols);
  lcd.createChar(4, tl);
  lcd.createChar(5, tr);
  lcd.createChar(6, bl);
  lcd.createChar(7, br);
  
  lcd.setCursor(18,0);
  lcd.write(4);
  lcd.setCursor(19,0);
  lcd.write(5);
  lcd.setCursor(18,1);
  lcd.write(6);
  lcd.setCursor(19,1);
  lcd.write(7);
    
  //lcd.setCursor(0,2);
  //lcd.print("Fuel ");
  lcd.setCursor(0,1);
  lcd.print("E");
  lcd.setCursor(17,1);
  lcd.print("F");
  lcd.setCursor(8,0);
  lcd.print("2");
  lcd.setCursor(4,0);
  lcd.print("1");
  lcd.setCursor(12,0);
  lcd.print("3");
  lcd.setCursor(16,0);
  lcd.print("4");
  }
  
void loop() {
	
	fuel = cs_22_23.capacitiveSensorRaw(200);
	
	fuel = fuel-9200;
	fuel = fuel / 100;
	lcd.setCursor(0,0);
	lcd.print("      ");
	lcd.setCursor(0,0);
	lcd.print(fuel);
	// lcd.createChar(2, block);

	if (last_fuel<0) { last_fuel=fuel; };
	
	if (millis()-last_time>=60000) {
		if (last_fuel-fuel >5) {
			// turn the LED on
			digitalWrite(Led_PIN,HIGH);

		} else
		digitalWrite(Led_PIN,LOW);
	}
	last_time=millis();
	last_fuel=fuel;
}

Like you can see it is just using what you supplied . The led state should be on if no fuel is used after 60 sec. i check the led to see if it is working and all other connections which is fine. im sure i missed out something along the lines.

Thank you

Only update last_time and last_fuel within the timing if-statement, otherwise there will never be enough difference to trigger the conditions.

void loop() {
 
 fuel = cs_22_23.capacitiveSensorRaw(200);
 
 fuel = fuel-9200;
 fuel = fuel / 100;
 lcd.setCursor(0,0);
 lcd.print("      ");
 lcd.setCursor(0,0);
 lcd.print(fuel);
 // lcd.createChar(2, block);

 if (last_fuel<0) { last_fuel=fuel; };
 
 if (millis()-last_time>=60000) {
 if (last_fuel-fuel >5) {
 // turn the LED on
 digitalWrite(Led_PIN,HIGH);

 } else digitalWrite(Led_PIN,LOW);
 last_time=millis();
 last_fuel=fuel;

 }
}