Show Posts
Pages: [1] 2 3 4
1  Using Arduino / Programming Questions / Restart Serial print on: March 05, 2014, 09:30:16 am
hello,
I am struggling to finish the code.

What I am trying to do is :

I am counting two  hall sensor's pulses. and printing as serial print.
and if sensor 1's pulse and sensor'2 pulse are different and the difference are bigger then 5, the serial print should start from 0. and start to count their pulses from 0 again.

The code below does count 2 sensor's pulses.
and when the differences bigger then 5 , it makes rpmcount and rpmcount_1's value  to 0.
but even though still those sensors are counting the their pulses, the value are not changing. it stays 0.

is their any way to count the sensor's pulse from 0 every time their difference is bigger then 5?

Code:
const int hallPin=A3 ;
const int hallPin_1=A4;     

const int ledPin=13;

int hallState ;
int hallState_1;

int lasthallState;
int lasthallState_1;

int rpmcount =0;
int rpmcount_1 =0;
       
int motorSt=2;
int motorDr=3;
int motorEn=4;

int motorSt_1=5;
int motorDr_1=6;
int motorEn_1=7;


void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(hallPin, INPUT);
  pinMode(hallPin_1, INPUT);
  Serial.begin(9600);
 
  pinMode(motorEn, OUTPUT);
  pinMode(motorDr, OUTPUT);
  pinMode(motorSt, OUTPUT);
 
  pinMode(motorEn_1, OUTPUT);
  pinMode(motorDr_1, OUTPUT);
  pinMode(motorSt_1, OUTPUT);
 
  lasthallState = digitalRead(hallPin);
  lasthallState_1 = digitalRead(hallPin_1);
}

void loop()
 {
 
    digitalWrite(motorSt, HIGH);
    delayMicroseconds(500);
    digitalWrite(motorSt, LOW);
    delayMicroseconds(500);
   
     
    digitalWrite(motorSt_1, HIGH);
    delayMicroseconds(500);
    digitalWrite(motorSt_1, LOW);
    delayMicroseconds(500);
   
  hallState = digitalRead(hallPin);
     if (hallState!=lasthallState )
     {
        if (hallState==LOW)
       {
        rpmcount++;
        Serial.print("Motor 1's pulse");
        Serial.println(rpmcount);
        }
     }
     
//-----------------------------------------------------//
 
  hallState_1 = digitalRead(hallPin_1);
     if (hallState_1 !=lasthallState_1 )
     {
        if (hallState_1==LOW)
       {
        rpmcount_1++;
        Serial.print("Motor 2's pulse");
        Serial.println(rpmcount_1);
        }
     }

   
if (abs(rpmcount-rpmcount_1) > 5)
{
rpmcount=0;
rpmcount_1=0;
}


lasthallState=hallState;
lasthallState_1=hallState_1;

   



 }
   

2  Using Arduino / Programming Questions / Counting pulses using hall effect sensor on: March 03, 2014, 07:16:46 am
Hello,

We are trying to count the number of pulses registered by a hall sensor. When the magnet passes the sensor, it's output is low and then returns high when the magnetic field is removed

Every time this happens we want to that to be counted as one pulse. However, when using serial print, the code we have at the moment the counter increases many times for one pass.

Code:
const int hallPin=8;
const int ledPin=13;
int hallState ;
int lasthallState;
int rpmcount =0;
int motorSt=2;
int motorDr=3;
int motorEn=4;
int i=0;
boolean isCheck=false;

void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(hallPin, INPUT);
 Serial.begin(9600);
 
   pinMode(motorEn, OUTPUT);
  pinMode(motorDr, OUTPUT);
  pinMode(motorSt, OUTPUT);
   
}

void loop()
 {
   if(i<2500)
  {
    digitalWrite(motorSt, HIGH);
    delayMicroseconds(1000);
    digitalWrite(motorSt, LOW);
    delayMicroseconds(1000);
    i++;

  }
    hallState = digitalRead(hallPin);
 
     if (hallState!=lasthallState  ){
     digitalWrite(ledPin, HIGH);
     rpmcount++;
     Serial.println(rpmcount);
           lasthallState = digitalRead(hallPin);


}

else{digitalWrite(ledPin, LOW);}
hallState=lasthallState;

 }
   


Can anyone tell us why it doesn't count one for every pass of the magnet?

Many thanks

note: this is for use with a stepper motor and counting the number of revolutions
3  Using Arduino / Sensors / 2-axis hall effect sensor for multiple turning stepper motor's position sensing on: February 25, 2014, 05:33:41 am
Hello,  
I am not really sure if using 2-axis hall effect sensor for multiple turning stepper motor's position sensing is a right idea.
basically I have a stepper motor that has a lead screw and a nut coming up and down when the stepper motor turing.
and I would like to the nut always start to move from same position.
and now I am using a photo cell sensor to tell the motor to turn until the nut comes to start position when power on.
but I need something to constantly check if the motor is stalling. So I was going to add a hall effect sensor beside of the photocell sensor. but I found a 2-axis hall effect sensor. I've never used the 2-axis hall effect sensor. and I am wondering if this 2-axis hall effect sensor can do both instead of the photocell and the hall effect sensor for my need?
4  Using Arduino / Programming Questions / when the stepper motor stalls.. on: February 20, 2014, 12:08:00 pm
In the event of the stepper motor stalling, I want to the motor to automatically stop without using additional sensors.
is there a code that exists for this purpose?
or any ideas?
5  Using Arduino / Programming Questions / Re: Two different functions push buttons on: February 19, 2014, 05:32:41 am
The code above doesn't work for any buttons...
LED just stays on.
 
6  Using Arduino / Programming Questions / Re: Two different functions push buttons on: February 18, 2014, 12:44:48 pm
Sorry I must have massed up with my other code.
this was the final code I tried.
still not working
all the inputs have been defined.  but still same.

Code:
int ledPin = 13;                // LED is connected to pin 12
int switchPin1 = 2; 
int switchPin2 = 3;
int switchPin3 = 4;

int val;                       
int val2;

int check1;                       
int dbcheck1;


int button3State ;  //button three state

int lightMode = 0; 


void setup() {
  pinMode(ledPin, OUTPUT);      // Set the LED pin as output
  pinMode(switchPin1, INPUT); 
  pinMode(switchPin2, INPUT);
  pinMode(switchPin3, INPUT);  // Set the switch pin as input
 
 
  digitalWrite(ledPin, LOW);
  button3State= digitalRead(switchPin3);
}


void loop(){
val = digitalRead(switchPin1);
val2 = digitalRead(switchPin2);

check1= digitalRead(switchPin3);
delay(5);
dbcheck1= digitalRead(switchPin3);


// read input value and store it in val
  if (val == LOW || val2 ==LOW ) {               // check if the button is pressed
    digitalWrite(ledPin, LOW);   // turn LED on
  }
  if (val == HIGH || val2 ==HIGH) {              // check if the button is not pressed
    digitalWrite(ledPin, HIGH);    // turn LED off
  }
 

 // read the input again to check for bounces
  if (check1 == dbcheck1) {                 // make sure we got 2 consistant readings!
    if (check1 != button3State) {          // the button state has changed!
      if (check1 == LOW) {                // check if the button is pressed
        if (lightMode == 0) {          // is the light off?
          lightMode = 1;               // turn light on!
          digitalWrite(ledPin, HIGH);
     
        } else {
          lightMode = 0;               // turn light off!
          digitalWrite(ledPin, LOW);
     
        }
      }
    }
    button3State = val;                 // save the new state in our variable
  }
 
}
7  Using Arduino / Programming Questions / Re: Two different functions push buttons on: February 18, 2014, 12:36:34 pm
It only works for button 2 and 3.
and button 4 doesn't work at all.
8  Using Arduino / Programming Questions / Two different functions push buttons on: February 18, 2014, 12:20:52 pm
Hello,
I am struggling to write this code working.
I imagine this code can be written much simpler and clean way. but I can't really figure out why it is not working.
Basically what I am trying to do is:
if you press the button 2 and 3, a LED is only on while one of the buttons is pressed.
but when you press button 4, LED stays on even though the button is released until you press button again.
and I would like to add denounce function on the button 4.

any advise or tip would be grateful.
thanks


Code:
int ledPin = 13;                // LED is connected to pin 12
int switchPin = 2;  
int switchPin2 = 3;
int switchPin3 = 4;

int val;                        
int val2;

int check1;                        
int dbcheck1;


int button3State ;  //button three state

int lightMode = 0;  


void setup() {
  pinMode(ledPin, OUTPUT);      // Set the LED pin as output
  pinMode(switchPin, INPUT);    // Set the switch pin as input
  digitalWrite(ledPin, LOW);
  button3State= digitalRead(switchPin3);
}


void loop(){
val = digitalRead(switchPin);
val2 = digitalRead(switchPin2);  

check1= digitalRead(switchPin3);
delay(5);
dbcheck1= digitalRead(switchPin3);

// read input value and store it in val
  if (val == LOW || val2 ==LOW ) {               // check if the button is pressed
    digitalWrite(ledPin, LOW);   // turn LED on
  }
  if (val == HIGH || val2 ==HIGH) {              // check if the button is not pressed
    digitalWrite(ledPin, HIGH);    // turn LED off
  }
 check1= digitalRead(switchPin3);
delay(5);
dbcheck1= digitalRead(switchPin3);
 // read the input again to check for bounces
  if (check1 == dbcheck1) {                 // make sure we got 2 consistant readings!
    if (val != button3State) {          // the button state has changed!
      if (val == LOW) {                // check if the button is pressed
        if (lightMode == 0) {          // is the light off?
          lightMode = 1;               // turn light on!
          digitalWrite(ledPin, HIGH);
      
        } else {
          lightMode = 0;               // turn light off!
          digitalWrite(ledPin, LOW);
    
        }
      }
    }
    button3State = val;                 // save the new state in our variable
  }
  
}
9  Using Arduino / Programming Questions / Stepper motors run slower with other components on: November 15, 2013, 05:07:07 am
Hello. I am not an expert at Arduino Code.  So I wonder if any part of my code is making a problem to achieve what i want to.

First of all, in my set up there are 2 motors, 2 motor driver, 2 photo sensors, 4 switches and 1 LED.
and my code is based on very basic code to run stepper motor which is below;
the reason why i am using the basic code was that motor has to handle quite hight torque and many reason the stepper motor and the accelerate Library didn't work for me.

And my problem that i can't really understand is the motor running faster when they run with basic code without any extra code for other functions and other components .

Code:
 
 
void loop()
{
#define dDELAY_SLOW 300  // Max Delay Value
int m_motorDelay= dDELAY_SLOW;   

void setup()
{
  pinMode(7, OUTPUT); Motor Enable Pin
  pinMode(6, OUTPUT); // Motor Dir
  pinMode(5, OUTPUT); // Motor Step
  pinMode(4, OUTPUT);// Motor Enable Pin2
  pinMode(3, OUTPUT); // Motor Dir2
  pinMode(2, OUTPUT); // Motor Step2

  Serial.begin(9600);

  digitalWrite(7, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(3, HIGH);

}

digitalWrite(7, HIGH);
  digitalWrite(5, HIGH);
  delayMicroseconds(m_motorDelay);
  digitalWrite(5, LOW);
  delayMicroseconds(m_motorDelay);

 digitalWrite(7, HIGH);
  digitalWrite(5, HIGH);
  delayMicroseconds(m_motorDelay);
  digitalWrite(5, LOW);
  delayMicroseconds(m_motorDelay);

  digitalWrite(4, HIGH);
  digitalWrite(2, HIGH);
  delayMicroseconds(m_motorDelay);
  digitalWrite(2, LOW);
  delayMicroseconds(m_motorDelay);
  }

But If i added more code to complete all the functions I need the motor running slower.
the code below is my final code
Code:
#define dSTART_POINT 0        //Start Position
#define dEND_POINT 2000       // Target Position
#define dDELAY_FAST 650       // Minimum Delay Value
#define dDELAY_SLOW 300  // Max Delay Value
#define dSWITCH_PIN_1 8     //Button one
#define dSWITCH_PIN_2 9     //Button two
#define dSWITCH_PIN_3 10    //Button three
#define dSWITCH_PIN_4 11    //Button four

enum
{
  eMOTOR_STOP = 0,
  eMOTOR_RUN,
};

enum
{
  eSWITCH_STATE_UP = 0,        // normal button state
  eSWITCH_STATE_DOWN           // When it is pressed
};

int m_motorIndex = 0;                   //motor position
int m_motorDir = HIGH;                  //motor direction
int m_motorState = eMOTOR_STOP;         //motor state

int m_switch1State = eSWITCH_STATE_UP;  //button one state
int m_switch2State = eSWITCH_STATE_UP;  //button two state
int m_switch3State = eSWITCH_STATE_UP;  //button three state
int m_switch4State = eSWITCH_STATE_UP;  //button four state
int m_motorDelay= dDELAY_SLOW;          //motor delay


int photoRPin = 0;                 //Sensor Pins
int photoRPin_2 = 1;

int lightLevel;                    //Sensor Value
int lightLevel_2;

int ledPin = 13;                   //LED
int ledPin_2 = 12;

int ledState=HIGH;                  //LED Value

int workingMode;                   
int workingMode_2;

void setup()
{
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT); // Motor Dir
  pinMode(5, OUTPUT); // Motor Step
  pinMode(4, OUTPUT);
  pinMode(3, OUTPUT); // Motor Dir
  pinMode(2, OUTPUT); // Motor Step

  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);

  pinMode(dSWITCH_PIN_1, INPUT);
  pinMode(dSWITCH_PIN_2, INPUT);
  pinMode(dSWITCH_PIN_3, INPUT);
  pinMode(dSWITCH_PIN_4, INPUT);

  Serial.begin(9600);

  digitalWrite(7, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(3, HIGH);

  lightLevel=analogRead(photoRPin);
  lightLevel_2=analogRead(photoRPin_2);
  m_motorIndex = 0;
}


void moveMotor()
{
  digitalWrite(7, HIGH);
  digitalWrite(5, HIGH);
  delayMicroseconds(m_motorDelay);
  digitalWrite(5, LOW);
  delayMicroseconds(m_motorDelay);

  digitalWrite(4, HIGH);
  digitalWrite(2, HIGH);
  delayMicroseconds(m_motorDelay);
  digitalWrite(2, LOW);
  delayMicroseconds(m_motorDelay);


  if(m_motorDir == HIGH)
  {
    m_motorIndex ++;
  }
  else 
  {
    m_motorIndex --;
  }
}


void moterProc()
{

  if(m_motorState == eMOTOR_STOP){
    digitalWrite(5, LOW);
    digitalWrite(7, LOW);
    digitalWrite(4, LOW);
    digitalWrite(2, LOW);
  }

  else if(m_motorState == eMOTOR_RUN && m_motorIndex <= dEND_POINT && m_motorIndex >= dSTART_POINT)
  {
    moveMotor();
  }


  if( m_motorIndex <= dSTART_POINT || m_motorIndex >= dEND_POINT)
  {
    m_motorState = eMOTOR_STOP;
    digitalWrite(5, LOW);
    digitalWrite(7, LOW);
    digitalWrite(4, LOW);
    digitalWrite(2, LOW);
  }
}






