Buy an Axon, Axon II, or Axon Mote and build a great robot, while helping to support SoR.
0 Members and 1 Guest are viewing this topic.
void loop () { int val; // Ir val float distance; // distance of val in cm float rightDistance; float leftDistance; time = millis(); if ( time - previousTime > 250){ previousTime = time; // BUTTON CODE buttonState = debounce(previousButtonState); if ( buttonState == HIGH && previousButtonState == LOW) { botState = !botState; } previousButtonState = buttonState; val = analogRead(sensorPin); distance = 6787.0 /(val - 3.0) - 4.0; if ( botState == HIGH ) { // Means if it is on digitalWrite(ledPin, HIGH); Serial.println(distance); if ( distance > 25 ){ testa.write(85); Forward(220, 170); delay(1000); } else { // If there is an obstacle... STOP(); // NOW WATCH RIGHT (0 DEGREES) AND LEFT. SAVE DISTANCES Serial.println("0 GRADI "); delay(1000); testa.write(0); val = analogRead(sensorPin); distance = 6787.0 /(val - 3.0) - 4.0; rightDistance = distance; Serial.print("VALORE: "); Serial.println(rightDistance); Serial.println("180 GRADI "); testa.write(180); val = analogRead(sensorPin); distance = 6787.0 /(val - 3.0) - 4.0; leftDistance = distance; Serial.print("VALORE: "); Serial.println(leftDistance); if (rightDistance > leftDistance){ Serial.println("DESTRA"); Right90(); // More space on right then turn right } else { Serial.println("SINISTRA"); Left90(); // turn left } delay(1000); } }else{ digitalWrite(ledPin, LOW); STOP(); } }}
void loop () { int val; float distance; float rightDistance = 0; float leftDistance = 0; time = millis(); if ( time - previousTime > 250){ previousTime = time; // BOTTONE buttonState = debounce(previousButtonState); if ( buttonState == HIGH && previousButtonState == LOW) { botState = !botState; } previousButtonState = buttonState; // FINE BOTTTONE val = analogRead(sensorPin); distance = 6787.0 /(val - 3.0) - 4.0; if ( botState == HIGH ) { digitalWrite(ledPin, HIGH); Serial.println(distance); if ( distance > 25 ){ testa.write(80); Forward(220, 170); delay(1000); } else { STOP(); // MUOVUI LA TESTOLINA Serial.println("0 GRADI "); delay(1000); testa.write(0); delay(1000); val = analogRead(sensorPin); distance = 6787.0 /(val - 3.0) - 4.0; rightDistance = distance; Serial.print("VALORE A DESTRA: "); Serial.println(rightDistance); Serial.println("180 GRADI "); testa.write(165); delay(1000); val = analogRead(sensorPin); distance = 6787.0 /(val - 3.0) - 4.0; leftDistance = distance; Serial.print("VALORE A SINISTRA: "); Serial.println(leftDistance); // Fine muovi testolina delay(1000); if (rightDistance > leftDistance){ Serial.println("DESTRA"); Right90(); } else { Serial.println("SINISTRA"); Left90(); } testa.write(80); delay(1000); } }else{ digitalWrite(ledPin, LOW); STOP(); } }}
void loop() { read all inputs; possibly change state based on current state and inputs; apply outputs based on current state;}
float speedleft = 0;float speedright = 0;float leftdistance = 0;float rightdistance = 0;float turn = 0;float servoposition = 90;unsigned int statetime = 0;bool oldbutton = true;enum State { StateStopped, StateForward, StateScanningLeft, StateScanningRight, StateTurningLeft, StateTurningRight};State curstate = StateStopped;void loop() { float distance = read_sensor(); bool button = read_button(); if (oldbutton == false && button == true) { if (curstate == StateStopped) { curstate = StateForward; } else { curstate = StateStopped; } } switch (curstate) { case StateStopped: speedleft = 0; speedright = 0; servoposition = 90; statetime = millis(); break; case StateForward: speedleft = 1; speedright = 1; if (distance < 25) { curstate = StateScanningLeft; speedleft = 0; speedright = 0; } statetime = millis(); break; case StateScanningLeft: servoposition = 0; if (millis() - statetime > 500) { leftdistance = distance; curstate = StateScanningRight; statetime = millis(); } break; case StateScanningRight: servoposition = 180; if (millis() - statetime > 500) { rightdistance = distance; if (rightdistance > leftdistance) { curstate = StateTurningRight; } else { curstate = StateTurningLeft; } statetime = millis(); } break; case StateTurningLeft: speedleft = -1; speedright = 1; if (millis() - statetime > 700) { state = StateForward; statetime = millis(); } break; case StateTurningRight: speedleft = 1; speedright = -1; if (millis() - statetime > 700) { state = StateForward; statetime = millis(); } break; } oldbutton = button; set_motor_speeds(speedleft, speedright); set_servo_position(servoposition);}
boolean debounce(boolean last){ boolean current = digitalRead(buttonPin); if (last != current){ delay(5); current = digitalRead(buttonPin); } return current;}
void loop () { float distance; time = millis(); // BOTTONE buttonState = debounce(previousButtonState); if ( buttonState == HIGH && previousButtonState == LOW) { botState = !botState; if ( currentState == StateStopped){ currentState = StateForward; } else { currentState = StateStopped; } } // FINE BOTTTONE distance = 6787.0 / (analogRead(sensorPin) - 3.0) - 4.0; if ( botState == HIGH ) { digitalWrite(ledPin, HIGH); switch (currentState){ case StateForward: FORWARD(220, 170); if ( distance < 25 && prevDistance < 25){ currentState = StateScanningLeft; STOP(); tone(piezoPin, 3000, 1000); } break; case StateScanningLeft: testa.write(165); if (millis() - stateTime > 1000) { leftDistance = distance; currentState = StateScanningRight; stateTime = millis(); } break; case StateScanningRight: testa.write(0); if (millis() - stateTime > 1000) { rightDistance = distance; currentState = StateChoosingDirection; stateTime =millis(); } break; case StateChoosingDirection: testa.write(80); if (millis() - stateTime > 1000){ if(rightDistance > leftDistance){ currentState = StateTurningRight; leftDistance = 0; rightDistance = 0; } else { currentState = StateTurningLeft; leftDistance = 0; rightDistance = 0; } stateTime =millis(); } break; case StateTurningLeft: LEFT90(); if (millis() - stateTime > 1000) { currentState = StateForward; stateTime =millis(); } break; case StateTurningRight: RIGHT90(); testa.write(80); if (millis() - stateTime > 1000) { currentState = StateForward; stateTime =millis(); } break; } } else { digitalWrite(ledPin, LOW); testa.write(80); STOP(); } previousButtonState = buttonState; prevDistance = distance; }