1

I'm using a stepper motor with an external limit switch (so when power is on the motor will know where to start from) with an L6470 motor drive. What I want to do here is

  1. when power is ON → turn Clock Wise (CW) until it hits external Limit Switch
  2. Once the limit switch is ON → Turn Anti-Clock Wise (CCW) for 550 mil seconds
  3. Rest for 100 mil seconds
  4. Turn Clock Wise (CW) for 200 mil seconds
  5. Rest for 100 mil seconds
  6. Turn Anti-Clock Wise (CCW) for 200 mil seconds
  7. ....loop from 3 to 6

My current code is using "counter". This is because I can't use "delay" in this code since it interrupts the limit switch activation code.

I've only written easy codes and don't know how to make it rest without using "delay". Please help....

/***********************

回転

************************/
#include <Arduino.h>
#include <SPI.h>

/*************


long max_speed = 7000;// 70000 固定

long int dd = 200;// 微増・微減の量 400
int dt = 10; // 微増・微減にかける時間 ミリ秒

int t_interval = 10;// (休止する時間 みり秒)
int t_hold = 170; // 最高速を維持する時間 ミリ秒)

int steps; 



#define PIN_SPI_MOSI 11
#define PIN_SPI_MISO 12
#define PIN_SPI_SCK 13
#define PIN_SPI_SS 10

#define PIN_L6470_SS PIN_SPI_SS

void L6470_write(unsigned char data);
void L6470_init(void);
void L6470_run(long speed);


long speed = 1000; 
long int n_dir = 0 ;
long offTime;


int c_step = max_speed / dd;

const int limit_sw = 7;
bool switchState = false;
int counter = 0;
int milsec = 0;
bool speedSwitch = false;

void setup()
{
 pinMode(PIN_SPI_MOSI, OUTPUT);
 pinMode(PIN_SPI_MISO, INPUT);
 pinMode(PIN_SPI_SCK, OUTPUT);
 pinMode(PIN_L6470_SS, OUTPUT);


 pinMode(limit_sw, INPUT_PULLUP );
 
 digitalWrite(PIN_L6470_SS, HIGH);

 SPI.begin();
 
 Serial.begin(9600);
 

 L6470_init();
 
 Serial.println("Start...");



 
void loop()
{
 
int value = digitalRead(limit_sw);



if(value == 0){
  counter ++;
  if(counter > 50){//もともと3
    switchState = true;
    counter = 50;
  }
}
if (switchState == true){
  milsec++;
  Serial.println(milsec);
  if(milsec > 550){
    speedSwitch = !speedSwitch;
    milsec = 0;
  }

  int speed;
  if(speedSwitch == true){
    //Serial.println("speedSwitch : true");
    speed = 200;

  }

  else{
    //Serial.println("speedSwitch : false");
    speed = -200;
  }
 
 


}
    
 

else{

  
   L6470_run( 100 );//速度

}
}


void L6470_write(unsigned char data)
{
 // ~SSイネーブル。
 digitalWrite(PIN_L6470_SS, LOW);
 // SPI転送。
 SPI.transfer(data);
 // ~SSディスエーブル。
 digitalWrite(PIN_L6470_SS, HIGH);
}

void L6470_init()
{
 // MAX_SPEED設定。
 /// レジスタアドレス。
 L6470_write(0x07);
 // 最大回転スピード値(10bit) 0020

 L6470_write(0x00);
 L6470_write(0x90);
 
 // KVAL_HOLD設定。
 /// レジスタアドレス。
 L6470_write(0x09);
 // モータ停止中の電圧設定(8bit)
 L6470_write(0xFF);
 
 // KVAL_RUN設定。
 /// レジスタアドレス。
 L6470_write(0x0A);
 // モータ定速回転中の電圧設定(8bit)
 //L6470_write(0xFF);
 L6470_write(0x64);
 
 // KVAL_ACC設定。
 /// レジスタアドレス。
 L6470_write(0x0B);
 // モータ加速中の電圧設定(8bit)
 L6470_write(0xFF);
 
 // KVAL_DEC設定。
 /// レジスタアドレス。
 L6470_write(0x0C);
 // モータ減速中の電圧設定(8bit)
 L6470_write(0xFF);
 
 // OCD_TH設定。
 /// レジスタアドレス。
 L6470_write(0x13);
 // オーバーカレントスレッショルド設定(4bit)
 L6470_write(0x0F);
 
 // STALL_TH設定。
 /// レジスタアドレス。
 L6470_write(0x14);
 // ストール電流スレッショルド設定(4bit)
 L6470_write(0x7F);
}

void L6470_run(long speed)
{
 unsigned short dir;
 unsigned long spd;
 unsigned char spd_h;
 unsigned char spd_m;
 unsigned char spd_l;

//spd = speed;

 // 方向検出。
 
 if (speed < 0)
 {
  dir = 0x50;
  spd = -1 * speed;
 }
 else
 {
  dir = 0x51;
  spd = speed;
 }


 // 送信バイトデータ生成。
 spd_h = (unsigned char)((0x0F0000 & spd) >> 16);
 spd_m = (unsigned char)((0x00FF00 & spd) >> 8);
 spd_l = (unsigned char)(0x00FF & spd);
 
 // コマンド(レジスタアドレス)送信。
 L6470_write(dir);
 // データ送信。
 L6470_write(spd_h);
 L6470_write(spd_m);
 L6470_write(spd_l);
 
  Serial.print( " current spd = ");
  Serial.println( spd );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.