void pushSwitch1or2()
{
  if( m_motorState == eMOTOR_RUN)
  {
    m_motorState = eMOTOR_STOP;
  }

  else if(m_motorState == eMOTOR_STOP && m_motorIndex == dSTART_POINT)
  {
    digitalWrite(6, HIGH);
    digitalWrite(3, HIGH);
    m_motorDir = HIGH;
    m_motorState = eMOTOR_RUN;
    moveMotor();
  }
  else if(m_motorState == eMOTOR_STOP && m_motorIndex == dEND_POINT)
  {
    digitalWrite(6, LOW);
    digitalWrite(3, LOW);
    m_motorDir = LOW;
    m_motorState = eMOTOR_RUN;
    moveMotor();
  }
  else if(m_motorState == eMOTOR_STOP && m_motorIndex >= (dEND_POINT/2))
  {
    digitalWrite(6, HIGH);
    digitalWrite(3, HIGH);
    m_motorDir = HIGH;
    m_motorState = eMOTOR_RUN;
    moveMotor();
  }
  else if(m_motorState == eMOTOR_STOP && m_motorIndex < (dEND_POINT/2))
  {
    digitalWrite(6, LOW);
    digitalWrite(3, LOW);
    m_motorDir = LOW;
    m_motorState = eMOTOR_RUN;
    moveMotor();
  }
}



void pushSwitch3()
{
  if( m_motorState == eMOTOR_RUN)
  {
    m_motorState = eMOTOR_STOP;
  }//If motor is running, it stops

  // if the moter stopps and it is in working area



  else if(m_motorState == eMOTOR_STOP && m_motorIndex >= dSTART_POINT && m_motorIndex < dEND_POINT)
  {
    digitalWrite(6, HIGH);
    digitalWrite(3, HIGH);
    m_motorDir = HIGH;
    m_motorState = eMOTOR_RUN;
    moveMotor();
  }
}





void pushSwitch4()
{
  if( m_motorState == eMOTOR_RUN)
  {
    m_motorState = eMOTOR_STOP;
  }

  else if(m_motorState == eMOTOR_STOP && m_motorIndex > dSTART_POINT && m_motorIndex <= dEND_POINT)
  {
    digitalWrite(6, LOW);
    digitalWrite(3, LOW);
    m_motorDir = LOW;
    m_motorState = eMOTOR_RUN;
    moveMotor();
  }
}



void loop()
{

  digitalWrite(5, HIGH);
  delayMicroseconds(m_motorDelay);
  digitalWrite(5, LOW);
  delayMicroseconds(m_motorDelay);
  lightLevel=analogRead(photoRPin);
  if (lightLevel <= 600){   
    digitalWrite(ledPin,ledState);
    workingMode=ledState;
    digitalWrite(5, LOW);
    digitalWrite(7, LOW);
  }




  digitalWrite(2, HIGH);
  delayMicroseconds(m_motorDelay);
  digitalWrite(2, LOW);   
  delayMicroseconds(m_motorDelay);
  lightLevel_2=analogRead(photoRPin_2);
  if (lightLevel_2 <= 600){   
    digitalWrite(ledPin_2,ledState);
    workingMode_2=ledState;
    digitalWrite(4, LOW);
    digitalWrite(2, LOW);

  }

  //Locking Function while both of sensors are ON!

  if (workingMode==ledState && !workingMode_2==ledState)
  {
    digitalWrite(5, LOW);
    digitalWrite(7, LOW);
  }
  if(workingMode_2==ledState && !workingMode==ledState)
  {
    digitalWrite(4, LOW);
    digitalWrite(2, LOW);
  }


  if (workingMode==ledState &&   workingMode_2==ledState){
    moterProc();

    if (digitalRead(dSWITCH_PIN_1) == HIGH) //When button 1 is pressed
    {
      if(m_switch1State == eSWITCH_STATE_UP)                                                                                                                               
      {
        m_switch1State = eSWITCH_STATE_DOWN; //Change the button state pressed
        pushSwitch1or2();
      }
    }
    else if (digitalRead(dSWITCH_PIN_1) == LOW)
    {
      m_switch1State = eSWITCH_STATE_UP;
    }
    if (digitalRead(dSWITCH_PIN_2) == HIGH)
    {
      if(m_switch2State == eSWITCH_STATE_UP)
      {
        m_switch2State = eSWITCH_STATE_DOWN; //When button 2 is pressed
        pushSwitch1or2();
      }
    }
    else if (digitalRead(dSWITCH_PIN_2) == LOW)
    {
      m_switch2State = eSWITCH_STATE_UP;
    }
  }// Close Working MODE




  if (digitalRead(dSWITCH_PIN_3) == HIGH)
  {
    if(m_switch3State == eSWITCH_STATE_UP)
    {
      m_switch3State = eSWITCH_STATE_DOWN;
      pushSwitch3();
    }
  }
  else if (digitalRead(dSWITCH_PIN_3) == LOW)
  {
    if(m_switch3State == eSWITCH_STATE_DOWN)
    {
      m_switch3State = eSWITCH_STATE_UP;
      m_motorState = eMOTOR_STOP;
    }
  }

  if (digitalRead(dSWITCH_PIN_4) == HIGH)
  {
    if(m_switch4State == eSWITCH_STATE_UP)
    {
      m_switch4State = eSWITCH_STATE_DOWN;
      pushSwitch4();
    }
  }
  else if (digitalRead(dSWITCH_PIN_4) == LOW)
  {
    if(m_switch4State == eSWITCH_STATE_DOWN)
    {
      m_switch4State = eSWITCH_STATE_UP;
      m_motorState = eMOTOR_STOP;
    }
  }

} //Close Loop



