Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code not working
10-03-2016, 07:03 PM (This post was last modified: 10-03-2016 07:12 PM by frabraha.)
Post: #1
Code not working
I've build the startracker using arduino UNO, A4988 and NEMA 17 motor.

I've connected everything, and added the code (a little modified). I can hear that the motor has power and is on. But when I press start, nothing happens.

What is wrong?

Code:
#define  BACK_PIN  13
#define  STOP_PIN  12
#define  START_PIN 11
#define  ENABLE_PIN 6
#define  DIR_PIN 5
#define  LED_ON_PIN 10
#define  LED_OFF_PIN 9
#define  LED_BACK_PIN 8


#define COMINGUPSPEED 1.25
#define LENGTH 228
// Calculus here:
#define STEP ((2*3.14159)/1436)*LENGTH //rotational velocity of the small gear

#define RPS (STEP/(60*0.2549))/COMINGUPSPEED //rotational velocity of the large gear

#define ZERO_SPEED 65535
#define STEPS_PER_REV 3200     // 200 steps motor with 1/16 microstepping
#define MAX_RPM (RPS*60.0)  

// BIT functions
#define CLR(x,y) (x&=(~(1<<y)))
#define SET(x,y) (x|=(1<<y))

//uint16_t rpm;
float rpm;
uint16_t period;
uint16_t userCommand=0;
uint8_t motor_enable;

// TIMER 1: STEP INTERRUPT
ISR(TIMER1_COMPA_vect)
{
  if (motor_enable)
    {
    SET(PORTB,4);
    delayMicroseconds(2);
    CLR(PORTB,4);
    }
}

void setRpm()
{
  float temp;
  if (rpm == 0)
  {
    ICR1 = ZERO_SPEED;
    digitalWrite(ENABLE_PIN,HIGH);  // Disable motor
  }
  else
  {
    digitalWrite(ENABLE_PIN,LOW);  // Enable motor
/*   if (rpm<8)
      rpm = 8;*/
    if (rpm>MAX_RPM)
      rpm = MAX_RPM;
    temp = (rpm/60.0)*STEPS_PER_REV;
    temp = 2000000 / temp;          //  2000000 = (16000000/8) timer1 16Mhz with 1/8 preescaler
    if (period<600000)
      period=60000;
    period = temp;
    while (TCNT1 < 30);   // Wait until a pulse to motor has finished
    //cli();
    ICR1 = period; //+ userCommand;
    if (TCNT1 > ICR1)     // Handle when we need to reset the timer
      TCNT1=0;
    //sei();
  }
}

int buttonSTARTState = 0;
int buttonSTOPState = 0;
int buttonBACKState = 0;
int ENABLEState = 1;
int DIRState = 0;
int LED_ON_State = 0;
int LED_OFF_State = 0;
int LED_BACK_State = 0;

void setup() {

  // Set up the three button inputs, with pullups
  pinMode(BACK_PIN, INPUT_PULLUP);
  pinMode(STOP_PIN, INPUT_PULLUP);
  pinMode(START_PIN, INPUT_PULLUP);
  pinMode(ENABLE_PIN, OUTPUT);
  pinMode(DIR_PIN, OUTPUT);
  pinMode(LED_ON_PIN, OUTPUT);
  pinMode(LED_OFF_PIN, OUTPUT);
  pinMode(LED_BACK_PIN, OUTPUT);

  Serial.begin(115200);
  digitalWrite(LED_OFF_PIN, HIGH);
  motor_enable = 0;
    // PWM SETUP
  // Fast PWM mode => TOP:ICR1
  TCCR1A =(1<<WGM11);          
//  TCCR1B = (1<<WGM13)|(1<<WGM12)|(1<<CS10);   //No Prescaler, Fast PWM
   TCCR1B = (1<<WGM13)|(1<<WGM12)|(1<<CS11);   // Prescaler 1:8, Fast PWM
  ICR1 = ZERO_SPEED;
  TIMSK1 = (1<<OCIE1A);  // Enable Timer interrupt
  
  rpm = 0;
  
  while (digitalRead(START_PIN)==HIGH);    // Wait until START button is pressed
  motor_enable = 1;
  digitalWrite(LED_OFF_PIN, LOW);
  digitalWrite(LED_ON_PIN, HIGH);
  delay(250);
  while (digitalRead(START_PIN)==LOW);
}

