is this right?
It would have been better if you Auto Formatted it and posted it in code tags like this
int sensor = 1;
unsigned long startMillis; //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 1000; //the value is a number of milliseconds
const byte ledPin = 13; //using the built in LED
int lowstate = 0;
int hightstate = 0;
long ratio = 0;
void setup()
{
Serial.begin(9600);
pinMode(sensor, INPUT);
startMillis = millis(); //initial start time
}
void loop()
{
currentMillis = millis();
int state = digitalRead(sensor);
if (state == 1) hightstate = hightstate + 1;
if (state == 0) lowstate = lowstate + 1;
ratio = hightstate / (lowstate + hightstate);
if (currentMillis - startMillis >= period) //test whether the period has elapsed
{
Serial.println(state);
startMillis = currentMillis; //IMPORTANT to save the start time of the current LED state.
}
}
The idea looks OK but
1 - you don't need to count both the HIGH and LOW states
2 - do any calculations at the end of the period
3 - you should really count when the input becomes HIGH rather than when it is HIGH
4 - why are you printing state ?