Hello people of the interwebs.
I have been working a project for the last 10 weeks. I am trying to get let a rgb led strip lightup when a generator is spinned. It worked all fine and dandy. But i wanted to try it with a waterflow sensor instred of a electromotor(thegenerator). And its not working with my orginal code. I got the found some code on the internet and it works but i have no idea how this peace of code works. If someone could explain this peace of code to me that would really help a lot.
this is the image of the waterflow sensor if you were wondering
this is the peace of code that came with it
/*
YF‐ S201 Water Flow Sensor
Water Flow Sensor output processed to read in litres/hour
Adaptation Courtesy: www.hobbytronics.co.uk
*/
volatile int flow_frequency; // Measures flow sensor pulses
unsigned int l_hour; // Calculated litres/hour
unsigned char flowsensor = 2; // Sensor Input
unsigned long currentTime;
unsigned long cloopTime;
void flow () // Interrupt function
{
flow_frequency++;
}
void setup()
{
pinMode(flowsensor, INPUT);
digitalWrite(flowsensor, HIGH); // Optional Internal Pull-Up
Serial.begin(9600);
attachInterrupt(0, flow, RISING); // Setup Interrupt
sei(); // Enable interrupts
currentTime = millis();
cloopTime = currentTime;
}
void loop ()
{
currentTime = millis();
// Every second, calculate and print litres/hour
if(currentTime >= (cloopTime + 1000))
{
cloopTime = currentTime; // Updates cloopTime
// Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
l_hour = (flow_frequency * 60 / 7.5); // (Pulse frequency x 60 min) / 7.5Q = flowrate in L/hour
flow_frequency = 0; // Reset Counter
Serial.print(l_hour, DEC); // Print litres/hour
Serial.println(" L/hour");
}
}
if you are wondering what my code looks like that i make to control the rgb led strip this is it. If there is anything you say could be done better or could improve please let me know
//generator pins
int GeneratorPin1 = 2;
int GeneratorPin2 = A1;
//buttonpin
int ButtonPin = 1;
//RGB verlichting pins
#define REDPIN 6
#define GREENPIN 10
#define BLUEPIN 11
//include timer & storage of values
#include <elapsedMillis.h>
#include <EEPROM.h>
//generator1 values
int GeneratorValue1 = 0;
int GeneratorMin1 = 0;
int GeneratorMax1 = 0;
//generator2 values
int GeneratorValue2 = 0;
int GeneratorMin2 = 0;
int GeneratorMax2 = 0;
//generators saved values
int GeneratorMax1Save;
int GeneratorMax2Save;
//button values
int ButtonCurrent = 0;
int ButtonCurrent1 = 0;
//set elapsedmillis to time elapsed
elapsedMillis timeElapsed;
//run void setup 1x
void setup()
{
Serial.begin(9600);
Serial.print("Pre-Setup Compleet\n");
//in-built led is output-ing
pinMode(LED_BUILTIN, OUTPUT);
//in-built led is low
digitalWrite(LED_BUILTIN, LOW);
//Button Input pin
pinMode(ButtonPin, INPUT);
//RGB Output pin
pinMode(REDPIN, OUTPUT);
pinMode(GREENPIN, OUTPUT);
pinMode(BLUEPIN, OUTPUT);
Serial.print("Pinmodes Set\n");
//see if data is stored above 0 then skip to
//Dataread();
//run buttonswitch setup
ButtonSwitch();
//run calibration setup
Calibration();
//rerun calibation for other water source
//run buttonswitch setup
ButtonSwitch();
//run calibration setup
Calibration();
//write data to save variables
//Datawrite();
Serial.print("Starting loop\n");
}
void loop()
{
//starts RGB values
int r, g, b;
//read generator 1 pin value
GeneratorValue1 = analogRead(GeneratorPin1);
//read generator 2 pin value
GeneratorValue2 = analogRead(GeneratorPin2);
//remap generator1 value
int GeneratorValue1map = map(GeneratorValue1, GeneratorMin1, GeneratorMax1, 0, 255);
int GeneratorValue2map = map(GeneratorValue2, GeneratorMin2, GeneratorMax2, 0, 255);
//constrain generator values
int GeneratorValue1con = constrain(GeneratorValue1map, 0, 255);
int GeneratorValue2con = constrain(GeneratorValue2map, 0, 255);
//set rgb values
r = GeneratorValue1con;
g = 0;
b = GeneratorValue2con;
//output rgb values
analogWrite(REDPIN, r);
analogWrite(GREENPIN, g);
analogWrite(BLUEPIN, b);
Serial.print("red: ");
Serial.println(r);
Serial.print("blue: ");
Serial.println(b);
//read button value
//if button value changed run resetvalue
if(ButtonCurrent1 != ButtonCurrent)
{
resetvalues();
Serial.print("resetting");
}
//delay
delay(1000);
}
void Calibration()
{
Serial.print("Calibration starting\n");
timeElapsed = 0;
//herhaal codering voor 10000ms
while (timeElapsed < 10000)
{
//set generator values met analogread
GeneratorValue1 = analogRead(GeneratorPin1);
//set generator max
if (GeneratorValue1 > GeneratorMax1)
{
GeneratorMax1 = GeneratorValue1;
}
------------------------------------------------
//set generator values met analogread
GeneratorValue2 = analogRead(GeneratorPin2);
//set generator max
if (GeneratorValue2 > GeneratorMax2)
{
GeneratorMax2 = GeneratorValue2;
}
}
Serial.print("Calibration finished\n");
Serial.println(GeneratorMax1);
Serial.println(GeneratorMax2);
}
void ButtonSwitch()
{
Serial.print("ButtonSwitch started\n");
//set buttoncurrent to high or low
ButtonCurrent = digitalRead(ButtonPin);
//buttoncurrent en buttoncurrent1 same value
ButtonCurrent1 = ButtonCurrent;
//run while buttoncurrent is same as buttoncurrent1
while(ButtonCurrent == ButtonCurrent1)
{
Serial.print("Press Button\n");
//while flashing press button
//in-built led high
digitalWrite(LED_BUILTIN, HIGH);
//read button value
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
//repeat while buttoncurrent value is same as buttoncurrent1value
ButtonCurrent1 = digitalRead(ButtonPin);
//wait 1000ms
delay(1000);
}
//sketch finished
//set buttoncurrent to high or low
ButtonCurrent = digitalRead(ButtonPin);
//buttoncurrent en buttoncurrent1 same value
ButtonCurrent1 = ButtonCurrent;
Serial.print("Buttonsetup compleet\n");
}
void Datawrite()
{
//create saveble formate
GeneratorMax1Save = GeneratorMax1 / 4;
GeneratorMax2Save = GeneratorMax2 / 4;
//write generatormax
EEPROM.write(GeneratorMax1, GeneratorMax1Save);
EEPROM.write(GeneratorMax2, GeneratorMax2Save);
}
void Dataread()
{
Serial.print("Running dataread\n");
//start reading from the first byte
byte GeneratorMax1Save;
byte GeneratorMax2Save;
//generatormax read
GeneratorMax1Save = EEPROM.read(GeneratorMax1);
GeneratorMax2Save = EEPROM.read(GeneratorMax2);
GeneratorMax1 = GeneratorMax1Save * 4;
GeneratorMax2 = GeneratorMax2Save * 4;
if (GeneratorMax1 > 0)
{
if (GeneratorMax2 > 0)
{
//set buttoncurrent to high or low
ButtonCurrent = digitalRead(ButtonPin);
//buttoncurrent en buttoncurrent1 same value
ButtonCurrent1 = ButtonCurrent;
//run void loop and skip calibration
loop();
}
}
}
void resetvalues()
{
//set time elapsed to 0
timeElapsed = 0;
//set buttoncurrent to high or low
ButtonCurrent = digitalRead(ButtonPin);
//buttoncurrent en buttoncurrent1 same value
ButtonCurrent1 = ButtonCurrent;
while (timeElapsed < 10000)
{
digitalWrite(LED_BUILTIN, HIGH);
ButtonCurrent1 = digitalRead(ButtonPin);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
if(ButtonCurrent1 != ButtonCurrent)
{
//set generator values met analogread
GeneratorValue1 = analogRead(GeneratorPin1);
//generator1 values
int GeneratorValue1 = 0;
int GeneratorMin1 = 0;
int GeneratorMax1 = 0;
//set generator values met analogread
GeneratorValue2 = analogRead(GeneratorPin1);
//generator2 values
int GeneratorValue2 = 0;
int GeneratorMin2 = 0;
int GeneratorMax2 = 0;
//run buttonswitch setup
ButtonSwitch();
//run calibration setup
Calibration();
//rerun calibation for other water source
//run buttonswitch setup
ButtonSwitch();
//run calibration setup
Calibration();
//write data to save variables
Datawrite();
}
}
loop();
}
ps: The electricmotor i have been using the last couple of weeks only has a + and a -, as you can see the waterflow sensor has 3. and i am not sure how to combine the 2 codes to make the waterflow sensor replace the "generators"
If your wandering what am i talking about and i haven't been clear with my post, please let me know, and i will try to make my self more clear.
Also if i write a simple code like
int flowsensor = 2; // Sensor Input
void setup()
{
pinMode(flowsensor, INPUT);
Serial.begin(9600);
}
void loop ()
{
Serial.print(analogRead(flowsensor));
Serial.println(" L/hour");
delay(1000);
}
it just prints 264 L/hour of sometimes 263L/hour at idle. and when it spins it does not have any effect