i have to make a train thingy for school but the break from case 1 to --- case 2 doesnt work anybody knows how does this come?
# include <Servo.h>
Servo servo1;
Servo servo2;
int angle=10;
int pos0=0;
int pos1=180;
int moment;
int ledpingroen1=2;
int ledpinrood1=3;
int ledpingroen2=4;
int ledpinrood2=9;
int piezopin=10;
int brightness=0;
int fadeAmount=5;
byte sensorpin1=A1;
int waarde1;
byte sensorpin2=A2;
int waarde2;
void setup() {
pinMode(ledpingroen1,OUTPUT);
pinMode(ledpinrood1,OUTPUT);
pinMode(ledpingroen2,OUTPUT);
pinMode(ledpinrood2,OUTPUT);
servo1.attach(5);
servo2.attach(6);
servo1.write(180);
servo2.write(90);
Serial.begin(9600);
}
void loop() {
waarde1=analogRead(sensorpin1);
waarde2=analogRead(sensorpin2);
switch(moment){
case 0: // de normale stand geen treinen
digitalWrite(ledpinrood1,LOW);
digitalWrite(ledpinrood2,LOW);
digitalWrite(ledpingroen1,HIGH);
digitalWrite(ledpingroen2,HIGH);
delay(500);
digitalWrite(ledpingroen1,LOW);
digitalWrite(ledpingroen2,LOW);
delay(400);
if (waarde1<=100 || waarde2<=100){
moment=1;
}
break; // naar 2de moment gaan
case 1: // er is trein gezien rode lampen en slagboom en tutter
digitalWrite(ledpingroen1,LOW);
digitalWrite(ledpingroen2,LOW);
digitalWrite(ledpinrood1,HIGH);
digitalWrite(ledpinrood2,HIGH);
for(angle = 40; angle < 155; angle++) {
servo1.write(angle);
servo2.write(angle);
delay(15);
tone(10,1000);
delay(50);
noTone(10);
delay(50); }
if (waarde1>=850 || waarde2>=850){
moment=2;
}
break;
case 2: // slagbomen terug open en veilig
for(angle = 160; angle < 40; angle--) {
servo1.write(angle);
servo2.write(angle);
delay(15);
break;
}
}
}
If You check "How to use this Forum" You will discouver that Code Tags ought to be used for attached code. Then a lot more members can read the code and contribute.
To me it looks like
if (waarde1>=850 || waarde2>=850){
moment=2;
should be investigated. What value do waarde 1 and waarde2 have here? Use Serial.print for debugging.
i am new with programming first project i have a problem that case 1 doesnt go to case 2 i hope you guys can help me thx
# include <Servo.h>
Servo servo1;
Servo servo2;
int angle=10;
int pos0=0;
int pos1=180;
int moment;
int ledpingroen1=2;
int ledpinrood1=3;
int ledpingroen2=4;
int ledpinrood2=9;
int piezopin=10;
int brightness=0;
int fadeAmount=5;
byte sensorpin1=A1;
int waarde1;
byte sensorpin2=A2;
int waarde2;
void setup() {
pinMode(ledpingroen1,OUTPUT);
pinMode(ledpinrood1,OUTPUT);
pinMode(ledpingroen2,OUTPUT);
pinMode(ledpinrood2,OUTPUT);
servo1.attach(5);
servo2.attach(6);
servo1.write(180);
servo2.write(90);
Serial.begin(9600);
}
void loop() {
waarde1=analogRead(sensorpin1);
waarde2=analogRead(sensorpin2);
switch(moment){
case 0: // de normale stand geen treinen
digitalWrite(ledpinrood1,LOW);
digitalWrite(ledpinrood2,LOW);
digitalWrite(ledpingroen1,HIGH);
digitalWrite(ledpingroen2,HIGH);
delay(500);
digitalWrite(ledpingroen1,LOW);
digitalWrite(ledpingroen2,LOW);
delay(400);
if (waarde1<=100 || waarde2<=100){
moment=1;
}
break; // naar 2de moment gaan
case 1: // er is trein gezien rode lampen en slagboom en tutter
digitalWrite(ledpingroen1,LOW);
digitalWrite(ledpingroen2,LOW);
digitalWrite(ledpinrood1,HIGH);
digitalWrite(ledpinrood2,HIGH);
for(angle = 40; angle < 155; angle++) {
servo1.write(angle);
servo2.write(angle);
delay(15);
tone(10,1000);
delay(50);
noTone(10);
delay(50); }
if (waarde1>=850 || waarde2>=850){
moment=2;
}
break;
case 2: // slagbomen terug open en veilig
for(angle = 160; angle < 40; angle--) {
servo1.write(angle);
servo2.write(angle);
delay(15);
break;
}
}
}
viktordries:
i am new with programming first project i have a problem that case 1 doesnt go to case 2 i hope you guys can help me thx
Case 1 does not go to case 2 because of the break. To run case 2 you will have to satisfy switch( 2 ).
You can take the break out or put the code in case 2 into case 1. Whatever is logical.