I can't really find the part in my code where possibly make motors go slower.
Any advise or suggestion would be great.
10  Using Arduino / Motors, Mechanics, and Power / Problem with getting fast speed with heavy load. on: November 14, 2013, 01:10:52 pm
So Firstly what i trying to do is lifting 100Kg load with two stepper motor, 4 switch and 2 photo sensor in 400RPM speed .
And problem i am going through is the motor seems not running fast enough then it's data sheet figure. now the motors are running in 250RPM

the motors i am using SANYO DENKI 60mm 1.8 degree bipolar stepper motor (103H7823-1740) and the motor drivers is UIM24004 ( from zappautomation.co.uk)

and the code below is what i wrote.
I can't really think what can be the problem. According to the figure on database they should run faster then this.
It would be great if anyone can suggest problem on my code or better solution.

Thank you.



Code:
#define dSTART_POINT 0        //Start Position
#define dEND_POINT 2000       // Target Position
#define dDELAY_FAST 650       // Minimum Delay Value
#define dDELAY_SLOW 300  // Max Delay Value
#define dSWITCH_PIN_1 8     //Button one
#define dSWITCH_PIN_2 9     //Button two
#define dSWITCH_PIN_3 10    //Button three
#define dSWITCH_PIN_4 11    //Button four

enum
{
  eMOTOR_STOP = 0,
  eMOTOR_RUN,
};

enum
{
  eSWITCH_STATE_UP = 0,        // normal button state
  eSWITCH_STATE_DOWN           // When it is pressed
};

int m_motorIndex = 0;                   //motor position
int m_motorDir = HIGH;                  //motor direction
int m_motorState = eMOTOR_STOP;         //motor state

int m_switch1State = eSWITCH_STATE_UP;  //button one state
int m_switch2State = eSWITCH_STATE_UP;  //button two state
int m_switch3State = eSWITCH_STATE_UP;  //button three state
int m_switch4State = eSWITCH_STATE_UP;  //button four state
int m_motorDelay= dDELAY_SLOW;          //motor delay


int photoRPin = 0;                 //Sensor Pins
int photoRPin_2 = 1;

int lightLevel;                    //Sensor Value
int lightLevel_2;

int ledPin = 13;                   //LED
int ledPin_2 = 12;

int ledState=HIGH;                  //LED Value

int workingMode;                   
int workingMode_2;

void setup()
{
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT); // Motor Dir
  pinMode(5, OUTPUT); // Motor Step
  pinMode(4, OUTPUT);
  pinMode(3, OUTPUT); // Motor Dir
  pinMode(2, OUTPUT); // Motor Step

  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);

  pinMode(dSWITCH_PIN_1, INPUT);
  pinMode(dSWITCH_PIN_2, INPUT);
  pinMode(dSWITCH_PIN_3, INPUT);
  pinMode(dSWITCH_PIN_4, INPUT);

  Serial.begin(9600);

  digitalWrite(7, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(3, HIGH);

  lightLevel=analogRead(photoRPin);
  lightLevel_2=analogRead(photoRPin_2);
  m_motorIndex = 0;
}


void moveMotor()
{
  digitalWrite(7, HIGH);
  digitalWrite(5, HIGH);
  delayMicroseconds(m_motorDelay);
  digitalWrite(5, LOW);
  delayMicroseconds(m_motorDelay);

  digitalWrite(4, HIGH);
  digitalWrite(2, HIGH);
  delayMicroseconds(m_motorDelay);
  digitalWrite(2, LOW);
  delayMicroseconds(m_motorDelay);


  if(m_motorDir == HIGH)
  {
    m_motorIndex ++;
  }
  else 
  {
    m_motorIndex --;
  }
}


void moterProc()
{

  if(m_motorState == eMOTOR_STOP){
    digitalWrite(5, LOW);
    digitalWrite(7, LOW);
    digitalWrite(4, LOW);
    digitalWrite(2, LOW);
  }

  else if(m_motorState == eMOTOR_RUN && m_motorIndex <= dEND_POINT && m_motorIndex >= dSTART_POINT)
  {
    moveMotor();
  }


  if( m_motorIndex <= dSTART_POINT || m_motorIndex >= dEND_POINT)
  {
    m_motorState = eMOTOR_STOP;
    digitalWrite(5, LOW);
    digitalWrite(7, LOW);
    digitalWrite(4, LOW);
    digitalWrite(2, LOW);
  }
}






void pushSwitch1or2()
{
  if( m_motorState == eMOTOR_RUN)
  {
    m_motorState = eMOTOR_STOP;
  }

  else if(m_motorState == eMOTOR_STOP && m_motorIndex == dSTART_POINT)
  {
    digitalWrite(6, HIGH);
    digitalWrite(3, HIGH);
    m_motorDir = HIGH;
    m_motorState = eMOTOR_RUN;
    moveMotor();
  }
  else if(m_motorState == eMOTOR_STOP && m_motorIndex == dEND_POINT)
  {
    digitalWrite(6, LOW);
    digitalWrite(3, LOW);
    m_motorDir = LOW;
    m_motorState = eMOTOR_RUN;
    moveMotor();
  }
  else if(m_motorState == eMOTOR_STOP && m_motorIndex >= (dEND_POINT/2))
  {
    digitalWrite(6, HIGH);
    digitalWrite(3, HIGH);
    m_motorDir = HIGH;
    m_motorState = eMOTOR_RUN;
    moveMotor();
  }
  else if(m_motorState == eMOTOR_STOP && m_motorIndex < (dEND_POINT/2))
  {
    digitalWrite(6, LOW);
    digitalWrite(3, LOW);
    m_motorDir = LOW;
    m_motorState = eMOTOR_RUN;
    moveMotor();
  }
}



