Stap 7: Het toevoegen van meer wiskunde!
Hier is de definitieve code waarin Arduino omgezet in de hoeveelheid inch voeten, werven, centimeters en meters. Dan het keert terug naar de knop"aanpassen" ga vooruit en werken door de hele code, en zorg ervoor dat je alles begrijpen.Ik voegde een conversie naar voeten, werven, centimeters en meters. Vergeet niet om te definiëren wat deze zijn in het begin van de schets.
yards = (feet / 3); centimeters = (inches * 2.54); meters = (centimeters * 100);
Iint voeten; int werven; lange inch; lange centimeter; lange meter; int lcdbl = 10; instellen van de achtergrondverlichting pin aan pin 10 int wachten = 3000; het woord "wait" aan toewijzen 3000 milliseconden Iint feet; int yards; long inches; long centimeters; long meters; int lcdbl = 10; // set the backlight pin to pin 10 int wait = 3000; // assign the word "wait" to 3000 milliseconds #include <LiquidCrystal.h> LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void setup() { lcd.begin(16,2); // columns, rows. use 16,2 for a 16x2 LCD, etc. pinMode(lcdbl, OUTPUT); // setting the lcdbl (lcd backlight) pin to an OUPUT analogWrite(lcdbl, 255); // set the backlight brightness to on lcd.print("Welcome to the"); lcd.setCursor(0,1); // the cursor is like the rows on your LCD. After this, it will print // the text on the bottom line of your LCD screen. lcd.print("unit converter!"); delay(wait); /* if you look here, intead of the usual time in milliseconds it says "wait". If we look what "wait" is, at the beginning of the code, we see that "wait" is assigned to 3000 milliseconds, so whenever I type in delay(wait); it wil have a delay of 3000 milliseconds, or 3 seconds. */ lcd.clear(); delay(1000); int sensorValue = analogRead(A0); // read the input on analog pin 0: float inches = sensorValue * (100 / 1023.0); // Convert the analog reading } // the loop routine runs over and over again forever: void loop() { for(int i=0; i<50; ++i) { int sensorValue = analogRead(A0); // read the input on analog pin 0: float inches = sensorValue * (500 / 1023.0); lcd.print("Adjust the knob"); lcd.setCursor(0,1); lcd.print(inches); delay(100); lcd.clear(); } int sensorValue = analogRead(A0); // read the input on analog pin 0: float inches = sensorValue * (500 / 1023.0); // for feet lcd.print(inches); lcd.print(" inches:"); delay(wait); lcd.clear(); delay(1000); // for feet feet = (inches / 12); // conversion from feet to inches Here this is telling Arduino that // inches is equal to feet times 12 lcd.print(feet); lcd.print(" feet:"); delay(wait); lcd.clear(); delay(1000); yards = (feet / 3); // conversion to yards lcd.print(yards); lcd.print(" yards:"); delay(wait); lcd.clear(); delay(1000); centimeters = (inches * 2.54); // conversion to centimeters lcd.print(centimeters); lcd.setCursor(0,1); lcd.print("centimeters"); delay(wait); lcd.clear(); delay(1000); meters = (centimeters / 100); // conversion to meters lcd.print(meters); lcd.print(" meters"); delay(wait); lcd.clear(); delay(1000); }
Hier is een overzicht van de bovenstaande sketch:
- Bij het opstarten, "Welcome to de Convertor van de eenheid"
- "De knop aanpassen" met de hoeveelheid inch hieronder. Draai de knop!
- De hoeveelheid inch
- De hoeveelheid voeten
- De hoeveelheid centimeter
- Wordt het bedrag of de meter