Arduinot oskab keegi progeda?
#7
Kood mis töötab aga väike küsimus : Kuidas saab panna rea 0,00 EUR-i 5 kohta peale koma kui liitri hind on 0,00276 €-i ?


#include "LiquidCrystal.h"
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

// which pin to use for reading the sensor? can use any pin!
#define FLOWSENSORPIN 2


// count how many pulses!
volatile uint16_t pulses = 0;
// track the state of the pulse pin
volatile uint8_t lastflowpinstate;
// you can try to keep time of how long it is between pulses
volatile uint32_t lastflowratetimer = 0;
// and use that to calculate a flow rate
volatile float flowrate;
// Interrupt is called once a millisecond, looks for any pulses from the sensor!
SIGNAL(TIMER0_COMPA_vect) {
uint8_t x = digitalRead(FLOWSENSORPIN);

if (x == lastflowpinstate) {
lastflowratetimer++;
return; // nothing changed!
}

if (x == HIGH) {
//low to high transition!
pulses++;
}
lastflowpinstate = x;
flowrate = 1000.0;
flowrate /= lastflowratetimer; // in hertz
lastflowratetimer = 0;
}

void useInterrupt(boolean v) {
if (v) {
// Timer0 is already used for millis() - we'll just interrupt somewhere
// in the middle and call the "Compare A" function above
OCR0A = 0xAF;
TIMSK0 |= _BV(OCIE0A);
} else {
// do not call the interrupt function COMPA anymore
TIMSK0 &= ~_BV(OCIE0A);
}
}

void setup() {
Serial.begin(9600);
Serial.print("Flow sensor test!");
lcd.begin(16, 2);

pinMode(FLOWSENSORPIN, INPUT);
digitalWrite(FLOWSENSORPIN, HIGH);
lastflowpinstate = digitalRead(FLOWSENSORPIN);
useInterrupt(true);
}

void loop() // run over and over again
{
lcd.setCursor(0, 0);
lcd.print(pulses, DEC); lcd.print("Liitrit");


Serial.println(pulses, DEC); Serial.print("Liitrit ");


float liters = pulses;
liters *= 0.00276;



Serial.print(liters); Serial.println(" EUR-i");
lcd.setCursor(0, 1);
lcd.print(liters); lcd.print(" EUR-i");

delay(100);
}
Vasta


Sõnumeid selles teemas
Arduinot oskab keegi progeda? - Autor: insippo - 10-02-2013, 04:55 PM
RE: Arduinot oskab keegi progeda? - Autor: felch - 11-02-2013, 08:54 AM
RE: Arduinot oskab keegi progeda? - Autor: insippo - 11-02-2013, 09:04 AM
RE: Arduinot oskab keegi progeda? - Autor: bloody-orc - 11-02-2013, 02:44 PM
RE: Arduinot oskab keegi progeda? - Autor: insippo - 11-02-2013, 04:12 PM
RE: Arduinot oskab keegi progeda? - Autor: insippo - 11-02-2013, 08:51 PM
RE: Arduinot oskab keegi progeda? - Autor: insippo - 12-02-2013, 08:26 PM
RE: Arduinot oskab keegi progeda? - Autor: madis - 13-02-2013, 01:05 AM

Alamfoorumi hüpe:


Kasutaja, kes vaatavad seda teemat: 1 külali(st)ne