void loop() {

  buttonSTARTState = digitalRead(START_PIN);
  buttonSTOPState = digitalRead(STOP_PIN);
  buttonBACKState = digitalRead(BACK_PIN);
  ENABLEState = digitalRead(ENABLE_PIN);
  DIRState = digitalRead(DIR_PIN);
  LED_ON_State = digitalRead(LED_ON_PIN);
  LED_OFF_State = digitalRead(LED_OFF_PIN);
  LED_BACK_State = digitalRead(LED_BACK_PIN);

if (digitalRead(START_PIN)==LOW)   // START/STOP Button pressed?
    {
    rpm = 0;
    userCommand=0;
    setRpm();
    if (motor_enable == 1)
      motor_enable = 0;
    else
      motor_enable = 1;
    while (digitalRead(START_PIN)==LOW);   // Wait until botton release
    }
    if (digitalRead(BACK_PIN)==LOW)   //  Button BACK pressed?
    {
         digitalWrite(DIR_PIN,HIGH);    // Motor direction

          rpm=50;
        setRpm();
  
        if (motor_enable == 1)
          motor_enable = 0;
        else
          motor_enable = 1;
        
    while (digitalRead(BACK_PIN)==LOW);   // Wait until botton release
    }
     if (motor_enable)
      {
        rpm++;
        digitalWrite(LED_ON_PIN,HIGH);
      }
      else
      {
        rpm = 0;
        digitalWrite(LED_ON_PIN,LOW);
      }
  
if (digitalRead(STOP_PIN)==LOW)   // Decrease button
    {
    digitalWrite(LED_ON_PIN,LOW);
    userCommand--;
    while (digitalRead(STOP_PIN)==LOW);  // Wait until released
    }
  if (digitalRead(BACK_PIN)==LOW)   // Increase button
    {
    digitalWrite(LED_ON_PIN,LOW);
    userCommand++;
    while (digitalRead(BACK_PIN)==LOW);  // Wait until released
    }
}
Find all posts by this user
Quote this message in a reply
10-07-2016, 08:10 AM
Post: #2
RE: Code not working
I've updated the code. Still can't figure out what's wrong.

I checked and rechecked everything. Found out that the leds was connected wrong. Now they all work. But still no go on the motor. I can hear that the motor humz when power is connected and makes a clunk noise when I press the buttons. But it still wont move.

I tried to change the pins for the motor, but same result. It humz when power is connected and makes clunky noises when I the buttons.

Anyone have any suggestions on how to fix it?

Code:
#define  BACK_PIN  13
#define  STOP_PIN  12
#define  START_PIN 11
#define  ENABLE_PIN 6
#define  DIR_PIN 5
#define  LED_ON_PIN 10
#define  LED_OFF_PIN 9
#define  LED_BACK_PIN 8


#define COMINGUPSPEED 1.25
#define LENGTH 228
// Calculus here:
#define STEP ((2*3.14159)/1436)*LENGTH //rotational velocity of the small gear

#define RPS (STEP/(60*0.2549))/COMINGUPSPEED //rotational velocity of the large gear

#define ZERO_SPEED 65535
#define STEPS_PER_REV 3200     // 200 steps motor with 1/16 microstepping
#define MAX_RPM (RPS*60.0)  

// BIT functions
#define CLR(x,y) (x&=(~(1<<y)))
#define SET(x,y) (x|=(1<<y))

