i want to make motor DC rpm counter and this my script below
"
int enablePin = 11;
int in1Pin = 10;
int in2Pin = 9;
int switchPin = 7;
int potPin = 0;
int statusPin= 13;
int encoder_pin = 2;
unsigned int rpm = 0;
float velocity = 0;
volatile byte pulses = 0;
unsigned long timeold = 0;
unsigned int pulsesperturn = 20;
const int wheel_diameter = 64;
static volatile unsigned long debounce = 0;
void setup()
{
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(switchPin, INPUT_PULLUP);
pinMode(statusPin,OUTPUT);
Serial.begin(9600);
pinMode(encoder_pin, INPUT);
attachInterrupt(0, counter, RISING);
pulses = 0;
rpm = 0;
timeold = 0;
Serial.print("Seconds ");
Serial.print("RPM ");
Serial.print("Pulses ");
Serial.println("Velocity[Km/h]");}
}
void loop()
{
digitalWrite(13,HIGH);
int speed = analogRead(potPin) / 4;
boolean reverse = digitalRead(switchPin);
setMotor(speed, reverse);
if (millis() - timeold >= 1000)
{
noInterrupts();
rpm = (60 * 1000 / pulsesperturn )/ (millis() - timeold)* pulses;
velocity = rpm * 3.1416 * wheel_diameter * 60 / 1000000;
timeold = millis();
Serial.print(millis()/1000); Serial.print(" ");
Serial.print(rpm,DEC); Serial.print(" ");
Serial.print(pulses,DEC); Serial.print(" ");
Serial.println(velocity,2);
pulses = 0;
interrupts();
}
}
void setMotor(int speed, boolean reverse)
{
analogWrite(enablePin, speed);
digitalWrite(in1Pin, ! reverse);
digitalWrite(in2Pin, reverse);
}
void counter()
{
if( digitalRead (encoder_pin) && (micros()-debounce > 500) && digitalRead (encoder_pin) )
{
debounce = micros();
pulses++;
}
else ;
}
"
i kept stuck on that slashed code "'counter' was not declared in this scope"
Please use code block:
int enablePin = 11;
int in1Pin = 10;
int in2Pin = 9;
int switchPin = 7;
int potPin = 0;
int statusPin= 13;
int encoder_pin = 2;
unsigned int rpm = 0;
float velocity = 0;
volatile byte pulses = 0;
unsigned long timeold = 0;
unsigned int pulsesperturn = 20;
const int wheel_diameter = 64;
static volatile unsigned long debounce = 0;
void setup()
{
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(switchPin, INPUT_PULLUP);
pinMode(statusPin,OUTPUT);
Serial.begin(9600);
pinMode(encoder_pin, INPUT);
attachInterrupt(0, counter, RISING);
pulses = 0;
rpm = 0;
timeold = 0;
Serial.print("Seconds ");
Serial.print("RPM ");
Serial.print("Pulses ");
Serial.println("Velocity[Km/h]");}
}
void loop()
{
digitalWrite(13,HIGH);
int speed = analogRead(potPin) / 4;
boolean reverse = digitalRead(switchPin);
setMotor(speed, reverse);
if (millis() - timeold >= 1000)
{
noInterrupts();
rpm = (60 * 1000 / pulsesperturn )/ (millis() - timeold)* pulses;
velocity = rpm * 3.1416 * wheel_diameter * 60 / 1000000;
timeold = millis();
Serial.print(millis()/1000); Serial.print(" ");
Serial.print(rpm,DEC); Serial.print(" ");
Serial.print(pulses,DEC); Serial.print(" ");
Serial.println(velocity,2);
pulses = 0;
interrupts();
}
}
void setMotor(int speed, boolean reverse)
{
analogWrite(enablePin, speed);
digitalWrite(in1Pin, ! reverse);
digitalWrite(in2Pin, reverse);
}
void counter()
{
if( digitalRead (encoder_pin) && (micros()-debounce > 500) && digitalRead (encoder_pin) )
{
debounce = micros();
pulses++;
}
else ;
}
Then, you may realize that there are two curly braces at the end of setup(). There should only be one.
Count your braces. They do not match, so the loop function finishes early and the compiler does not see the rest of the code, and that includes the definition of counter.
Grumpy_Mike:
Count your braces. They do not match, so the loop function finishes early and the compiler does not see the rest of the code, and that includes the definition of counter.
Oops Mike. Didn't know you were looking at it too.
This could be different styles of where to put curly braces. I always put mine in a separate line. Others like to keep it behind a function name and then after the last line of code in the function.