Pages: [1]   Go Down
Author Topic: control rpm of a motor  (Read 1010 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 1
Posts: 149
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,
i have  a problem. I want to control thr rpm of a DC Motor with a Photoelectric sensor. My Idea  is as follows:
Every 10 seconds the number of Pulses  per second  from the photoelectric sensor should be counted. Can some one help me please to write this function.

Thank you
Logged

The Netherlands
Offline Offline
Edison Member
*
Karma: 42
Posts: 1494
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sure.
But to help, we need to see what you have already and for you to tell us what exact problem you have.
Do you really need the 10 seconds ? (a lot of time)
Logged

Have a look at "blink without delay".
Did you connect the grounds ?
Je kunt hier ook in het Nederlands terecht: http://arduino.cc/forum/index.php/board,77.0.html

Offline Offline
Full Member
***
Karma: 1
Posts: 149
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i though of 5-10 seconds. Can I do it with a Timer Interrupt?
Logged

The Netherlands
Offline Offline
Edison Member
*
Karma: 42
Posts: 1494
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As my signoff tells:
Try the "blink without delay" sketch, and see what that's about.
Once you understand that, you'll know how to do your periodical RPM test without interfering (too much) with the rest of your sketch.
Logged

Have a look at "blink without delay".
Did you connect the grounds ?
Je kunt hier ook in het Nederlands terecht: http://arduino.cc/forum/index.php/board,77.0.html

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

so here's a simple sketch that counts a photointerrupt and Serial.prints the result
Code:
/*  Photo inturrupt with phototransister RPT-38PB3F
 taken from Fan Timer circuit http://www.neufeld.newton.ks.us/electronics/?p=244
 
metasphere72_091413
 
 */

volatile static long counter = 0;

void setup() {
  Serial.begin(9600);
  attachInterrupt (0, count, CHANGE);
}

void loop(){
  delay(500);
  Serial.println(counter);
  counter = 0;
}
void count() {
  ++ counter;
}

it's really simple but I was also advised that when using a counter I needed to do the Counting in a SAFE way.
like this sketch that shows how  to create a safe count variable to count the pulses without any interruptions.
Code:
/*  Photo inturrupt with phototransister RPT-38PB3F
 taken from Fan Timer circuit http://www.neufeld.newton.ks.us/electronics/?p=244
 altered to work Four LED's when count gets higher.
 Also this sketch incorporates a servo that moves an analog meter needle up and down in proportion to
 interrupt count.
 
 RPT-38PB3F phototransister Pins:
 Blk wire on Emmitter - TO Arduino GROUND PIN with 100k R in series.
 Red wire on Collector - TO Arduino 5v PIN
 Yellow signal Wire - connect to (Blk)ground before the 100k R TO DIGITAL PIN 2
 LED units:
 Powering the LED's are TIP-120 darlingtons running 12VDC. The Signal output comes from Arduino Digital Out Pins.
 PINS 6, 7, 8, 9 and 10.
 
 Sketch hacked and written by: metasphere72_11/13/13
 
 */
#include <Servo.h>  // include servo library
#define ServoMIN 29  // Don't go to end of servo travel which is 0
#define ServoMAX 145  // which may be 0 to 180.

Servo myservo;   // servo object to control a servo
int val; // variable to store the servo position

unsigned long counter = 0;
unsigned long countVal = 0;
int ledPin01 = 6;
int ledPin02 = 7;
int ledPin03 = 8;
int ledPin04 = 9;
int ledPin05 = 10;
boolean cheatState = 0;
long previousMillis = 0;
long interval = 6000;


void setup() {
  Serial.begin(9600);
  myservo.attach(5);
  myservo.write(30);
  attachInterrupt (0, count, CHANGE);
  attachInterrupt (1, count, CHANGE);

  pinMode(ledPin01, OUTPUT);
  pinMode(ledPin02, OUTPUT);
  pinMode(ledPin03, OUTPUT);
  pinMode(ledPin04, OUTPUT);
  pinMode(ledPin05, OUTPUT);
}

void loop(){
counter = 0;
  noInterrupts();// This creates a safe count variable
  countVal = counter;
  interrupts();
  delay(50);
  countVal = (counter - countVal);// calculates the number of interrupts since last poll
  // Serial.println(counter);
 // Serial.println(countVal);
 
  // Normal Settings
if (cheatState = 0){
 
  if(countVal >=5  && countVal <=9)  {
    digitalWrite(ledPin01, HIGH);
  }
  else{
    digitalWrite(ledPin01, LOW);

  }
  if(countVal >=10 && countVal <=18)  { 
    digitalWrite(ledPin02, HIGH);

  }
  else{
    digitalWrite(ledPin02, LOW);
  }
  if(countVal >=19 && countVal <=29)
  {
    digitalWrite(ledPin03, HIGH);
  }
  else
  {
    digitalWrite(ledPin03, LOW);
  }
  if(countVal >=30 && countVal <=40)
  {
    digitalWrite(ledPin04, HIGH);
  }
  else
  {
    digitalWrite(ledPin04, LOW);
  }
  if(countVal >=41 && countVal <=100)
  {
    digitalWrite(ledPin05, HIGH);
  }
  else
  {
    digitalWrite(ledPin05, LOW);
  }
 val = (countVal);
 val = map(val, 0, 45, 30, 140);
    myservo.write(val);
 }
}
void count() {
  ++ counter;
}
 unsigned long currentMillis = millis();


hope this helps.
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 149
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you. I Use an Interrupt to count the  general cycles with the function ( 0, count, RISING),

i would like use something  like this:
void pulses()
     {
       for (int z = 0; z<10;z++)
       {
        pulses += digitalRead(lightsensor)
   and then
 timer start();
 pulses();
timer stop();

 
Logged

Pages: [1]   Go Up
Jump to: