I am using code to move a step motor.
What I am trying to understand is how to print out the power usage required when doing the work.
Example:
3 volts and 1 amp was required to move the motor.
When I hold the motor on the 2nd time it moves, and force the amperage to go up to spin the motor, I would like to capture that power increase and print it out. I am not sure where to start with getting that data.
Thanks in advanced for any help.
Code:
// constants won't change. Used here to
// set pin numbers:
const int ledPin = 13; // the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000; // interval at which to blink (milliseconds)
// reading sound information
int echoPin = 4;
int trigPin = 5;
#include <Stepper.h>
int stepIN1Pin = 8;
int stepIN2Pin = 9;
int stepIN3Pin = 10;
int stepIN4Pin = 11;
int stpspeed = 20;
int stepsPerRevolution = 2048;
Stepper myStepper(stepsPerRevolution, stepIN1Pin, stepIN3Pin, stepIN2Pin, stepIN4Pin);
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
// set up serial
Serial.begin(9600);
// set the pinmode on our ultrasonic echo, and tric pins
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
// set the RPM
myStepper.setSpeed(10);
}
void loop() {
float distanceCentimeters;
int pulseLenMicroseconds;
// bit-bang a small square wave
// on the trig pin to start the range
// finder
digitalWrite(trigPin, LOW);
//delayMicroseconds(20);
digitalWrite(trigPin, HIGH);
//delayMicroseconds(100);
digitalWrite(trigPin, LOW);
// measure the pulse length from the echo pin
pulseLenMicroseconds = pulseIn(echoPin, HIGH);
// calculate the distance using the speed of sound
distanceCentimeters = pulseLenMicroseconds / 29.387 / 2;
// print it out over serial
if ( distanceCentimeters <= 5 && distanceCentimeters >= 0 ) {
Serial.println("Move Slide Out until MAX distance is reached: ");
Serial.println(distanceCentimeters);
ledState = HIGH;
digitalWrite(ledPin, ledState);
// step one revolution in one direction
myStepper.setSpeed(10);
myStepper.step(stepsPerRevolution);
// wait a second
//delay(1000);
} else if ( distanceCentimeters >= 5 && distanceCentimeters <= 10 ) {
ledState = LOW;
digitalWrite(ledPin, ledState);
Serial.println("Slide is in Stop Max Distance reached.");
//myStepper.step(0);
//Serial.println("Slide is Out until Max Distance reached.");
myStepper.setSpeed(10);
myStepper.step(-stepsPerRevolution);
Serial.println(distanceCentimeters);
} else if (distanceCentimeters <= 0 || distanceCentimeters >= 10) {
Serial.println("Slide is in Stop Max Distance reached.");
Serial.println(distanceCentimeters);
myStepper.setSpeed(0);
}
//delay(100);
}