Arduino / C ++中的函数/方法定义错误。还是语法错误?

时间:2013-12-01 14:29:06

标签: c++ function compiler-errors arduino

我正在将我在Ruby中编写的程序翻译成Arduino / C ++。在我第一次尝试定义函数/方法时,我不断收到以下错误:“BreadboardTestFunctions:41:错误:在'{'令牌之前不允许使用函数定义 BreadboardTestFunctions:91:错误:在输入结束时预期`}'

难以理解,因为函数定义必须后跟一对括号。这可能反映了语法错误,因为它已经保留,因为我已经纠正了函数中的错误分数,因为我试图解决这个问题。但现在它对我来说还不错。

我在“//例程乘以:行为=大脑*刺激”这一部分的矩阵乘法后生成输出。虽然有问题的方法,“mody”(第40行)现在只调用一次,一旦我开始工作,所有输出都会调用它。

代码:

    /*  BREADBOARD
  Implement program on Arduino + breadboard
*/

// constants 
int foodPin = 2;     // to provide food
int painPin = 3;     // to punish
int ucsPin = 4;      // the UCS
int csPin = 5;       // the CS
int lightPin = 6;    // turn the "light" stim on/off
int thresh = 700;

// variables 
int buttonState = 0; // variable for reading the pushbutton status
boolean lighton = false;
unsigned short int energy = 10000;
int stimulus[11] = {0,0,0,0,0,0,0,0,0,0,0};

int brain[7][11] = { {0,0,0,0,99,0,0,0,0,1,0},
                     {0,0,0,0,0,99,0,0,0,1,0},
                     {0,0,0,0,0,0,99,0,0,1,0},
                     {90,0,0,0,0,0,0,1,-1,1,-99},
                     {0,90,0,0,0,0,0,1,-1,1,1},
                     {0,0,90,0,0,0,0,1,-1,1,1},
                     {0,0,0,90,0,0,0,1,-1,1,1} };

int behavior[7] = {0,0,0,0,0,0,0};

void setup() {
  // initialize the pushbutton pins as an input:
  pinMode(foodPin, INPUT); 
  pinMode(painPin, INPUT);
  pinMode(ucsPin, INPUT);
  pinMode(csPin, INPUT);
  pinMode(lightPin, INPUT);
  Serial.begin(9600);
  int ix=0;

  // define behavioral methods
  void mody (int ix, int brain[], int stimulus[])
      { int psp=20;
        int j;
        for(j=7;j<11;j++)
    {if (brain[ix][j] > 0) brain[ix][j]+= stimulus[j] * (99-brain[ix][j])/psp;
     if (brain[ix][j] < 0) brain[ix][j]+= -1*(stimulus[j] * abs(99-brain[ix][j])/psp);}
         return;}

} // end void setup

void loop(){
  // decay stimulus vector.  do this and check inputs for ALL stimulii later
  int k;
  for(k=0;k<11;k++)
  {if (stimulus[k] > 1) stimulus[k]-=2; else stimulus[k]=0;}

  //check inputs

  buttonState = digitalRead(foodPin);
  if (buttonState == HIGH) stimulus[4] = 9;
  buttonState = digitalRead(painPin);
  if (buttonState == HIGH) stimulus[5] = 9;
  buttonState = digitalRead(ucsPin);
  if (buttonState == HIGH) stimulus[6] = 9;
  buttonState = digitalRead(lightPin);
  if (buttonState == HIGH) {stimulus[7] = 9; stimulus[8] = 9;lighton = true;}
      else {stimulus[7] = 0; stimulus[8] = 0;lighton = false;}
  buttonState = digitalRead(ucsPin);
  if (buttonState == HIGH) stimulus[6] = 9;

// routine to multiply:  behavior=brain * stimulus'
int i, j;
    for(i=0;i<7;i++)
    {  behavior[i]=0;
       for (j=0;j<11;j++)
           {behavior[i]= behavior[i]+stimulus[j]*brain[i][j]; }
    } // end for i
    if (behavior[0] > thresh) {Serial.println("Positive Fixer");}
    if (behavior[1] > thresh) {Serial.println("Negative Fixer");}
    if (behavior[2] > thresh) {Serial.println("UCR"); mody (2, brain[], stimulus[]);}
    if (behavior[3] > thresh) {Serial.println("Operant one");}
    if (behavior[4] > thresh) {Serial.println("Operant two");}
    if (behavior[5] > thresh) {Serial.println("Operant three");}
    if (behavior[6] > thresh) {Serial.println("Operant four");}

// generate random operant
   if (random(energy) < 10) stimulus[random(4)]= 9 + random(3);

energy --;
Serial.println(energy);

}  // end void loop

1 个答案:

答案 0 :(得分:2)

您可能无法在另一个函数中定义一个函数。这是您在以下代码片段中尝试执行的操作

void setup() {
  // initialize the pushbutton pins as an input:
  pinMode(foodPin, INPUT); 
  pinMode(painPin, INPUT);
  pinMode(ucsPin, INPUT);
  pinMode(csPin, INPUT);
  pinMode(lightPin, INPUT);
  Serial.begin(9600);
  int ix=0;

  // define behavioral methods
  void mody (int ix, int brain[], int stimulus[])

您正在尝试在功能设置中定义功能。