我这部分代码有问题
if ( drop_count > 9 )
{
time_end = ( millis() ) / 1000;
//read time final
//cal final - first
timereal = time_end - time_start;
Serial.print( "time = " );
Serial.println( timereal );
drop_velo = 720 / timereal;
Serial.print( "drop velo = " );
Serial.println( drop_velo );
Serial.print( "dm = " );
Serial.println( dm );
//if(drop_velo<dml||drop_velo>dmh)
if ( drop_velo != dm )
{
Serial.print( "enter if" );
//digitalWrite(1,HIGH);delay(1000);}
tone( 1, 800, 1000 );
}
drop_count = 0;
}
如果它进入if ( drop_velo != dm )
块,它应该打印"enter if"
并且蜂鸣器会发出一些声音。可以正常打印"enter if"
,但蜂鸣器不起作用。我不知道我在做什么错。我只测试了蜂鸣器,而且效果很好。
这还是我的完整代码:
#include <Wire.h>
#include <LiquidCrystal_PCF8574.h>
#include <Keypad.h>
#include <StepperMotor.h>
LiquidCrystal_PCF8574 lcd( 0x27 );
StepperMotor motor( 10, 11, 12, 13 );
int motorSpeed = 1;
int motorSteps = 30;
#define led 13
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] =
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
String num;
int ml;
int df;
int m;
int dm;
int dmh;
int dml;
int count = 0;
int analogPin = A0;
int val = 0;
int drop_count = 0;
int drop_velo;
int countWater = 0;
unsigned long time_start = 0, time_end = 0;
int timereal;
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap( keys ), rowPins, colPins, ROWS, COLS );
void( * resetFunc )( void ) = 0;
void setup()
{
Serial.begin( 9600 );
pinMode( led, OUTPUT );
digitalWrite( led, LOW ); // turn off LED
motor.setStepDuration( motorSpeed );
pinMode( 1, OUTPUT );
lcd.setBacklight( 225 );
lcd.begin( 16, 2 );
while ( count = '0' )
{
lcd.setCursor( 0, 0 );
lcd.print( "(ml*dropfac)/min" );
//lcd.setCursor(0, 1);
//lcd.print("ml,drop fact,min");
char key = keypad.getKey();
if ( key == 'A' )
{
ml = num.toInt();
Serial.print( "ml:" );
Serial.println( ml );
num = '0';
lcd.clear();
lcd.setCursor( 0, 1 );
lcd.print( "ml:" );
lcd.print( ml );
lcd.blink();
}
else if ( key == 'B' )
{
df = num.toInt();
Serial.print( "drop factor:" );
Serial.println( df );
num = '0';
lcd.setCursor( 0, 1 );
lcd.print( "drop factor:" );
lcd.print( df );
lcd.blink();
}
else if ( key == 'C' )
{
m = num.toInt();
Serial.print( "min:" );
Serial.println( m );
num = '0';
lcd.clear();
lcd.setCursor( 0, 1 );
lcd.print( "min:" );
lcd.print( m );
lcd.blink();
}
else if ( key == 'D' )
{
delay( 1000 );
resetFunc();
}
else if ( key == '*' )
{
break;
}
else
{
num = num + String( key );
}
}
dm = ( ml * df ) / m;
Serial.print( "drop/min" );
Serial.println( dm );
lcd.clear();
lcd.print( "drop/min" );
lcd.print( dm );
dmh = 105 / 100 * dm;
dml = 95 / 100 * dm;
delay( 5000 );
}
void loop()
{
pinMode( 1, OUTPUT );
digitalWrite( led, HIGH ); // shows forward rotation
motor.step( -motorSteps );
//Read water
val = analogRead( analogPin );
if ( val > 100 )
{
drop_count = drop_count + 1;
}
Serial.println( drop_count );
//waterDrop++;
if ( drop_count == 1 )
{
//read time first
time_start = ( millis() ) / 1000;
}
if ( drop_count > 9 )
{
time_end = ( millis() ) / 1000;
//read time final
//cal final - first
timereal = time_end - time_start;
Serial.print( "time = " );
Serial.println( timereal );
drop_velo = 720 / timereal;
Serial.print( "drop velo = " );
Serial.println( drop_velo );
Serial.print( "dm = " );
Serial.println( dm );
//if(drop_velo<dml||drop_velo>dmh)
if ( drop_velo != dm )
{
Serial.print( "enter if" );
//digitalWrite(1,HIGH);delay(1000);}
tone( 1, 800, 1000 );
}
drop_count = 0;
}
char key = keypad.getKey();
if ( key )
{
if ( key == 'D' )
{
delay( 1000 );
resetFunc();
}
}
lcd.clear();
lcd.print( "drop velo = " );
lcd.print( drop_velo );
delay( 100 );
/*motor.step(1000);
delay(2000);
motor.step(-1000);
delay(2000);*/
}