void pushSwitch3()
{
  if( m_motorState == eMOTOR_RUN)
  {
    m_motorState = eMOTOR_STOP;
  }//If motor is running, it stops

  // if the moter stopps and it is in working area



  else if(m_motorState == eMOTOR_STOP && m_motorIndex >= dSTART_POINT && m_motorIndex < dEND_POINT)
  {
    digitalWrite(6, HIGH);
    digitalWrite(3, HIGH);
    m_motorDir = HIGH;
    m_motorState = eMOTOR_RUN;
    moveMotor();
  }
}





void pushSwitch4()
{
  if( m_motorState == eMOTOR_RUN)
  {
    m_motorState = eMOTOR_STOP;
  }

  else if(m_motorState == eMOTOR_STOP && m_motorIndex > dSTART_POINT && m_motorIndex <= dEND_POINT)
  {
    digitalWrite(6, LOW);
    digitalWrite(3, LOW);
    m_motorDir = LOW;
    m_motorState = eMOTOR_RUN;
    moveMotor();
  }
}



void loop()
{

  digitalWrite(5, HIGH);
  delayMicroseconds(m_motorDelay);
  digitalWrite(5, LOW);
  delayMicroseconds(m_motorDelay);
  lightLevel=analogRead(photoRPin);
  if (lightLevel <= 600){   
    digitalWrite(ledPin,ledState);
    workingMode=ledState;
    digitalWrite(5, LOW);
    digitalWrite(7, LOW);
  }




  digitalWrite(2, HIGH);
  delayMicroseconds(m_motorDelay);
  digitalWrite(2, LOW);   
  delayMicroseconds(m_motorDelay);
  lightLevel_2=analogRead(photoRPin_2);
  if (lightLevel_2 <= 600){   
    digitalWrite(ledPin_2,ledState);
    workingMode_2=ledState;
    digitalWrite(4, LOW);
    digitalWrite(2, LOW);

  }

  //Locking Function while both of sensors are ON!

  if (workingMode==ledState && !workingMode_2==ledState)
  {
    digitalWrite(5, LOW);
    digitalWrite(7, LOW);
  }
  if(workingMode_2==ledState && !workingMode==ledState)
  {
    digitalWrite(4, LOW);
    digitalWrite(2, LOW);
  }


  if (workingMode==ledState &&   workingMode_2==ledState){
    moterProc();

    if (digitalRead(dSWITCH_PIN_1) == HIGH) //When button 1 is pressed
    {
      if(m_switch1State == eSWITCH_STATE_UP)                                                                                                                               
      {
        m_switch1State = eSWITCH_STATE_DOWN; //Change the button state pressed
        pushSwitch1or2();
      }
    }
    else if (digitalRead(dSWITCH_PIN_1) == LOW)
    {
      m_switch1State = eSWITCH_STATE_UP;
    }
    if (digitalRead(dSWITCH_PIN_2) == HIGH)
    {
      if(m_switch2State == eSWITCH_STATE_UP)
      {
        m_switch2State = eSWITCH_STATE_DOWN; //When button 2 is pressed
        pushSwitch1or2();
      }
    }
    else if (digitalRead(dSWITCH_PIN_2) == LOW)
    {
      m_switch2State = eSWITCH_STATE_UP;
    }
  }// Close Working MODE




  if (digitalRead(dSWITCH_PIN_3) == HIGH)
  {
    if(m_switch3State == eSWITCH_STATE_UP)
    {
      m_switch3State = eSWITCH_STATE_DOWN;
      pushSwitch3();
    }
  }
  else if (digitalRead(dSWITCH_PIN_3) == LOW)
  {
    if(m_switch3State == eSWITCH_STATE_DOWN)
    {
      m_switch3State = eSWITCH_STATE_UP;
      m_motorState = eMOTOR_STOP;
    }
  }

  if (digitalRead(dSWITCH_PIN_4) == HIGH)
  {
    if(m_switch4State == eSWITCH_STATE_UP)
    {
      m_switch4State = eSWITCH_STATE_DOWN;
      pushSwitch4();
    }
  }
  else if (digitalRead(dSWITCH_PIN_4) == LOW)
  {
    if(m_switch4State == eSWITCH_STATE_DOWN)
    {
      m_switch4State = eSWITCH_STATE_UP;
      m_motorState = eMOTOR_STOP;
    }
  }

} //Close Loop

11  Using Arduino / Programming Questions / Re: How to make a sensor working once only when arduino is restarted? on: June 20, 2013, 05:39:12 am
I am sorry.
This is the code that i tried.



Code:
const int tcrtPin = 2;     // connected to the TCRT5000 C pin
const int led =  13;      // the number of the LED pin
 
// variables will change:
int tcrtState = 0;         // variable for reading the TCRT5000 status
 