//uint16_t rpm;
float rpm;
uint16_t period;
uint16_t userCommand=0;
uint8_t motor_enable;

// TIMER 1: STEP INTERRUPT
ISR(TIMER1_COMPA_vect)
{
  if (motor_enable)
    {
    SET(PORTB,4);
    delayMicroseconds(2);
    CLR(PORTB,4);
    }
}

void setRpm()
{
  float temp;
  if (rpm == 0)
  {
    ICR1 = ZERO_SPEED;
    digitalWrite(ENABLE_PIN,HIGH);  // Disable motor
  }
  else
  {
    digitalWrite(ENABLE_PIN,LOW);  // Enable motor
/*   if (rpm<8)
      rpm = 8;*/
    if (rpm>MAX_RPM)
      rpm = MAX_RPM;
    temp = (rpm/60.0)*STEPS_PER_REV;
    temp = 2000000 / temp;          //  2000000 = (16000000/8) timer1 16Mhz with 1/8 preescaler
    if (period<600000)
      period=60000;
    period = temp;
    while (TCNT1 < 30);   // Wait until a pulse to motor has finished
    //cli();
    ICR1 = period; //+ userCommand;
    if (TCNT1 > ICR1)     // Handle when we need to reset the timer
      TCNT1=0;
    //sei();
  }
}

int buttonSTARTState = 0;
int buttonSTOPState = 0;
int buttonBACKState = 0;
int ENABLEState = 1;
int DIRState = 0;
int LED_ON_State = 0;
int LED_OFF_State = 0;
int LED_BACK_State = 0;

void setup() {

  // Set up the three button inputs, with pullups
  pinMode(BACK_PIN, INPUT_PULLUP);
  pinMode(STOP_PIN, INPUT_PULLUP);
  pinMode(START_PIN, INPUT_PULLUP);
  pinMode(ENABLE_PIN, OUTPUT);
  pinMode(DIR_PIN, OUTPUT);
  pinMode(LED_ON_PIN, OUTPUT);
  pinMode(LED_OFF_PIN, OUTPUT);
  pinMode(LED_BACK_PIN, OUTPUT);

  Serial.begin(115200);
  digitalWrite(LED_OFF_PIN, HIGH);
  motor_enable = 0;
    // PWM SETUP
  // Fast PWM mode => TOP:ICR1
  TCCR1A =(1<<WGM11);          
//  TCCR1B = (1<<WGM13)|(1<<WGM12)|(1<<CS10);   //No Prescaler, Fast PWM
   TCCR1B = (1<<WGM13)|(1<<WGM12)|(1<<CS11);   // Prescaler 1:8, Fast PWM
  ICR1 = ZERO_SPEED;
  TIMSK1 = (1<<OCIE1A);  // Enable Timer interrupt
  
  rpm = 0;
  
  while (digitalRead(START_PIN)==HIGH);    // Wait until START button is pressed
  motor_enable = 1;
  digitalWrite(LED_OFF_PIN, LOW);
  digitalWrite(LED_ON_PIN, HIGH);
  delay(250);
  while (digitalRead(START_PIN)==LOW);
}

void loop() {

if (digitalRead(START_PIN)==LOW)   // START/STOP Button pressed?
    {
    rpm = 0;
    userCommand=0;
    setRpm();
    if (motor_enable == 1)
      motor_enable = 0;
    else
      motor_enable = 1;
    while (digitalRead(START_PIN)==LOW);   // Wait until botton release
    }
    if (digitalRead(BACK_PIN)==LOW)   //  Button BACK pressed?
    {
         digitalWrite(DIR_PIN,HIGH);    // Motor direction

          rpm=50;
        setRpm();
  
        if (motor_enable == 1)
          motor_enable = 0;
        else
          motor_enable = 1;
        
    while (digitalRead(BACK_PIN)==LOW);   // Wait until botton release
    }
     if (motor_enable)
      {
        rpm++;
        digitalWrite(LED_ON_PIN,HIGH);
      }
      else
      {
        rpm = 0;
        digitalWrite(LED_ON_PIN,LOW);
      }
  
if (digitalRead(STOP_PIN)==LOW)   // Decrease button
    {
    digitalWrite(LED_ON_PIN,LOW);
    userCommand--;
    while (digitalRead(STOP_PIN)==LOW);  // Wait until released
    }
  if (digitalRead(BACK_PIN)==LOW)   // Increase button
    {
    digitalWrite(LED_ON_PIN,LOW);
    userCommand++;
    while (digitalRead(BACK_PIN)==LOW);  // Wait until released
    }
}


