Pages: [1]   Go Down
Author Topic: Motor position control using single input encoder  (Read 468 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying to control a 24V DC motor position using a single input encoder. The encoder wheel has got 30 spikes on it. It is connected to the interrupt 0 pin. I am trying to move the motor first to a forward position (theta1 = 120deg) from a home position. Then back to the home position from where it has to be moved to a backward position (theta1D = 90deg). I have written a code for the same but while compiling it gives an error on line 6 as
 error : expected unqualified-id before '{' token
The code is as shown below:
Code:
int encoder1Pin = 2;
int motor1PinA = 3;
int motor1PinB = 4;
volatile int encoder1Pos = 0;

{
 void setup ()
 {
   pinMode(encoder1Pin,INPUT);
   pinMode (motor1PinA,OUTPUT);
   pinMode (motor1PinB,OUTPUT);
   attachInterrupt(0, ISR, CHANGE);
   int theta1 = 120;
   int theta1D = 90;
 }
    int motor1Forward();
   {
     digitalWrite(motor1PinA,HIGH);
     {
     if(encoder1Pos*6 == theta1);
     }
     digitalWrite(motor1PinA,LOW);
     encoder1Pos = 0;
   }
   int motor1Home();
   {
     digitalWrite(motor1PinB,HIGH);
     {
       if(encoder1Pos*6 == theta1);
     }
     digitalWrite(motor1PinB,LOW);
     encoder1Pos = 0;
   }
   int motor1Backward();
   {
      digitalWrite(motor1PinB,HIGH);
     {
       if(encoder1Pos*6 == theta1D);
     }
     digitalWrite(motor1PinB,LOW);
     encoder1Pos = 0;
   }
}
{
   void ISR()
   {
     encoder1Pos++
   }
}
« Last Edit: March 06, 2014, 01:00:51 pm by ajbk » Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4255
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The line that it errors on has an opening brace on it.  Why ?

The outer braces in this code section need to go as well
Code:
{
  void ISR()
  {
    encoder1Pos++
  }
}
As do the semi-colons at the end of the function names such as this one (and others)
Code:
int motor1Home();
There may be other problems but I stopped looking.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

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

Thanks UKHeliBob..
I modified the code as shown below. But still errors.
Code:
int encoder1Pin = 2;
int motor1PinA = 3;
int motor1PinB = 4;
volatile int encoder1Pos = 0;
int theta1 = 120;
int theta1D = 90;

void setup() {
  pinMode(encoder1Pin,INPUT);
  pinMode (motor1PinA,OUTPUT);
  pinMode (motor1PinB,OUTPUT);
  attachInterrupt(0, encoder1svc, CHANGE);
}

void loop()
{
int motor1Forward()
{
  digitalWrite(motor1PinA,HIGH);
  {
    if(encoder1Pos*6 == theta1);
  }
  digitalWrite(motor1PinA,LOW);
  encoder1Pos = 0;
}
int motor1Home()
{
  digitalWrite(motor1PinB,HIGH);
  {
    if(encoder1Pos*6 == theta1);
  }
  digitalWrite(motor1PinB,LOW);
  encoder1Pos = 0;
}
int motor1Backward()
{
  digitalWrite(motor1PinB,HIGH);
  {
    if(encoder1Pos*6 == theta1D);
  }
  digitalWrite(motor1PinB,LOW);
  encoder1Pos = 0;
}
void encoder1svc()
{
  encoder1Pos++;
}
}







Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4255
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have got several function definitions within the loop() function.  In fact there is nothing in the loop() function apart from function definitions.
« Last Edit: March 06, 2014, 04:59:18 pm by UKHeliBob » Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Pages: [1]   Go Up
Jump to: