Go Down

Topic: customized function "led" can't be recognized (Read 148 times) previous topic - next topic

Tan_Yuanqing

Hi Here is my code. The error is "exit status 1:  'led' was not declared in this scope". For covenience, I have spread red color on the "led" code.
Code: [Select]

// Digital value of sensor
#define SENSORD 26
//int count = 0;
int KEY_NUM = 0;

//analog value of sensor
#define SENSORA A1
int val_AD = 0;
// LED

//DC motor with encoder
 #define outputA 24//interrupt pin
 #define outputB 22//input pin
 int counter = 0;
 int aState;
 int aLastState;
 int val = 50;//pressure speed->motor 1
 int delaytime = 20;
 int VAL = 50;//stress speed->motor 2
 
 void setup() {
   pinMode(SENSORD,INPUT);//sensor digital value
   pinMode(SENSORA,INPUT);//sensor analog value
   pinMode(outputB,INPUT);//encoder signal 1
   pinMode(outputA,INPUT);
   pinMode(7,OUTPUT);//motor positive pole
   pinMode(8,OUTPUT);//motor negative pole
   pinMode(9,OUTPUT);//motor speed
   pinMode(10,OUTPUT);//motor positive pole
   pinMode(11,OUTPUT);//motor negative pole
   pinMode(12,OUTPUT);//motor speed
   pinMode(13,OUTPUT);//led pin
   Serial.begin (9600);   // Reads the initial state of the outputA
   
    digitalWrite(34,HIGH); //signal to make motor start rotate
   attachInterrupt(34, positionx, HIGH);
   attachInterrupt(26, [color=red]led[/color], HIGH);//thredhold
   attachInterrupt(26, motorBrake, HIGH);//forward brake
   attachInterrupt(32, motorreverse1, HIGH);//reverse begin
   attachInterrupt(30, motorBrake, HIGH);//reverse brake
   
 }
 void loop() {
    sensor();
   // Serial.print("Pulse:");
   // Serial.println(counter);
     
  }
 //motor attacth
 void positionx(){
  for( int i=1; i<=5; i++ ) {
  aLastState = digitalRead(outputA);
  motorforward1();
    Serial.print("access positionx");///////////////////////
    aState = digitalRead(outputA); // Reads the "current" state of the outputA
   // If the previous and the current state of the outputA are different, that means a Pulse has occuredL     
   Serial.println(aLastState);/////////////
          Serial.println(aState);
   if (aState != aLastState){     
     // If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
     int M=digitalRead(outputB);
       Serial.println(M);////////////////////

     if (M != aState) {
       counter ++;
     } else {
       counter --;//rotation direction: after each pulse if outputA and B are the same, means rotate
     }
     Serial.print("Position: ");////////
     Serial.println(counter);/////////
  }
  delay(5000);
  motorreverse1();
 }


//motor forward
   void motorforward1(){
   digitalWrite(7,LOW);
   digitalWrite(8,HIGH);
   int value1 = val+(val_AD/8);//P control of motor1 speed
   analogWrite(9,value1);
    delay(delaytime);
    }
     void motorforward2(){
      digitalWrite(10,LOW);
    digitalWrite(11,HIGH);
    int value2 = VAL+(val_AD/8);
    analogWrite(12,value2);
    delay(delaytime);
       }
     void motorreverse1(){
   digitalWrite(7,HIGH);
   digitalWrite(8,LOW);
   analogWrite(9,val);
    delay(delaytime);
     }
     void motorreverse2(){
    digitalWrite(10,HIGH);
    digitalWrite(11,LOW);
    analogWrite(12,VAL);
    delay(delaytime);
     }

  //motor stop
   void motorBrake()  {
   digitalWrite(7, LOW);
   digitalWrite(8, LOW);
   digitalWrite(10,LOW);
   digitalWrite(11,LOW);
   }
    //Digital value of sensor
    void sensor()
    {
    Serial.println("Digital value of sensor");
  if(digitalRead(SENSORD) == HIGH)
  {
    delay(10);
    if(digitalRead(SENSORD) == HIGH)
    {
      while(digitalRead(SENSORD) == HIGH);
         Serial.println("threhold of sensor");
      KEY_NUM = 1;
    }
  if(KEY_NUM == 1)
  {
    KEY_NUM = 0;
    Serial.println("press!");///////////////////////////////
  }
 
}
 
  //analog value of sensor
    val_AD = 1023 - analogRead(SENSORA);//detect analog number can be used to P control.
  Serial.print("AD = ");////////////////////////////////////
  Serial.println(val_AD); //////////////////////////////
  delay(1000);
    }
       // LED
   void [color=red]led[/color]()  {
   digitalWrite(13,HIGH);
   Serial.println("LED!");///////////////////////////////
   motorBrake();
   }

[color=red][/color]

johnwasser

You are missing a '}' at the end of positionx() so any functions after that are illegally inside the positionx() function.
Code: [Select]


// Digital value of sensor
#define SENSORD 26
//int count = 0;
int KEY_NUM = 0;

//analog value of sensor
#define SENSORA A1
int val_AD = 0;
// LED

//DC motor with encoder
#define outputA 24//interrupt pin
#define outputB 22//input pin
int counter = 0;
int aState;
int aLastState;
int val = 50;//pressure speed->motor 1
int delaytime = 20;
int VAL = 50;//stress speed->motor 2

void setup()
{
  pinMode(SENSORD, INPUT); //sensor digital value
  pinMode(SENSORA, INPUT); //sensor analog value
  pinMode(outputB, INPUT); //encoder signal 1
  pinMode(outputA, INPUT);
  pinMode(7, OUTPUT); //motor positive pole
  pinMode(8, OUTPUT); //motor negative pole
  pinMode(9, OUTPUT); //motor speed
  pinMode(10, OUTPUT); //motor positive pole
  pinMode(11, OUTPUT); //motor negative pole
  pinMode(12, OUTPUT); //motor speed
  pinMode(13, OUTPUT); //led pin
  Serial.begin (9600);   // Reads the initial state of the outputA

  digitalWrite(34, HIGH); //signal to make motor start rotate
  attachInterrupt(34, positionx, HIGH);
  attachInterrupt(26, led, HIGH); //thredhold
  attachInterrupt(26, motorBrake, HIGH);//forward brake
  attachInterrupt(32, motorreverse1, HIGH);//reverse begin
  attachInterrupt(30, motorBrake, HIGH);//reverse brake

}
void loop()
{
  sensor();
  // Serial.print("Pulse:");
  // Serial.println(counter);
}

//motor attacth
void positionx()
{
  for ( int i = 1; i <= 5; i++ )
  {
    aLastState = digitalRead(outputA);
    motorforward1();
    Serial.print("access positionx");///////////////////////
    aState = digitalRead(outputA); // Reads the "current" state of the outputA
    // If the previous and the current state of the outputA are different, that means a Pulse has occuredL
    Serial.println(aLastState);/////////////
    Serial.println(aState);
    if (aState != aLastState)
    {
      // If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
      int M = digitalRead(outputB);
      Serial.println(M);////////////////////

      if (M != aState)
      {
        counter ++;
      }
      else
      {
        counter --;//rotation direction: after each pulse if outputA and B are the same, means rotate
      }
      Serial.print("Position: ");////////
      Serial.println(counter);/////////
    }
    delay(5000);
    motorreverse1();
  }
}  // This was missing

//motor forward
void motorforward1()
{
  digitalWrite(7, LOW);
  digitalWrite(8, HIGH);
  int value1 = val + (val_AD / 8); //P control of motor1 speed
  analogWrite(9, value1);
  delay(delaytime);
}

void motorforward2()
{
  digitalWrite(10, LOW);
  digitalWrite(11, HIGH);
  int value2 = VAL + (val_AD / 8);
  analogWrite(12, value2);
  delay(delaytime);
}

void motorreverse1()
{
  digitalWrite(7, HIGH);
  digitalWrite(8, LOW);
  analogWrite(9, val);
  delay(delaytime);
}

void motorreverse2()
{
  digitalWrite(10, HIGH);
  digitalWrite(11, LOW);
  analogWrite(12, VAL);
  delay(delaytime);
}

//motor stop
void motorBrake()
{
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
}

//Digital value of sensor
void sensor()
{
  Serial.println("Digital value of sensor");
  if (digitalRead(SENSORD) == HIGH)
  {
    delay(10);
    if (digitalRead(SENSORD) == HIGH)
    {
      while (digitalRead(SENSORD) == HIGH);
      Serial.println("threhold of sensor");
      KEY_NUM = 1;
    }
    if (KEY_NUM == 1)
    {
      KEY_NUM = 0;
      Serial.println("press!");///////////////////////////////
    }

  }

  //analog value of sensor
  val_AD = 1023 - analogRead(SENSORA);//detect analog number can be used to P control.
  Serial.print("AD = ");////////////////////////////////////
  Serial.println(val_AD); //////////////////////////////
  delay(1000);
}

// LED
void led()
{
  digitalWrite(13, HIGH);
  Serial.println("LED!");///////////////////////////////
  motorBrake();
}
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

PaulS

Quote
For covenience, I have spread red color on the "led" code.
As you can see, that was useless, since nothing turned red.

It would have been better to post the ACTUAL error message, with line number.
The art of getting good answers lies in asking good questions.


Go Up