Functions aufrufen

Hallo und Frohe Weihnachten erstmals.

Ich hab jetzt mein Code ein wenig verkleinert und wollte wissen, wie ich nun diese Funktion aufrufe. Allerdings möchte ich es nicht im Loop, weil es nur einmal aufgerufen werden soll. Wie mache ich das nun? Hier mein Code.

#include <Wire.h>

#define startbyte 0x0F
#define I2Caddress 0x07

int sv[2]={135,120};                 // servo positions:0:tilt:13,1:open/close:5
int lmspeed,rmspeed;                                 // left and right motor speed from -255 to +255 (negative value = reverse)
byte lmbrake,rmbrake;                                // left and right motor brake (non zero value = brake)
byte devibrate=50;                                   // time delay after impact to prevent false re-triggering due to chassis vibration
int sensitivity=50;                                  // threshold of acceleration / deceleration required to register as an impact
int lowbat=550;                                      // adjust to suit your battery: 550 = 5.50V
byte i2caddr=7;                                      // default I2C address of T'REX is 7. If this is changed, the T'REX will automatically store new address in EEPROM
byte i2cfreq=0;                                      // I2C clock frequency. Default is 0=100kHz. Set to 1 for 400kHz


int stay () {
  
   //stay
  Serial.println("Stay");
  lmspeed=0;
  rmspeed=0;
  MasterSend(startbyte,6,lmspeed,lmbrake,rmspeed,rmbrake,sv[0],sv[1],devibrate,sensitivity,lowbat,i2caddr,i2cfreq);
  delay(200); 
}

int forward () {
  
  //forward
  Serial.println("Forward");
 
  lmspeed=60;
  rmspeed=60;

  
  MasterSend(startbyte,6,lmspeed,lmbrake,rmspeed,rmbrake,sv[0],sv[1],devibrate,sensitivity,lowbat,i2caddr,i2cfreq);
  delay(50); //50
  for (int i = 0; i < 6; i++) {
     MasterReceive();                                   // receive data packet from T'REX controller
      delay(10); //50
  }
  
}


int turn_right () {
  
  //turn-right
  Serial.println("Turn Right");
 
  lmspeed=0;
  rmspeed=90;

  
  MasterSend(startbyte,6,lmspeed,lmbrake,rmspeed,rmbrake,sv[0],sv[1],devibrate,sensitivity,lowbat,i2caddr,i2cfreq);
  delay(10); //50
  for (int i = 0; i < 6; i++) {
     MasterReceive();                                   // receive data packet from T'REX controller
      delay(10); //50
  }
  
}

void setup()
{
  Serial.begin(9600);
  Wire.begin();                                      // no address - join the bus as master
  Serial.println("Sample Program started!");
}






void loop()
{
                      
}

weil es nur einmal aufgerufen werden soll.

Funktionen die am Anfang einmal ausgeführt werden sollen kommen in setup()

Nachtrag: Schau dir mal allgemein was zu Funktionen an. Deine Funktionen haben int als Rückgabe-Wert aber du gibst nichts zurück. Die sollten daher als "void" deklariert werden.

http://de.wikipedia.org/wiki/Void_%28Schl%C3%BCsselwort%29

http://www.willemer.de/informatik/cpp/funktion.htm http://de.wikibooks.org/wiki/C-Programmierung:_Funktionen http://openbook.galileo-press.de/c_von_a_bis_z/009_c_funktionen_001.htm

Also so jetzt:

#include <Wire.h>

#define startbyte 0x0F
#define I2Caddress 0x07

int sv[2]={135,120};                 // servo positions:0:tilt:13,1:open/close:5
int lmspeed,rmspeed;                                 // left and right motor speed from -255 to +255 (negative value = reverse)
byte lmbrake,rmbrake;                                // left and right motor brake (non zero value = brake)
byte devibrate=50;                                   // time delay after impact to prevent false re-triggering due to chassis vibration
int sensitivity=50;                                  // threshold of acceleration / deceleration required to register as an impact
int lowbat=550;                                      // adjust to suit your battery: 550 = 5.50V
byte i2caddr=7;                                      // default I2C address of T'REX is 7. If this is changed, the T'REX will automatically store new address in EEPROM
byte i2cfreq=0;                                      // I2C clock frequency. Default is 0=100kHz. Set to 1 for 400kHz




