Hi everyone,
I'm fairly new when it comes to programming and am currently working on a project. I have two separate codes that are working perfectly but I would like to join them together into the one code. The first code runs two motors at the push of a button and the second is a hall effect sensor to detect the rotation of a wheel. I'm just wondering how I would be able to do that and whats the best way to go about it.
int E1 = 5;
int E2 = 6;
int M1 = 4;
int M2 = 7;
int pushButton = 2;
void setup()
{
pinMode(pushButton, INPUT);
Serial.begin(9600);
}
void loop()
{
int buttonState = digitalRead(pushButton);
int potValue = analogRead(A0);
int pwmOutput = map(potValue, 0, 1023 , 0 , 255);
buttonState = HIGH;
if (buttonState == HIGH)
digitalWrite(M1, HIGH);
else
digitalWrite(M1, LOW);
analogWrite(E1, pwmOutput);
analogWrite(E2, pwmOutput);
}
volatile byte half_revolutions;
unsigned int rpm;
unsigned long timeold;
void setup()
{
Serial.begin(9600);
attachInterrupt(0, magnet_detect, RISING);
half_revolutions = 0;
rpm = 0;
timeold = 0;
}
void loop()//Measure RPM
{
if (half_revolutions >= 20) {
rpm = 30*1000/(millis() - timeold)*half_revolutions;
timeold = millis();
half_revolutions = 0;
//Serial.println(rpm,DEC);
}
}
void magnet_detect()
{
half_revolutions++;
Serial.println("detect");
}