Pages: [1]   Go Down
Author Topic: Help please  (Read 636 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am doing a speed sensor project using optocoupler to count pulses  , I uses Color Graphic LCD with I2C Interface here is the link of the lcd http://www.web4robot.com/GraphicLCD.html     I don't know how to write a code to  link the optocoupler with the lcd  to print the rpm of a dc motor

http://www.web4robot.com/files/GLCD-FLEXEL.pdf

http://www.web4robot.com/files/GLCD-I2C.zip
« Last Edit: May 05, 2013, 04:42:50 am by mohamed92 » Logged

Offline Offline
Faraday Member
**
Karma: 65
Posts: 2500
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I could probably guess, but, what's the role of the opto-coupler here?
(It's more fun for me to dig the details out of you.)
Logged

"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

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

I'm using optocoupler to count the RPM of a DC motor and her is the code , The problem is that the LCD write RPM=0 however i run the dc motor

Quote
             
volatile byte rpmcount;
unsigned int rpm;
unsigned long timeold;

// include the library code:
#include <GLCDI2C.h>
#include <Wire.h>

// Color definitions
#define   BLACK           0x0000
#define   BLUE            0x001F
#define   RED             0xF800
#define   GREEN           0x07E0
#define CYAN            0x07FF
#define MAGENTA         0xF81F
#define YELLOW          0xFFE0  
#define WHITE           0xFFFF


unsigned int maxX = 160;  // LCD x-resolution
unsigned int maxY = 128;  // LCD y-resolution
unsigned char i2cAddress = 0x46;  // LCD module I2C address
unsigned int counter;
unsigned int tmp;
GLCDI2C lcd = GLCDI2C(maxX, maxY, i2cAddress);


void rpm_fun()
 {
  
      rpmcount++;
 }

void setup()
 {
   delay(500);
  Wire.begin();  
  lcd.init();
  
   attachInterrupt(0, rpm_fun, FALLING);

  

   rpmcount = 0;
   rpm = 0;
   timeold = 0;
 }

 void loop()
 {
  
   delay(1000);
  
   detachInterrupt(0);
  
   rpm = 30*1000/(millis() - timeold)*rpmcount;
   timeold = millis();
   rpmcount = 0;

  
   delay(500);
   lcd.setColor(BLACK);
   lcd.clear();
   lcd.fontType(FONT_SMALL);
   lcd.setColor(WHITE);
   lcd.cursor(15,15);
  
   lcd.print("RPM=");
   lcd.print(rpm);
   delay(3000);

  
   attachInterrupt(0, rpm_fun, FALLING);
  }
« Last Edit: May 05, 2013, 04:52:24 am by mohamed92 » Logged

Offline Offline
Faraday Member
**
Karma: 65
Posts: 2500
Now, More Than Ever
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The problem is that the LCD write RPM=0 however i run the dc motor
Is that because you have rpmcount=0 in your void loop( ) ?

Code:
void loop()
 { 
    delay(1000); 
    detachInterrupt(0);
    rpm = 30*1000/(millis() - timeold)*rpmcount;
    timeold = millis();
    rpmcount = 0;     \\ <<<<<<<< rpmcount=0!
Logged

"Hello, I must be going..."
"You gotta fight -- for your right -- to party!"
Don't react - Read.
"Who is like unto the beast? who is able to make war with him?"

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13668
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The use of delay is not adviced but OK,

 I have rewritten the code a bit
- more clear that you measure 1 second
- add a bit of debugging info
- reformulated the core formula

give it a try, code not tested smiley-wink
Code:
// vars here

void rpm_fun()
{
  rpmcount++;
}

void setup()
{
  Serial.begin(115200); // debugging output
  Wire.begin(); 
  lcd.init();

  rpmcount = 0; // set it before the ISR is attached
  timeold = 0;
}

void loop()
{
  // measure for about 1000 milliseconds
  timeold = millis();
  attachInterrupt(0, rpm_fun, FALLING);
  delay(1000);
  detachInterrupt(0);
  unsigned long now = millis();

  // do some math
  unsigned long duration = now - timeold;
  rpm = rpmcount *30UL * 1000UL/duration;       // changed this formula notice the UL for Unsigned Long
  rpmcount = 0;  // reset

  // debugging
  Serial.print("duration=");
  Serial.println(duration);
  Serial.print("rpmcount=");
  Serial.println(rpmcount);
  Serial.print("rpm=");
  Serial.println(rpm);

  // display results
  lcd.setColor(BLACK);
  lcd.clear();
  lcd.fontType(FONT_SMALL);
  lcd.setColor(WHITE);
  lcd.cursor(15,15);
  lcd.print("RPM=");
  lcd.print(rpm);
}
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: