Skip to content

Instantly share code, notes, and snippets.

@yeffrimic
Created February 5, 2024 19:43
Show Gist options
  • Save yeffrimic/fb2fbb7a4c3342109097687a461942a0 to your computer and use it in GitHub Desktop.
Save yeffrimic/fb2fbb7a4c3342109097687a461942a0 to your computer and use it in GitHub Desktop.
#define salida1 5
#define salida2 4
#define pwm 7
#define distanciaPorPulso 0.72 //mm
float distanciaEnMM = 0;
const int channelPinA = 2;
int PPR = 20;
volatile int ISRCounter = 0;
unsigned int pulsos = 0;
unsigned long Time = 0;
unsigned int RPM = 0;
void doEncode() {
ISRCounter++;
}
void setup() {
pinMode(salida1, OUTPUT);
pinMode(salida2, OUTPUT);
pinMode(pwm, OUTPUT);
analogWrite(pwm, 255);
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
pinMode(channelPinA, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(channelPinA), doEncode, CHANGE);
Time = millis();
}
void loop() {
if (Serial.available() > 0) {
int dato = Serial.readString().toInt();
int pulsosNecesarios = ((dato) / (2 * 3.14 * 1.25)) * 20;
float pulsosNecesarios2 = ((dato) / (2 * 3.14 * 1.25)) * 20;
Serial.println(dato);
Serial.println(pulsosNecesarios);
Serial.println(pulsosNecesarios2);
while (pulsosNecesarios2 >= pulsos) {
pulsos = ISRCounter ;
digitalWrite(salida2, HIGH);
Serial.println(pulsos);
}
digitalWrite(salida2, LOW);
pulsos = 0;
ISRCounter=0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment