Ich habe eine kurze Frage. Ich habe einen Code für einen Snackautomat geschrieben. Der sieht wie folgt aus:
#include <LiquidCrystal.h> // includes the LiquidCrystal Library
#include <Servo.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
Servo Servo1, Servo2, Servo3, Servo4;
#define pirPin 9
int T1 = 52;
int T2 = 50;
int T3 = 48;
int T4 = 46;
int T1Pressed;
int T2Pressed;
int T3Pressed;
int T4Pressed;
void setup() {
lcd.begin(16, 2);
Servo1.attach(22);
Servo2.attach(24);
Servo3.attach(26);
Servo4.attach(28);
pinMode(pirPin, INPUT);
pinMode(T1, INPUT);
pinMode(T2, INPUT);
pinMode(T3, INPUT);
pinMode(T4, INPUT);
}
void loop() {
// Print "Insert a coin!" on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Insert a coin!");
// Wait until a coin is detected
while (true) {
if (digitalRead(pirPin) == LOW) { // If a coin is detected, exit the from the while loop
break;
}
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Select your item");
while (true) {
if (digitalRead(T1) == HIGH) {
T1Pressed = 1;
break;
}
if (digitalRead(T2) == HIGH) {
T2Pressed = 2;
break;
}
if (digitalRead(T3) == HIGH) {
T3Pressed = 3;
break;
}
if (digitalRead(T4) == HIGH) {
T4Pressed = 4;
break;
}
}
// Print "Delivering..."
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Delivering...");
// Depending on the pressed button, move the carrier to that position and discharge the selected item
switch (T1Pressed) {
case 1:
// Move the container to location 1
Servo1.write(0);
delay(2200);
Servo1.detach();
break;
case 2:
Servo2.write(0);
delay(2200);
Servo2.detach();
break;
case 3:
Servo3.write(0);
delay(2200);
Servo3.detach();
break;
case 4:
Servo4.write(0);
delay(2200);
Servo4.detach();
break;
}
lcd.clear(); // Clears the display
lcd.setCursor(0, 0);
lcd.print("Item delivered!"); // Prints on the LCD
delay(1000);
}
Nun ist mein Problem :
Wenn die Bedingung das 1. mal erfüllt ist, dann funktioniert der Code perfekt. Wenn ich denn Sensor und denn Taster das 2. mal betätige kommt zwar auf dem Lcd die Meldung
"Item delivered " was ja auch gut so ist, aber der Servo dreht sich leider nicht.
Kann mir jemand helfen ? Vielen Dank für eure Antworten
Das funktioniert nun. Jetzt habe ich leider ein anderes problem
#include <LiquidCrystal.h> // includes the LiquidCrystal Library
#include <Servo.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
Servo Servo1, Servo2, Servo3, Servo4;
#define pirPin 9
int T1 = 52;
int T2 = 50;
int T3 = 48;
int T4 = 46;
int T1Pressed;
int T2Pressed;
int T3Pressed;
int T4Pressed;
void setup() {
lcd.begin(16, 2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
Servo1.attach(22);
Servo2.attach(24);
Servo3.attach(26);
Servo4.attach(28);
pinMode(pirPin, INPUT);
// Activating the digital pins pull up resistors
pinMode(T1, INPUT);
pinMode(T2, INPUT);
pinMode(T3, INPUT);
pinMode(T4, INPUT);
}
void loop() {
// Print "Insert a coin!" on the LCD
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Insert a coin!");
// Wait until a coin is detected
while (true) {
if (digitalRead(pirPin) == LOW) { // If a coin is detected, exit the from the while loop
break;
}
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Select your item");
while (true) {
if (digitalRead(T1) == HIGH) {
T1Pressed = 1;
break;
}
if (digitalRead(T2) == HIGH) {
T2Pressed = 2;
break;
}
if (digitalRead(T3) == HIGH) {
T3Pressed = 3;
break;
}
if (digitalRead(T4) == HIGH) {
T4Pressed = 4;
break;
}
}
// Print "Delivering..."
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Delivering...");
// Depending on the pressed button, move the carrier to that position and discharge the selected item
switch (T1Pressed) {
case 1:
Servo1.attach(22);
Servo1.write(0);
delay(2200);
Servo1.detach();
break;
case 2:
Servo2.attach(24);
Servo2.write(0);
delay(2200);
Servo2.detach();
break;
case 3:
Servo3.attach(26);
Servo3.write(0);
delay(2200);
Servo3.detach();
break;
case 4:
Servo4.attach(28);
Servo4.write(0);
delay(2200);
Servo4.detach();
break;
}
lcd.clear(); // Clears the display
lcd.setCursor(0, 0);
lcd.print("Item delivered!"); // Prints on the LCD
delay(1000);
}
und zwar:
Wenn ich nun den Sensor aktiviere und den taster1 drücke dreht es so wie es sein sollte. Wenn ich erneut den Sensor aktiviere und den taster1 drücke funktioniert es immer noch.
Wenn ich nun aber den Sensor aktiviere und den Taster2 drücke dreht sich der Servo1. Jedoch sollte sich der Servo2 beim taster2 drehen, Servo3 beim Taster 3, Servo4 beim Taster 4.
#include <LiquidCrystal.h> // includes the LiquidCrystal Library
#include <Servo.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
Servo Servo1, Servo2, Servo3, Servo4;
#define pirPin 9
int T1 = 52;
int T2 = 50;
int T3 = 48;
int T4 = 46;
int TPressed;
void setup() {
lcd.begin(16, 2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
Servo1.attach(22);
Servo2.attach(24);
Servo3.attach(26);
Servo4.attach(28);
pinMode(pirPin, INPUT);
// Activating the digital pins pull up resistors
pinMode(T1, INPUT);
pinMode(T2, INPUT);
pinMode(T3, INPUT);
pinMode(T4, INPUT);
}
void loop() {
// Print "Insert a coin!" on the LCD
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Insert a coin!");
// Wait until a coin is detected
while (true) {
if (digitalRead(pirPin) == LOW) { // If a coin is detected, exit the from the while loop
break;
}
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Select your item");
while (true) {
if (digitalRead(T1) == HIGH) {
TPressed = 1;
break;
}
if (digitalRead(T2) == HIGH) {
TPressed = 2;
break;
}
if (digitalRead(T3) == HIGH) {
TPressed = 3;
break;
}
if (digitalRead(T4) == HIGH) {
TPressed = 4;
break;
}
}
// Print "Delivering..."
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Delivering...");
// Depending on the pressed button, move the carrier to that position and discharge the selected item
switch (TPressed) {
case 1:
Servo1.attach(22);
Servo1.write(0);
delay(2200);
Servo1.detach();
break;
case 2:
Servo2.attach(24);
Servo2.write(0);
delay(2200);
Servo2.detach();
break;
case 3:
Servo3.attach(26);
Servo3.write(0);
delay(2200);
Servo3.detach();
break;
case 4:
Servo4.attach(28);
Servo4.write(0);
delay(2200);
Servo4.detach();
break;
}
lcd.clear(); // Clears the display
lcd.setCursor(0, 0);
lcd.print("Item delivered!"); // Prints on the LCD
delay(1000);
}
Sollte doch stimmen. Jetzt funktionieren T1, T3 und T4. Der T2 funktioniert jedoch nicht.
Keine Ahnung.
Ich hab das mal aufegräumt und und Deine Leerzeilen rausgeschmissen.
Der Code kompiliert fehler/warnungsfrei. Mangels hardware ungetestet.
Kontolliere auf dem Seriellen Monitor, was passiert.
#include <LiquidCrystal.h> // includes the LiquidCrystal Library
#include <Servo.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
Servo myServo;
const byte anzahl = 4;
const byte servoPin[anzahl] = {22, 24, 26, 28};
const byte tastPin[anzahl] = {52, 50, 48, 46};
const byte pirPin = 9;
void setup()
{
Serial.begin(115200);
Serial.println(F("Start..."));
lcd.begin(16, 2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display
pinMode(pirPin, INPUT);
// Activating the digital pins pull up resistors
for (byte b = 0; b < anzahl; b++)
pinMode(tastPin[b], INPUT);
}
void loop()
{
// Print "Insert a coin!" on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(F("Insert a coin!"));
// Wait until a coin is detected
while (true)
{
if (digitalRead(pirPin) == LOW) // If a coin is detected, exit the from the while loop
{
Serial.println(F("Coin gefunden"));
break;
}
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Select your item");
byte b = 0;
while (!myServo.attached())
{
if (digitalRead(tastPin[b]))
{
Serial.print(F("Taste ")); Serial.print(b); Serial.println(F("gedrückt!"));
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Delivering...");
myServo.attach(servoPin[b]);
myServo.write(0);
Serial.print(F("Servo ")); Serial.print(b); Serial.println(F(" ausgelöst"));
delay(2200);
myServo.detach();
Serial.println(F("Schluß"));
}
else
{
b++;
if (b >= anzahl) b = 0;
}
}
// Print "Delivering..."
// Depending on the pressed button, move the carrier to that position and discharge the selected item
lcd.clear(); // Clears the display
lcd.setCursor(0, 0);
lcd.print("Item delivered!"); // Prints on the LCD
delay(1000);
}