Ciao a tutti, sto scrivendo il codice per un robot, e sto cercando di fargli compiere delle azioni, richiamate dalle if, come muovere dei servi per un tempo prestabilito tra due valori random. Ho provato inserendo dei delay randomizzati, ma credo che durante quel periodo il robot non possa ottenere input o computare, quindi non è un buon sistema. Come posso fare in modo di non utilizzare il delay e permettere al robot per esempio di andare indietro per (da 250 a 1000 millisecondi) pero' potendo vedere eventuali ostacoli dietro di lui?
Poi ho un altro problema, vorrei accelerare i servi. e non farli partire a canna come si dice a Milano!! Ho provato con la tecnica dell'i++ ma non capisco se funziona, sembrano meno scattosi, ma non saprei dire, ecco il codice:
#include <MegaServo.h>
#include <Wire.h>
#define sensorAddress 0x70
#define readCentimeters 0x51
#define readMicroseconds 0x52
#define resultRegister 0x02
int stdledPin = 13; // choose the pin for the LED
int irFrontPin = 0; // choose the input pin (for a pushbutton)
int irFrontval = 0; // variable for reading the pin status
int speedLeft = 0; // speed we want on left servo
int speedRight = 0; // speed we want on right servo
boolean freeFront = false; // forward path
boolean freeBack = false; // backward path
boolean standBy = false; // standby mode
boolean moveBack = false; // backward mode
boolean moveForward = false; // backward mode
MegaServo left;
MegaServo right;
void setup() {
Wire.begin(); // start I2C bus
pinMode(stdledPin, OUTPUT); // declare LED as output
pinMode(irFrontPin, INPUT); // declare pushbutton as input
left.attach(10);
right.attach(11);
Serial.begin(9600);
}
void targetFront() { // there is someone in front of RADIYOSP
freeFront = false; // The front path is not free
moveBack = true; // we are no longer in standby we are going backward
float i = 1; // variable to add for accelerate servos
digitalWrite(stdledPin, HIGH); //don't light up stdled
left.write(89 - i++); //from standby position substract every cycle 0.1 (the servo start to go backward)
right.write(90 + i++); //from standby position add every cycle 0.1 (the servo start to go backward)
}
void targetBack() { // there is someone in the back of RADIYOSP
freeBack = true;
moveBack = true; // we are no longer in standby we are going backward
float i = 1; // variable to add for accelerate servos
digitalWrite(stdledPin, HIGH); //don't light up stdled
left.write(91 + i++); //from standby position substract every cycle 0.1 (the servo start to go backward)
right.write(90 - i++); //from standby position add every cycle 0.1 (the servo start to go backward)
}
/*void takeatrip {
standBy = false;
moveforward = true;
}
*/
void standFor() { // standby mode (no movements)
left.write(90); // stop servoleft
right.write(90); // stop servoright
standBy = true; // we are in standby
digitalWrite(stdledPin, LOW); // light up stdled
freeFront = true; // front is free we can navigate forward
moveBack = false; // we are in standby
moveForward = false;
}
void payattention() {
irFrontval = analogRead(irFrontPin); // read ir input value
sendCommand(sensorAddress, readCentimeters); // send the command to read the result in centimeters:
delay(70); // wait at least 70 milliseconds for a result:
setRegister(sensorAddress, resultRegister); // set the register that you want to reas the result from:
int sensorReading = readData(sensorAddress, 2); // read the result:
if (standBy = true) {
if (irFrontval > 100) { // if irfront input is > 100
targetFront();
delay(500 + random(100, 3000));
}
if (irFrontval < 100) { // if ir get something
standFor(); //wait for input
}
if (sensorReading < 40) { // if ultra (SFR 02) get something
targetBack(); // there is someone back RADIYOSP
delay(500 + random(100, 3000));
}
if (sensorReading > 40) {
standFor(); //wait for input
}
delay(70);
}
}
void loop(){
payattention(); // take a look around
}
//I2C COM
void sendCommand (int address, int command) { // start I2C transmission:
Wire.beginTransmission(address);
Wire.send(0x00);
Wire.send(command);
Wire.endTransmission();
}
//SET INPUT ADDRESS
void setRegister(int address, int thisRegister) {
Wire.beginTransmission(address); // start I2C transmission:
Wire.send(thisRegister); // send address to read from:
Wire.endTransmission();
}
//READ DATA
int readData(int address, int numBytes) { //readData() returns a result from the SRF sensor
int result = 0; // the result is two bytes long
Wire.requestFrom(address, numBytes); // send I2C request for data:
while (Wire.available() < 2 ) { // wait for two bytes to return:
// wait for result
}
result = Wire.receive() * 256; // read the two bytes, and combine them into one int:
result = result + Wire.receive();
return result; // return the result:
}
(ci sono varie variabili che apparentemente sono li per niente perchè il codice è undercostruction).