Hello everyone!
I’m having trouble combining two different sketches, namely a basic Ohm Meter Sketch and a Motor Stepper Sketch.
The wiring works well when run separately but after merging, only the Stepper works.
Here is my merged sketch code.
Hope someone can take a look and let me know if there is some way of fixing it.
#include <Stepper.h>
/*an exercise in combining code
*/
void setup(){
setupStepper();
setupOhm();
Serial.begin(9600);
}
void loop(){
loopStepper();
loopOhm();
}
//Constants
const int stepsPerRevolution = 200; //Motor steps per rotation
const int revolutions = 6000;
int steps = stepsPerRevolution * revolutions;
const int stepPin = 3; //Step
const int dirPin = 4; //Direction
int stepCount = 0;
int analogPin= 0; //Ohm Meter
int raw= 0; // Raw reading of the voltage divider
int Vin= 5;
float Vout= 0; // reading of the voltage divider
float R1= 10000; // can change to 1K or 100K resistor improve accuracy
float R2= 0; // Resistance of the unknown resistor
float buffer= 0;
void setupStepper(){
//Setup the pins as outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
}
void loopStepper(){
//Set motor direction clockwise
digitalWrite(dirPin, LOW);
for (int i = 0; i < steps; i++){
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
}
}
void setupOhm()
{
Serial.begin(9600);
}
void loopOhm()
{
raw= analogRead(analogPin);
if(raw)
{
buffer= raw * Vin;
Vout= (buffer)/1024.0;
buffer= (Vin/Vout) -1;
R2= R1 * buffer;
Serial.print("Vout: ");
Serial.println(Vout);
Serial.print("Measured Resistance in Ohms: ");
Serial.println(R2);
delay(1000);
}
}
StepperControl_OhmMeter.ino (1.34 KB)