void setup() {
  // initialize the LED pin as an output:
  pinMode(led, OUTPUT);     
   pinMode(6, OUTPUT);       // Motor Dir
   pinMode(5, OUTPUT);
   pinMode(7, OUTPUT);
  // initialize the tcrt5000 pin as an input, and turn on the internal pullup resistors:
  pinMode(tcrtPin, INPUT);
  digitalWrite(tcrtPin, HIGH); 
  digitalWrite(7, HIGH);
    // read the state of the tcrt5000:
  tcrtState = digitalRead(tcrtPin);
 
  // check if the tcrt5000 sensor detects something.
  // if it is, the tcrtState is LOW:
  if (tcrtState == LOW) {     
    // turn LED on:   
    digitalWrite(7, HIGH);
    digitalWrite(led, HIGH); 
    digitalWrite(6,HIGH);
      digitalWrite(5,HIGH);
      delayMicroseconds(500);
      digitalWrite(5,LOW);
      delayMicroseconds(500);
  }
}
 
void loop(){

 
    // turn LED off:
      digitalWrite(7, HIGH);
    digitalWrite(led, HIGH); 
    digitalWrite(6,LOW);
      digitalWrite(5,HIGH);
      delayMicroseconds(500);
      digitalWrite(5,LOW);
      delayMicroseconds(500);

}
12  Using Arduino / Programming Questions / Re: How to make a sensor working once only when arduino is restarted? on: June 20, 2013, 05:24:41 am
Yes I tried but that's not working. maybe it shouldn't be in if statement?
13  Using Arduino / Programming Questions / How to make a sensor working once only when arduino is restarted? on: June 20, 2013, 04:37:12 am
Hello Basically what i want to do is :

When arduino board is started a motor turns to the right until an optical sensor detects an object, and once the sensor detects an object it repeat the loop.
So I want to sensor only works once in the beginning of every time Arduino restarted.
Any comment or advise would be really helpful. thanks

Code:
const int tcrtPin = 2;     // connected to the TCRT5000 C pin
const int led =  13;      // the number of the LED pin
 
// variables will change:
int tcrtState = 0;         // variable for reading the TCRT5000 status
 
void setup() {
  // initialize the LED pin as an output:
  pinMode(led, OUTPUT);    
   pinMode(6, OUTPUT);       // Motor Dir
   pinMode(5, OUTPUT);
   pinMode(7, OUTPUT);
  // initialize the tcrt5000 pin as an input, and turn on the internal pullup resistors:
  pinMode(tcrtPin, INPUT);
  digitalWrite(tcrtPin, HIGH);  
  digitalWrite(7, HIGH);
}
 
void loop(){
  // read the state of the tcrt5000:
  tcrtState = digitalRead(tcrtPin);
 
  // check if the tcrt5000 sensor detects something.
  // if it is, the tcrtState is LOW:
  if (tcrtState == LOW) {    
    // turn LED on:    
    digitalWrite(7, HIGH);
    digitalWrite(led, HIGH);  
    digitalWrite(6,HIGH);
      digitalWrite(5,HIGH);
      delayMicroseconds(500);
      digitalWrite(5,LOW);
      delayMicroseconds(500);
  }
  else {
        digitalWrite(6,LOW);
      digitalWrite(5,HIGH);
      delayMicroseconds(500);
      digitalWrite(5,LOW);
      delayMicroseconds(500);
  }
}

and this is the loop I want to repeat


Code:
   

     digitalWrite(6,LOW);
      digitalWrite(5,HIGH);
      delayMicroseconds(500);
      digitalWrite(5,LOW);
      delayMicroseconds(500);
14  Using Arduino / Programming Questions / Push button doesn't response well. on: June 12, 2013, 11:24:36 am
Hello, I am trying to control a motor with 4 push buttons.
button 3 and 4 are working completely well.
but button 1 and 2 are sometimes not working with first next press.
it need to be pressed twice to move motor again.
Any idea what it's problem is?


Code:
#define dSTART_POINT 0 //Start Position
#define dEND_POINT 20000 // Target Position
#define dDELAY_FAST 25 // Min Delay Value
#define dDELAY_SLOW 100 // Max Delay Value
#define dSWITCH_PIN_1 8 //Button one
#define dSWITCH_PIN_2 9 //Button two
#define dSWITCH_PIN_3 10 //Button three
#define dSWITCH_PIN_4 11 //Button four

enum
{
eMOTOR_STOP = 0,
eMOTOR_RUN,
};

enum
{
eSWITCH_STATE_UP = 0, // normal state
eSWITCH_STATE_DOWN, // When it is pressed
};

int m_motorIndex = 0;//motor position
int m_motorDir = HIGH;//motor dir
int m_motorState = eMOTOR_STOP;//motor state
int m_switch1State = eSWITCH_STATE_UP; //button one state
int m_switch2State = eSWITCH_STATE_UP; //button two state
int m_switch3State = eSWITCH_STATE_UP; //button three state
int m_switch4State = eSWITCH_STATE_UP; //button four state
int m_motorDelay= dDELAY_SLOW; //motor delay

void setup()
{
pinMode(6, OUTPUT); // Motor Dir
pinMode(5, OUTPUT); // Motor Step
pinMode(13, OUTPUT);
pinMode(dSWITCH_PIN_1, INPUT);
pinMode(dSWITCH_PIN_2, INPUT);
pinMode(dSWITCH_PIN_3, INPUT);
pinMode(dSWITCH_PIN_4, INPUT);
Serial.begin(9600);
digitalWrite(6, HIGH);
m_motorIndex = 0;
}






void moveMotor()
{
digitalWrite(5, HIGH);
delayMicroseconds(m_motorDelay);
digitalWrite(5, LOW);
delayMicroseconds(m_motorDelay);

if(m_motorDir == HIGH)
{m_motorIndex ++;}
else  
{
digitalWrite(13, HIGH);
m_motorIndex --;}
}

// 1 CONDITION: DETACT MOTOR STATE AND RESPOSE
// 2 CONDITION: SOPEED CONTROLL
// 3 CONDITION: TRAP MOTOR IN MOVING AREA

void moterProc()

{
if(m_motorState == eMOTOR_STOP) {digitalWrite(5, LOW);}
else if(m_motorState == eMOTOR_RUN && m_motorIndex <= dEND_POINT && m_motorIndex >= dSTART_POINT)
{moveMotor();

if( (dEND_POINT - m_motorIndex) <= (dDELAY_SLOW - dDELAY_FAST)) // INCREASE SPEED
{
if(m_motorDelay < dDELAY_SLOW)
m_motorDelay +=2;
}
else if( (m_motorIndex - dSTART_POINT) <= (dDELAY_SLOW - dDELAY_FAST)) // REDUCE SPPED
{
if(m_motorDelay < dDELAY_SLOW)
m_motorDelay +=2;
}
else
{
if(m_motorDelay > dDELAY_FAST)
m_motorDelay -=2;
}
}
if(m_motorIndex >= dEND_POINT || m_motorIndex <= dSTART_POINT)
{
m_motorState = eMOTOR_STOP;
digitalWrite(5, LOW);
m_motorDelay = dDELAY_SLOW;
}
else
{
digitalWrite(13,LOW);
}
}






void pushSwitch1or2()
{
if( m_motorState == eMOTOR_RUN)
{
m_motorState = eMOTOR_STOP;
}



else if(m_motorState == eMOTOR_STOP && (m_motorIndex == dEND_POINT || m_motorIndex < 10000))
{
digitalWrite(6, HIGH);
m_motorDir = HIGH;
m_motorState = eMOTOR_RUN;
moveMotor();
}
else if(m_motorState == eMOTOR_STOP && (m_motorIndex == dSTART_POINT || m_motorIndex > 10000))
{
digitalWrite(6, LOW);
m_motorDir = LOW;
m_motorState = eMOTOR_RUN;
moveMotor();
}
}




void pushSwitch3()
{
 if(m_motorState == eMOTOR_STOP && (m_motorIndex == dEND_POINT || m_motorIndex > dSTART_POINT))
{
digitalWrite(6, LOW); //
m_motorDir = LOW;
moveMotor();
}



else
{

m_motorState = eMOTOR_STOP;
}
}

void pushSwitch4()
{
 if(m_motorState == eMOTOR_STOP && (m_motorIndex == dSTART_POINT || m_motorIndex < dEND_POINT))
{
digitalWrite(6, HIGH); //
m_motorDir = HIGH;
moveMotor();
}

else
{
m_motorState = eMOTOR_STOP;
}
}


void loop()
{
//digitalWrite(6, HIGH); // Turn left
if (digitalRead(dSWITCH_PIN_1) == HIGH) //버튼 1 누르고 있을 때
{
if(m_switch1State == eSWITCH_STATE_UP)
{
m_switch1State = eSWITCH_STATE_DOWN; //버튼 1의 상태를 누른 상태로 변경
pushSwitch1or2();
}
}




else if (digitalRead(dSWITCH_PIN_1) == LOW)

{

m_switch1State = eSWITCH_STATE_UP; //버튼 1의 상태를 누른 상태로 변경

}









if (digitalRead(dSWITCH_PIN_2) == HIGH)
{
if(m_switch2State == eSWITCH_STATE_UP)
{
m_switch2State = eSWITCH_STATE_DOWN; //버튼 2의 상태를 누른 상태로 변경
pushSwitch1or2();
}



}
else if (digitalRead(dSWITCH_PIN_2) == LOW)
{
m_switch2State = eSWITCH_STATE_UP;
}








if (digitalRead(dSWITCH_PIN_3) == HIGH)
{
  pushSwitch3();
}


else if (digitalRead(dSWITCH_PIN_3) == LOW)
{
if(m_switch3State == eSWITCH_STATE_DOWN)
{
m_switch3State = eSWITCH_STATE_UP;
m_motorState = eMOTOR_STOP;
}
}




if (digitalRead(dSWITCH_PIN_4) == HIGH)
{
  pushSwitch4();
}


else if (digitalRead(dSWITCH_PIN_4) == LOW)
{
if(m_switch3State == eSWITCH_STATE_DOWN)
{
m_switch3State = eSWITCH_STATE_UP;
m_motorState = eMOTOR_STOP;
}
}

moterProc();
}
15  Using Arduino / Motors, Mechanics, and Power / I am looking for a stepper motor driver that can handle up to 5A on: May 24, 2013, 10:25:20 am
I am looking for a stepper motor driver that can handle up to 5A.
But most of stepper drivers i have seen can only handle up to 2A.
is there anyone who knows 5A or higher A stepper motor driver?
Pages: [1] 2 3 4