Pages: [1]   Go Down
Author Topic: Ensimmäinen robotti  (Read 1206 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hei!
Osaisiko joku auttaa tämän koodin nopeuttamisessa? Kun robotti huomaa esteen, sillä kestää hetken "älytä" se ja kääntyä.
Code:
int servoFrontPin=8;
int servoBackPin=7;
int irLeft=2;
int irRight=4;

void pulseServo(int servoPin, int pulseLenUs)
{
digitalWrite(servoPin, HIGH);
delayMicroseconds(pulseLenUs);
digitalWrite(servoPin, LOW);
delay(20);
}

void setup()
{
        pinMode(irLeft, INPUT);
        pinMode(irRight, INPUT);
pinMode(servoFrontPin, OUTPUT);
pinMode(servoBackPin, OUTPUT);
}

void loop()
{
  int sensorRightValue = digitalRead(irRight);
  int sensorLeftValue = digitalRead(irLeft);
  if (sensorRightValue == 1 & sensorLeftValue ==1)
  {
   for (int i=0; i<=25; i++) {
pulseServo(servoFrontPin, 1900);
pulseServo(servoBackPin, 1000);
  }
  }
else if (sensorRightValue == 0 & sensorLeftValue == 0)
{
 for (int i=0; i<=25; i++) {
pulseServo(servoFrontPin, 1900);
pulseServo(servoBackPin, 1900);
 
 
}
}

else if (sensorRightValue == 1 & sensorLeftValue == 0)
{
  for (int i=0; i<=25; i++) {
pulseServo(servoFrontPin, 1000);
pulseServo(servoBackPin, 1000);
  }
}
else if (sensorRightValue == 0 & sensorLeftValue == 1)
{
 for (int i=0; i<=25; i++) {
pulseServo(servoFrontPin, 1900);
pulseServo(servoBackPin, 1900);

}
}
}

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 saattais nopeutua jos esm for luupeissa tutkit joka kierroksella onko sensor x valuet muuttuneet ja jos niin poistutaan luupista heti.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Siis...Täh?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

for (int i=0; i<=25; i++) {
      pulseServo(servoFrontPin, 1900);
      pulseServo(servoBackPin, 1000);
if(jos sensorrightvalue tai sensorleftvalue muuttujissa ja sensoreissa on eri arvo kuin tähän silmukkaan tullessa)
{
i=tähän sellainen arvo jotta for silmukassa ei tarvitse enää pyöriä
}
  }

edit: muokattu
« Last Edit: February 27, 2012, 06:21:57 am by uk350 » Logged

Pages: [1]   Go Up
Jump to: