Calculation always returns 0 serial print

Dear Arduinoers,

I have a potentiometer 10kOhm which I want to use to control a stepper motors (Nema 17 1.8 deg bipolar) RPM. I am using a TB6600 stepper driver. Serial print reads 0 - 1023 from the potentiometer as expected.

When I try to calculate the RPM however the calculation returns 0.

int Pot = A1; // Input pin for the potentiometer.
int RPM; // Declares variable for RPM

void loop(){
Pot = analogRead(A1);
RPM =(Pot/1023)*100

This probably has to do with integer. I don’t want to make it floating though since I use it later on in a formulea to calculate the corresponding delay for the steppermotor to get the required RPM.

  //Enables the motor direction to move in a certain direction.
  digitalWrite(dirPin,LOW);
  //Makes 200 Pulses for making one full cycle (1.8 degree stepper).
  for(int x = 0; x < 200; x++){
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(10^6/((RPM/60)*200*2));
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(10^6/((RPM/60)*200*2));

Any advice on how to fix this problem?!

Driez:

RPM = int(Pot/1023)*100

How much is Pot/1023 converted to an integer? What changes if you first multiply before dividing first cast to a long, then multiply, and then divide?

Also change int Pot = A1 to const int Pot = A1 and you'll discover another bug.

And "10^6" doesn't mean what you think it means. Bitwise operators

Pieter

PieterP:
What changes if you first multiply before dividing?

You stand a good chance of discovering more weird stuff?

PieterP:
How much is Pot/1023 converted to an integer? What changes if you first multiply before dividing first cast to a long, then multiply, and then divide?

Also change int Pot = A1 to const int Pot = A1 and you'll discover another bug.

And "10^6" doesn't mean what you think it means. Bitwise operators

Pieter

Dear Pieter, I have worked around the int problem by changing the RPM calculation into the following formula which also caps my RPM at around 100 RPM.

RPM =(Pot/10);

The Pot/1023 value will always be less then 1 so the integer makes it 0 that's why I knew it was an integer problem.

Why would I change the Pot to a const int?

Thank you, I changed 10^6 to 1000000

Try RPM =(Pot * 100) / 1023;

Try

RPM = (Pot * 100L) / 1023;

As AWOL alluded to, log2(102,300+1) > 15.

PieterP:
Try

RPM = (Pot * 100L) / 1023;

As AWOL alluded to, log2(102,300+1) > 15.

Oh! Forgot about that important little detail.

Cheers guys thanks a lot! I am almost there. Keep in mind it’s my first time programming anything so I really do appreciate all the help! I’ve been looking for this for more than 2 hours on google and forums.

Right now I have a setable RPM value with my potentiometer. I can continue to use this RPM to calculate the required corresponding delayMicroseconds. However I suspect there might be an easier way to do this?

Furthermore do I need to use an acceleration code or are the RPM’s low enough that this isn’t necessary (max 100RPM)?

This is my current code to drive the 17HS24-2104S nema 17 motor using the TB6600 stepper driver at 2A and 200 pulses per revolution.

I take the potentiometers input RPM and divide by 60 to convert it to RPS. Then I multiply this number by 10^6 (1000000) to convert seconds to microseconds. I divide by 200 to get the delay in microseconds per pulse and divide this by 2 to get the delay in microseconds per phase.

void loop(){
  Pot = analogRead(A1);
  //Serial.print(Pot);
  RPM =(Pot*100L)/1023;
  Serial.print(RPM);
  lcd.setCursor(0,0); //Defining position to write from first row, first column.
  lcd.print ("RPM"); // Showing the letters RPM
  lcd.setCursor(0,1); //Defining position to write from second row, first column.
  lcd.print (RPM); // Showing current RPM value
  
  //Enables the motor direction to move in a certain direction.
  digitalWrite(dirPin,LOW);
  //Makes 200 Pulses for making one full cycle (1.8 degree stepper).
  for(int x = 0; x < 200; x++){
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds=((((RPM*1000000)/60)/200)/2);
    //delayMicroseconds(500);
    digitalWrite(stepPin,LOW); 
    delayMicroseconds=((((RPM*1000000)/60)/200)/2);
    //delayMicroseconds(500);

Driez:
Cheers guys thanks a lot! I am almost there. Keep in mind it’s my first time programming anything so I really do appreciate all the help! I’ve been looking for this for more than 2 hours on google and forums.

Right now I have a setable RPM value with my potentiometer. I can continue to use this RPM to calculate the required corresponding delayMicroseconds. However I suspect there might be an easier way to do this?

Furthermore do I need to use an acceleration code or are the RPM’s low enough that this isn’t necessary (max 100RPM)?

This is my current code to drive the 17HS24-2104S nema 17 motor using the TB6600 stepper driver at 2A and 200 pulses per revolution.

I take the potentiometers input RPM and divide by 60 to convert it to RPS. Then I multiply this number by 10^6 (1000000) to convert seconds to microseconds. I divide by 200 to get the delay in microseconds per pulse and divide this by 2 to get the delay in microseconds per phase.

void loop(){

Pot = analogRead(A1);
  //Serial.print(Pot);
  RPM =(Pot100L)/1023;
  Serial.print(RPM);
  lcd.setCursor(0,0); //Defining position to write from first row, first column.
  lcd.print (“RPM”); // Showing the letters RPM
  lcd.setCursor(0,1); //Defining position to write from second row, first column.
  lcd.print (RPM); // Showing current RPM value
 
  //Enables the motor direction to move in a certain direction.
  digitalWrite(dirPin,LOW);
  //Makes 200 Pulses for making one full cycle (1.8 degree stepper).
  for(int x = 0; x < 200; x++){
    digitalWrite(stepPin,HIGH);
    delayMicroseconds=((((RPM
1000000)/60)/200)/2);
    //delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds=((((RPM*1000000)/60)/200)/2);
    //delayMicroseconds(500);

If you are new to ‘C’ then check this out: https://www.tinkercad.com/

It is free to use.

You can do 3D design but also circuits.

The circuit part of it is an excellent electronics simulator complete with and Arduino Uno, servos, everything.

It is just like using real electronic components.

To program the virtual Arduino you can either use the Scratch like interface or code it with C as you do with Arduino IDE.

But the brilliant thing about it is that you can view both Scratch blocks and C code at the same time.

Put the blocks together and view the C code it generates.

A rather convenient way to quickly learn C code if you are not entirely familiar with it.

Arduino is programmed in C++, not in C. They are different languages.