Attached File(s) Thumbnail(s)
   
Find all posts by this user
Quote this message in a reply
10-12-2016, 06:15 PM
Post: #3
RE: Code not working
(10-07-2016 08:10 AM)frabraha Wrote:  I've updated the code. Still can't figure out what's wrong.

I checked and rechecked everything. Found out that the leds was connected wrong. Now they all work. But still no go on the motor. I can hear that the motor humz when power is connected and makes a clunk noise when I press the buttons. But it still wont move.

I tried to change the pins for the motor, but same result. It humz when power is connected and makes clunky noises when I the buttons.

Anyone have any suggestions on how to fix it?

Code:
#define  BACK_PIN  13
#define  STOP_PIN  12
#define  START_PIN 11
#define  ENABLE_PIN 6
#define  DIR_PIN 5
#define  LED_ON_PIN 10
#define  LED_OFF_PIN 9
#define  LED_BACK_PIN 8


#define COMINGUPSPEED 1.25
#define LENGTH 228
// Calculus here:
#define STEP ((2*3.14159)/1436)*LENGTH //rotational velocity of the small gear

#define RPS (STEP/(60*0.2549))/COMINGUPSPEED //rotational velocity of the large gear

#define ZERO_SPEED 65535
#define STEPS_PER_REV 3200     // 200 steps motor with 1/16 microstepping
#define MAX_RPM (RPS*60.0)  

// BIT functions
#define CLR(x,y) (x&=(~(1<<y)))
#define SET(x,y) (x|=(1<<y))

//uint16_t rpm;
float rpm;
uint16_t period;
uint16_t userCommand=0;
uint8_t motor_enable;

// TIMER 1: STEP INTERRUPT
ISR(TIMER1_COMPA_vect)
{
  if (motor_enable)
    {
    SET(PORTB,4);
    delayMicroseconds(2);
    CLR(PORTB,4);
    }
}

void setRpm()
{
  float temp;
  if (rpm == 0)
  {
    ICR1 = ZERO_SPEED;
    digitalWrite(ENABLE_PIN,HIGH);  // Disable motor
  }
  else
  {
    digitalWrite(ENABLE_PIN,LOW);  // Enable motor
/*   if (rpm<8)
      rpm = 8;*/
    if (rpm>MAX_RPM)
      rpm = MAX_RPM;
    temp = (rpm/60.0)*STEPS_PER_REV;
    temp = 2000000 / temp;          //  2000000 = (16000000/8) timer1 16Mhz with 1/8 preescaler
    if (period<600000)
      period=60000;
    period = temp;
    while (TCNT1 < 30);   // Wait until a pulse to motor has finished
    //cli();
    ICR1 = period; //+ userCommand;
    if (TCNT1 > ICR1)     // Handle when we need to reset the timer
      TCNT1=0;
    //sei();
  }
}

int buttonSTARTState = 0;
int buttonSTOPState = 0;
int buttonBACKState = 0;
int ENABLEState = 1;
int DIRState = 0;
int LED_ON_State = 0;
int LED_OFF_State = 0;
int LED_BACK_State = 0;

void setup() {

  // Set up the three button inputs, with pullups
  pinMode(BACK_PIN, INPUT_PULLUP);
  pinMode(STOP_PIN, INPUT_PULLUP);
  pinMode(START_PIN, INPUT_PULLUP);
  pinMode(ENABLE_PIN, OUTPUT);
  pinMode(DIR_PIN, OUTPUT);
  pinMode(LED_ON_PIN, OUTPUT);
  pinMode(LED_OFF_PIN, OUTPUT);
  pinMode(LED_BACK_PIN, OUTPUT);

  Serial.begin(115200);
  digitalWrite(LED_OFF_PIN, HIGH);
  motor_enable = 0;
    // PWM SETUP
  // Fast PWM mode => TOP:ICR1
  TCCR1A =(1<<WGM11);          
//  TCCR1B = (1<<WGM13)|(1<<WGM12)|(1<<CS10);   //No Prescaler, Fast PWM
   TCCR1B = (1<<WGM13)|(1<<WGM12)|(1<<CS11);   // Prescaler 1:8, Fast PWM
  ICR1 = ZERO_SPEED;
  TIMSK1 = (1<<OCIE1A);  // Enable Timer interrupt
  
  rpm = 0;
  
  while (digitalRead(START_PIN)==HIGH);    // Wait until START button is pressed
  motor_enable = 1;
  digitalWrite(LED_OFF_PIN, LOW);
  digitalWrite(LED_ON_PIN, HIGH);
  delay(250);
  while (digitalRead(START_PIN)==LOW);
}

void loop() {

if (digitalRead(START_PIN)==LOW)   // START/STOP Button pressed?
    {
    rpm = 0;
    userCommand=0;
    setRpm();
    if (motor_enable == 1)
      motor_enable = 0;
    else
      motor_enable = 1;
    while (digitalRead(START_PIN)==LOW);   // Wait until botton release
    }
    if (digitalRead(BACK_PIN)==LOW)   //  Button BACK pressed?
    {
         digitalWrite(DIR_PIN,HIGH);    // Motor direction

          rpm=50;
        setRpm();
  
        if (motor_enable == 1)
          motor_enable = 0;
        else
          motor_enable = 1;
        
    while (digitalRead(BACK_PIN)==LOW);   // Wait until botton release
    }
     if (motor_enable)
      {
        rpm++;
        digitalWrite(LED_ON_PIN,HIGH);
      }
      else
      {
        rpm = 0;
        digitalWrite(LED_ON_PIN,LOW);
      }
  
if (digitalRead(STOP_PIN)==LOW)   // Decrease button
    {
    digitalWrite(LED_ON_PIN,LOW);
    userCommand--;
    while (digitalRead(STOP_PIN)==LOW);  // Wait until released
    }
  if (digitalRead(BACK_PIN)==LOW)   // Increase button
    {
    digitalWrite(LED_ON_PIN,LOW);
    userCommand++;
    while (digitalRead(BACK_PIN)==LOW);  // Wait until released
    }
}

Hmm... I am not sure the Arduino UNO can handle the original code (It only has one internal TIMER, the Arduino Leonardo has 2)...
Find all posts by this user
Quote this message in a reply
04-08-2017, 06:53 PM
Post: #4
RE: Code not working
Did you ever get yours working. I have exactly the same issue but am also using an UNO. I have a Leonardo turning up tomorrow so will see if that works. Have put some simple stepper motor code on it and that works fine so everything appears to be wired up correctly.
Find all posts by this user
Quote this message in a reply
04-13-2017, 10:13 AM
Post: #5
RE: Code not working
(04-08-2017 06:53 PM)Croccy22 Wrote:  Did you ever get yours working. I have exactly the same issue but am also using an UNO. I have a Leonardo turning up tomorrow so will see if that works. Have put some simple stepper motor code on it and that works fine so everything appears to be wired up correctly.

Just in case anyone comes across this issue. The Leonardo did indeed solve this issue. So the UNO will not work for this project.
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)