Nike Wang10/5
這是一個計數你拉單槓的機器,主要零件是LCD螢幕 超音波感測器和兩色不同燈泡。
適合國中到高中想要練上臂的
這是程式
#include
#include
int _name ; int UltrasonicSensorCM(int trigPin, int echoPin) //Ultrasonic Sensor Code Auto Generated Return CM max distance 200 { long duration; pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(20); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); duration = duration / 59; if ((duration < 2) || (duration > 200)) return false; return duration; }
// For these LCD controls to work you MUST replace the standard LCD library from... // https://github.com/marcoschwartz/LiquidCrystal_I2C // Direct download https://github.com/marcoschwartz/LiquidCrystal_I2C/archive/master.zip // Your project will not compile until this is done. LiquidCrystal_I2C lcd_I2C_27(0x27,16,2); // set the LCD address for a 16 chars and 2 line display
void setup(){ // put your setup code here, to run once: digitalWrite( 6 , LOW ); //set ultrasonic sensor trigPin
pinMode( 12 , OUTPUT); // sets the digital pin as output pinMode( 11 , OUTPUT); // sets the digital pin as output lcd_I2C_27.init (); // initialize the lcd lcd_I2C_27.backlight(); _name = 0 ;
}
void loop(){ // put your main code here, to run repeatedly: if ( UltrasonicSensorCM( 6 , 7 ) > 14 ) { digitalWrite( 12 , HIGH ); // sets the digital pin on/off digitalWrite( 11 , LOW ); // sets the digital pin on/off _name = ( _name + 1 ) ; } else { digitalWrite( 11 , HIGH ); // sets the digital pin on/off digitalWrite( 12 , LOW ); // sets the digital pin on/off } lcd_I2C_27.setCursor(0 , 0) ; // set the cursor, counting begins with 0 lcd_I2C_27.print( "number" ); // Print a message to the LCD. lcd_I2C_27.print( _name ); // Print a message to the LCD. delay(1000);}