airpump/CS1237气泵参考代码.txt

70 lines
1.3 KiB
Plaintext

int ndrdy = A4;
int clck = A5;
int pwm = 3;
void setup() {
Serial.begin(115200);
pinMode(pwm, OUTPUT);
while (!Serial) {}
digitalWrite(ndrdy, LOW);
pinMode(ndrdy, INPUT);
digitalWrite(clck, LOW);
pinMode(clck, OUTPUT);
while (digitalRead(ndrdy)) {}
while (!digitalRead(ndrdy)) {}
}
long readADC()
{
if (digitalRead(ndrdy))
return -1;
long result = 0;
for (int i = 0; i < 24; i++)
{
digitalWrite(clck, HIGH);
delayMicroseconds(1);
int new_bit = digitalRead(ndrdy);
digitalWrite(clck, LOW);
delayMicroseconds(1);
result <<= 1;
result |= new_bit;
}
for (int i = 0; i < 3; i++)
{
digitalWrite(clck, HIGH);
delayMicroseconds(1);
digitalWrite(clck, LOW);
delayMicroseconds(1);
}
return result;
}
void loop() {
long adcValue = readADC();
Serial.print("ADC reading: ");
Serial.print(adcValue);
if (adcValue >= 3400000)
{
delay(500);
analogWrite(pwm,0);
Serial.println(" End of Run ");
delay(500);
}
else
{
if (is_run == true)
{
analogWrite(pwm,255);
delay(500);
}
}
}