Skip to content

Instantly share code, notes, and snippets.

@cpyarger
Created April 12, 2021 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpyarger/75e7f5ed7d93b69c7a8922c841bb8bb2 to your computer and use it in GitHub Desktop.
Save cpyarger/75e7f5ed7d93b69c7a8922c841bb8bb2 to your computer and use it in GitHub Desktop.
bit ed square wave count
const byte INPUT_PIN = 3;
long maxCountTime = 2000;
long nowTime = 0;
int waveState = 0;
int waveCount = 0;
int waveHigh = 0;
void setup() {
// put your setup code here, to run once:
pinMode(INPUT_PIN, INPUT);
Serial.begin(9600);
}
// ================== functions =====================
// int waitForHigh(maxCountTime) {
int doed() {
// wave is low
long lowStart = millis();
long lowRun = 0;
int waitOut;
int highOK = 0;
while (highOK == 0)
{
if (waitOut < 0)
{
break;
}
nowTime = millis();
lowRun = nowTime - lowStart;
highOK = (digitalRead(INPUT_PIN));
while (waitOut >= 0)
{
waitOut = maxCountTime - lowRun;
}
}
return highOK;
}
// ========================================== main loop ================
void loop() {
Serial.println("Home");
waveCount = 0;
nowTime = millis();
waveHigh = (digitalRead(INPUT_PIN));
if (waveHigh < 1)
{
Serial.println(waveHigh);
// wave is low
// waveState = waitForHigh(maxCountTime );
waveState = doed();
waveHigh = waveState;
}
Serial.println( waveHigh);
Serial.println("end of script");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment