Go Down

Topic: Stepper motor not rotating (Read 158 times) previous topic - next topic

kumbhrageri

Jun 06, 2017, 10:42 am Last Edit: Jun 06, 2017, 10:48 am by kumbhrageri
Code: [Select]

#include <Wire.h>
#include <AccelStepper.h>
int motorDirPin = 9;
int motorStepPin = 10;
AccelStepper stepper(1, motorStepPin, motorDirPin);
int pos=0;
int LUXAddress = 0x4A; // Device address in which is also included the 8th bit for selecting the mode, read in this case.

#define LUX_High_Byte 0x03 //data register
#define LUX_Lower_Byte 0x04

void setup() {
  Wire.begin(); // Initiate the Wire library
  Serial.begin(9600);
  delay(100);
  stepper.setMaxSpeed(3000);
 stepper.setAcceleration(1000);
}

void loop() {
  int X0 = registerVal(LUX_High_Byte);
  int X1 = registerVal(LUX_Lower_Byte);
  int exponent=(X0&0xf0)>>4;
  int mant=(X0&0x0f)<<4|X1;
 float val = (float)(pow(2,exponent)*mant)*0.045;
  Serial.print(" LUX: ");
  Serial.println(val);
 if(val>100){
  pos-=3600;
  stepper.moveTo(pos);
  }
  else
    {
      pos+=3600;
    stepper.moveTo(pos);
    }
stepper.run();
delay(4000);
 }
int registerVal(int address)
{
Wire.beginTransmission(LUXAddress); // Begin transmission to the Sensor
  //Ask the particular registers for data
  Wire.write(address);
  Wire.endTransmission();
   Wire.requestFrom(LUXAddress,1);
  return Wire.read();
}




I wrote two different codes for stepper motor and LUX. When I combined them both, the motor isn't rotating. Why isn't the motor rotating for this code?

Robin2

This is all wrong
Code: [Select]
stepper.run();
delay(4000);


For stepper.run() to work properly there should be no delay() and loop() should be able to repeat hundreds or thousands of times per second.

If some other part of the code needs to happen only once every 4 seconds then use  millis() to manage timing without blocking as illustrated in Several things at a time

I am assuming you have had no problem getting your motor to run with a simple program?

...R
Stepper Motor Basics
Simple Stepper Code
Two or three hours spent thinking and reading documentation solves most programming problems.

kumbhrageri

This code is working


Code: [Select]

#include <Wire.h>

int LUXAddress = 0x4A; // Device address in which is also included the 8th bit for selecting the mode, read in this case.

#define LUX_High_Byte 0x03 //data register
#define LUX_Lower_Byte 0x04
const int dir=9;
const int pul=10;
int i,input;
void setup() {               
  pinMode(dir, OUTPUT);     //direction pin
  pinMode(pul, OUTPUT); //step pin
  digitalWrite(pul, LOW);
 Wire.begin();
  Serial.begin(9600);
  Serial.println("Starting");
}

void loop() {
  input=Serial.read();
  int X0 = registerVal(LUX_High_Byte);
  int X1 = registerVal(LUX_Lower_Byte);
  int exponent=(X0&0xf0)>>4;
  int mant=(X0&0x0f)<<4|X1;
 float val = (float)(pow(2,exponent)*mant)*0.045;
  Serial.print(" LUX: ");
  Serial.println(val);
  if(val>150)
  {
  digitalWrite(dir, HIGH);
  Serial.write(input);
  Serial.write(" Anti Clockwise\n");
  for(i=0;i<800;i++){
  digitalWrite(pul, HIGH);
  delayMicroseconds(1000);
  digitalWrite(pul,LOW);
  delayMicroseconds(1000);
  }
  }
  else if(input<150)
  {
  digitalWrite(dir, LOW);
  Serial.write(input);
  Serial.write(" Clockwise\n");
 for(i=0;i<800;i++){ 
  digitalWrite(pul, HIGH);
  delayMicroseconds(1000);
  digitalWrite(pul,LOW);
  delayMicroseconds(1000);
  }
  }
}
int registerVal(int address)
{
Wire.beginTransmission(LUXAddress); // Begin transmission to the Sensor
  //Ask the particular registers for data
  Wire.write(address);
  Wire.endTransmission();
   Wire.requestFrom(LUXAddress,1);
  return Wire.read();
}


Robin2

This code is working
Glad to hear you have found a solution.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up