void setup()
{
  Serial.begin(9600);
  Wire.begin();                                      // no address - join the bus as master
  Serial.println("Sample Program started!");
  
  
void stay () {
  
   //stay
  Serial.println("Stay");
  lmspeed=0;
  rmspeed=0;
  MasterSend(startbyte,6,lmspeed,lmbrake,rmspeed,rmbrake,sv[0],sv[1],devibrate,sensitivity,lowbat,i2caddr,i2cfreq);
  delay(200); 
}

void forward () {
  
  //forward
  Serial.println("Forward");
 
  lmspeed=60;
  rmspeed=60;

  
  MasterSend(startbyte,6,lmspeed,lmbrake,rmspeed,rmbrake,sv[0],sv[1],devibrate,sensitivity,lowbat,i2caddr,i2cfreq);
  delay(50); //50
  for (int i = 0; i < 6; i++) {
     MasterReceive();                                   // receive data packet from T'REX controller
      delay(10); //50
  }
  
}


void turn_right () {
  
  //turn-right
  Serial.println("Turn Right");
 
  lmspeed=0;
  rmspeed=90;

  
  MasterSend(startbyte,6,lmspeed,lmbrake,rmspeed,rmbrake,sv[0],sv[1],devibrate,sensitivity,lowbat,i2caddr,i2cfreq);
  delay(10); //50
  for (int i = 0; i < 6; i++) {
     MasterReceive();                                   // receive data packet from T'REX controller
      delay(10); //50
  }
  
}  
}


void forward () {
}

void stay () {
}

void turn_right () {
}

void stay () {
}

void forward () {
}

void stay () {
}
void turn_right () {
}
void stay () {
}
void forward () {
}
void stay () {
}
void turn_right () {
}
int stay () {
}
void forward () {
}
void stay () {
}



void loop()
{
                      
}

Lies dir erst mal die Links durch, damit dir die Grundlagen klar werden. Das muss du wissen. Selbst wenn man keine eigenen Funktionen schreibt, ruft man ständig fertige Funktionen auf.

Das geschieht über ihren Namen:

//Funktion definieren
void func()
{
}

void setup()
{
   //Funktion aufrufen
   func();
}

Auf dem Arduino ist dabei die Reihenfolge egal. Du kannst hier auch setup() nach oben schreiben und Funktionen aufrufen bevor sie erstellt wurden, da die IDE da was für dich im Hintergrund erledigt.

Okai vielen Dank. Ich hoffe, ich habs jetzt richtig gemacht:

#include <Wire.h>

#define startbyte 0x0F
#define I2Caddress 0x07

int sv[2]={135,120};                 // servo positions:0:tilt:13,1:open/close:5
int lmspeed,rmspeed;                                 // left and right motor speed from -255 to +255 (negative value = reverse)
byte lmbrake,rmbrake;                                // left and right motor brake (non zero value = brake)
byte devibrate=50;                                   // time delay after impact to prevent false re-triggering due to chassis vibration
int sensitivity=50;                                  // threshold of acceleration / deceleration required to register as an impact
int lowbat=550;                                      // adjust to suit your battery: 550 = 5.50V
byte i2caddr=7;                                      // default I2C address of T'REX is 7. If this is changed, the T'REX will automatically store new address in EEPROM
byte i2cfreq=0;                                      // I2C clock frequency. Default is 0=100kHz. Set to 1 for 400kHz


void stay () {
  
   //stay
  Serial.println("Stay");
  lmspeed=0;
  rmspeed=0;
  MasterSend(startbyte,6,lmspeed,lmbrake,rmspeed,rmbrake,sv[0],sv[1],devibrate,sensitivity,lowbat,i2caddr,i2cfreq);
  delay(200); 
}

void forward () {
  
  //forward
  Serial.println("Forward");
 
  lmspeed=60;
  rmspeed=60;

  
  MasterSend(startbyte,6,lmspeed,lmbrake,rmspeed,rmbrake,sv[0],sv[1],devibrate,sensitivity,lowbat,i2caddr,i2cfreq);
  delay(50); //50
  for (int i = 0; i < 6; i++) {
     MasterReceive();                                   // receive data packet from T'REX controller
      delay(10); //50
  }
  
}


void turn_right () {
  
  //turn-right
  Serial.println("Turn Right");
 
  lmspeed=0;
  rmspeed=90;

  
  MasterSend(startbyte,6,lmspeed,lmbrake,rmspeed,rmbrake,sv[0],sv[1],devibrate,sensitivity,lowbat,i2caddr,i2cfreq);
  delay(10); //50
  for (int i = 0; i < 6; i++) {
     MasterReceive();                                   // receive data packet from T'REX controller
      delay(10); //50
  }
  
}

void setup()
{
  Serial.begin(9600);
  Wire.begin();                                      // no address - join the bus as master
  Serial.println("Sample Program started!");
  
  
  forward();
  stay();
  turn_right();
  stay();
  forward();
  stay();
  turn_right();
  stay();
  forward();
  stay();
  turn_right();
  stay();
  forward();
  stay();
  
}





void loop()
{
                      
}