My code works fine for each part individually but i dont know how to comine it into one.. i can post what i have done already but i dont even know where to start to get it working together.
This is the code for the rotary encoder
int pulses, A_SIG=0, B_SIG=1;
void setup(){ attachInterrupt(0, A_RISE, RISING); attachInterrupt(1, B_RISE, RISING); Serial.begin(9600); }//setup
void loop(){
}
void A_RISE(){ detachInterrupt(0); A_SIG=1;
if(B_SIG==0) pulses++;//moving forward if(B_SIG==1) pulses--;//moving reverse Serial.println(pulses); attachInterrupt(0, A_FALL, FALLING); }
void A_FALL(){ detachInterrupt(0); A_SIG=0;
if(B_SIG==1)
pulses++;//moving forward
if(B_SIG==0)
pulses--;//moving reverse
Serial.println(pulses);
attachInterrupt(0, A_RISE, RISING);
}
void B_RISE(){ detachInterrupt(1); B_SIG=1;
if(A_SIG==1) pulses++;//moving forward if(A_SIG==0) pulses--;//moving reverse Serial.println(pulses); attachInterrupt(1, B_FALL, FALLING); }
void B_FALL(){ detachInterrupt(1); B_SIG=0;
if(A_SIG==0) pulses++;//moving forward if(A_SIG==1) pulses--;//moving reverse Serial.println(pulses); attachInterrupt(1, B_RISE, RISING); }
and this is the code for the flow meter and oled
// If using software SPI (the default case):
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
/* Uncomment this block to use hardware SPI
Adafruit_SSD1306 display(OLED_DC, OLED_RESET, OLED_CS); */
// 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!"); display.begin(SSD1306_SWITCHCAPVCC); display.display(); delay(2000); display.clearDisplay();
pinMode(FLOWSENSORPIN, INPUT); digitalWrite(FLOWSENSORPIN, HIGH); lastflowpinstate = digitalRead(FLOWSENSORPIN); useInterrupt(true); }
void loop() // run over and over again { display.setTextSize(2); display.setTextColor(WHITE); display.setCursor(0,0); display.println("Pulses:");display.println(pulses, DEC); float liters = pulses; liters /= 5.5; liters /= 60.0; display.print(liters);
display.println(" Liters "); display.display(); display.clearDisplay(); }
Both pieces of code i have taken from other peoples projects and edited to work with my hardware.