Which Arduino board are you using? How are you getting revolutions? With hall sensor or opto-reflecting one?
Here is my code that gets RPM for 3 motors and get torque from RS485.
Here is photo of my work.
And the code used. You can adjust it for your need. mySerial
used for RS485 so you can delete it and MegunoLink i used to plot data in PC.
#include <SoftwareSerial.h>
#include <MegunoLink.h>
//Software serial for 485 communication : Tx = pin 9, Rx = pin 8.
SoftwareSerial mySerial(9,8);
TimePlot MyPlot;
//CONSTANTS:
//Pin number for Vishay Telefunken Opto-reflecting TCRT1000 sensors:
Message MyCSVMessage("Stendas");
#define TCRT1 12
#define TCRT2 11
#define TCRT3 10
String readString;
String momentas1;
String momentas2;
String momentas3;
//Number of pulse changes per revolutuion:
long PulsesPerRevolution1 = 78;
long PulsesPerRevolution2 = 192;
long PulsesPerRevolution3 = 82;
long minute = 60;
//VARIABLES:
//Number of pulses counted:
long PulseCount1=0;
long PulseCount2=0;
long PulseCount3=0;
//Calculated rotations per minute:
long Rpm1 =0;
long Rpm2 =0;
long Rpm3 =0;
//Time saved to compare
unsigned long TimeOld =0;
//Achieved signals saved:
boolean Status1 =0;
boolean Status2 =0;
boolean Status3 =0;
//Signals saved to compare:
boolean StatusOld1 =0;
boolean StatusOld2 =0;
boolean StatusOld3 =0;
void setup() {
//Begin serial communication with BAUD rate 9600bps:
Serial.begin(9600);
mySerial.begin(57600);
//Set input pins for TCRT1000:
pinMode(TCRT1, INPUT);
pinMode(TCRT2, INPUT);
pinMode(TCRT3, INPUT);
}
void loop() {
//
//Read and save TCRT1000 status:
Status1=digitalRead(TCRT1);
Status2=digitalRead(TCRT2);
Status3=digitalRead(TCRT3);
//Compare current status with the previous one
//If changed, then increment the counting:
if (StatusOld1!=Status1){
StatusOld1=Status1;
PulseCount1++;
}
if (StatusOld2!=Status2){
StatusOld2=Status2;
PulseCount2++;
}
if (StatusOld3!=Status3){
StatusOld3=Status3;
PulseCount3++;
}
//Compare time if it exceeds 1s:
if (millis()-TimeOld>=1000){
//Get data from RS485:
READ01();
momentas1=between3rdAnd4th(momentas1);
delay(20);
READ02();
momentas2=between3rdAnd4th(momentas2);
delay(20);
READ03();
momentas3=between3rdAnd4th(momentas3);
delay(20);
momentas1.trim();
momentas2.trim();
momentas3.trim();
//Calculate RPM:
Rpm1=PulseCount1*minute/PulsesPerRevolution1;
Rpm2=PulseCount2*minute/PulsesPerRevolution2;
Rpm3=PulseCount3*minute/PulsesPerRevolution3;
//Print RPM
Serial.println(String(Rpm1)+ " ; "+String(momentas1)+ " | " +String(Rpm2) + " ; " + String(momentas2) + " | " + String(Rpm3) + " ; " + String(momentas3));
// Serial.println(String(Rpm2) + "," + String(momentas2));
// Serial.println(String(Rpm3) + "," + String(momentas3));
//reset the counting and time
TimeOld=millis();
PulseCount1=0;
PulseCount2=0;
PulseCount3=0;
}
}
//Get data from RS485:
void READ01(){
while (mySerial.available()){
mySerial.read();
}
mySerial.println("01READ");
momentas1="";
delay(20);
while (mySerial.available()) {
char c = mySerial.read();
if (c == '\n'){
break;
}
momentas1 += c;
}
}
void READ02(){
while (mySerial.available()){
mySerial.read();
}
mySerial.println("02READ");
momentas2="";
delay(20);
while (mySerial.available()) {
char c = mySerial.read();
if (c == '\n'){
break;
}
momentas2 += c;
}
}
void READ03(){
while (mySerial.available()){
mySerial.read();
}
mySerial.println("03READ");
momentas3="";
delay(20);
while (mySerial.available()) {
char c = mySerial.read();
if (c == '\n'){
break;
}
momentas3 += c;
}
}
String between3rdAnd4th(String myString) {
int pirmas = myString.indexOf(',');
int antras = myString.indexOf(',', pirmas+1);
int trecias = myString.indexOf(',', antras+1);
int ketvirtas = myString.indexOf(',', trecias+1);
String newStr = myString.substring(antras+1, trecias);
return newStr;
}
If you have any questions about code or want an advice feel free to ask.
** EDIT **
Ok, so i couldn't wait or your urgent answer and continue the work. Connect your hall sensor PULL UP output to pin 2 and try this code:
volatile byte rpmcount;
unsigned int rpm;
unsigned long timeold;
void setup()
{ //Interrupt 0 is digital pin 2
//Triggers on FALLING (change from HIGH to LOW)
attachInterrupt(0, rpm_fun, FALLING);
rpmcount = 0;
rpm = 0;
timeold = 0;
}
void loop()
{
//Update RPM every second
delay(1000);
//Don't process interrupts during calculations
detachInterrupt(0);
//Note that this would be 60*1000/(millis() - timeold)*rpmcount if the interrupt
//happened once per revolution instead of twice. Other multiples could be used
//for multi-bladed propellers or fans
rpm = 60*1000/(millis() - timeold)*rpmcount;
timeold = millis();
rpmcount = 0;
//Print out result
Serial.print("RPM=");
Serial.println(rpm);
//Restart the interrupt processing
attachInterrupt(0, rpm_fun, FALLING);
}
void rpm_fun()
{
//Each rotation, this interrupt function is run
//Update count
rpmcount++;
}
** EDIT 2 **
Debuging of communication
void setup(){
// Open serial communications and wait for port to open:
Serial.begin(9600);
Serial.println("I am ready to send some stuff!");}
void loop(){ // run over and over
if (Serial.available())
Serial.write(Serial.read());}