Hi friends!
I have a question. I have programmed a code that allow, without interruptions, to know if the engine turn clockwise or counterclockwise. It allows you to calculate the steps in one way and in the other way. The code is working very fine.
The code works for the motors that I show you: 6V, 210 rpm & DC.
The question: Maybe isn’t difficult (I need to improve my knowledge of Arduino computing) but I want to know the ratio of steps per second. In other words, I want to take the number steps every 1000 ms (for example) and do the next calculation:
• Time: 2513 --- step: +2380
• Time: 3515 ---step: +8543
Steps/second = (8543 – 2380) /1000.
I cannot get two measurements in two different times! Please I need your help!
I don’t need to know the exactly rpm, is enough to know the ratio of steps for second. If engine is stopped the steps/second will be 0.
motor 6v 210 rpm
//lecturas digitales
const int channelPinA = 8; //Motor RH (VERDE)
const int channelPinB = 12; //Motor RH (AMARILLO)
const int channelPinC = 7; //Motor LH (AMARILLO)
const int channelPinD = 6; //Motor LH (VERDE)
unsigned char stateChannelA;//Motor RH
unsigned char stateChannelB;//Motor RH
unsigned char stateChannelC;//Motor LH
unsigned char stateChannelD;//Motor LH
int estado_previo, estado_dos;//Motor RH
int value; //Motor RH
int prevValue; //Motor RH
int estado_previoL, estado_dosL;//Motor LH
int valueL; //Motor LH
int prevValueL; //Motor LH
const int timeThreshold = 1;
unsigned long currentTime;
unsigned long loopTime;
//Motor RH
int IN2 = 5;
int IN1 = 4;
int ENA = 3;
//motor LH
int IN3 = 11;
int IN4 = 10;
int ENB = 9;
void setup() {
// put your setup code here, to run once:
Serial.begin(57600);
pinMode(IN1, OUTPUT); //Motor RH
pinMode(IN2, OUTPUT); //Motor RH
pinMode(ENA, OUTPUT); //Motor RH
pinMode(IN3, OUTPUT); //Motor LH
pinMode(IN4, OUTPUT); //Motor LH
pinMode(ENB, OUTPUT); //Motor LH
pinMode(channelPinA, INPUT);
pinMode(channelPinB, INPUT);
currentTime = millis();
loopTime = currentTime;
value = 0;
}
void loop() {
digitalWrite(IN1, LOW); //Motor RH
digitalWrite(IN2, HIGH); //Motor RH
analogWrite(ENA, 255); //Motor RH
digitalWrite(IN3, LOW); //Motor LH
digitalWrite(IN4, HIGH); //Motor LH
analogWrite(ENB, 255); //Motor LH
currentTime = millis();
if (currentTime >= (loopTime + timeThreshold))
{
stateChannelA = digitalRead(channelPinA);
stateChannelB = digitalRead(channelPinB);
stateChannelC = digitalRead(channelPinC);
stateChannelD = digitalRead(channelPinD);
//Ánalisis del motor RH
if ((estado_previo == stateChannelB) && estado_dos != stateChannelB) {
if ((stateChannelA == LOW) && (stateChannelB == LOW)) { //Sentido horario
value++;
}
if ((stateChannelB == LOW) && (stateChannelA == HIGH)) { //Sentido antihorario
value--;
}
estado_previo = stateChannelB;
}
if (prevValue != value)
{
prevValue = value;
Serial.print("Pasos RH: ");
Serial.println(value);
}
}
if (millis - currentTime >= 1) {
estado_dos = stateChannelB;
}
//Ánalisis del motor LH
if ((estado_previoL == stateChannelD) && estado_dosL != stateChannelD) {
if ((stateChannelC == LOW) && (stateChannelD == LOW)) { //Sentido horario
valueL++;
}
if ((stateChannelD == LOW) && (stateChannelC == HIGH)) { //Sentido antihorario
valueL--;
}
estado_previoL = stateChannelD;
}
if (prevValueL != valueL)
{
prevValueL = valueL;
Serial.print("Pasos LH: ");
Serial.println(valueL);
}
if (millis - currentTime >= 1) {
estado_dosL = stateChannelD;
}
}