Digital Read delay

Hi all,
I have one problem, I'm using Arduino Mega and reading signal from my digital I/O. After that i want to switch a motor rotary direction when the signal is HIGH, but the switching goes one cycle later... I mean when i have LOW on Digital Input goes forward and when i get HIGH goes one cycle forward and then goes backward...

I think there is some delay that I can't find..

//The code to start shifting!
//The software is quite simple. You will need to create a register of 8 bits and shift it in when you want to update the actual register. The sample code blow illustrates a simple example which you can copy and paste into your project.

///////////////////////////////////
int DS_pin = 8;
int STCP_pin = 12;
int SHCP_pin = 4;
int EN_pin = 7;
int Speed_pin = 11;
int Sensor_pin = 24;
int var;
//boolean val = digitalRead(Sensor_pin);

void setup()
{
pinMode(DS_pin,OUTPUT);
pinMode(STCP_pin,OUTPUT);
pinMode(SHCP_pin,OUTPUT);
writereg();
pinMode(EN_pin,OUTPUT);
pinMode(Speed_pin,OUTPUT);
pinMode(Sensor_pin,INPUT);
Serial.begin(9600); // set up Serial library at 9600 bps

}

boolean registers[8];

void writereg()
{
digitalWrite(EN_pin,HIGH); //enable input
digitalWrite(STCP_pin, LOW);

for (int i = 7; i>=0; i--)
{
digitalWrite(SHCP_pin, LOW);
digitalWrite(DS_pin, registers );
digitalWrite(SHCP_pin, HIGH);
}
digitalWrite(STCP_pin, HIGH);
var = var+1;
}
void Forward()
{

  • digitalWrite(EN_pin,LOW); //enable input *
  • registers[2] = HIGH;*
  • registers[3] = LOW;*
  • delay(20);*
    }
    void Backward()
    {
  • digitalWrite(EN_pin,LOW); //enable input*
  • registers[2] = LOW;*
  • registers[3] = HIGH;*
  • delay(20);*
    }
    void Stop()
    {
  • digitalWrite(EN_pin,HIGH); //enable input*
    }

void loop()
{
while (digitalRead(Sensor_pin) == LOW)
{

  • digitalWrite(EN_pin,LOW); //enable input*
  • analogWrite(Speed_pin, 200);*
    Forward();
    delay(1000);
    writereg();
    Stop();
    delay(1000);
    writereg();
    digitalRead(Sensor_pin);
  • delay(1000);*

Serial.println(digitalRead(Sensor_pin)); //for visualization
delay(10);
}
while(digitalRead(Sensor_pin) == HIGH)
{

  • digitalWrite(EN_pin,LOW); //enable input*

  • analogWrite(Speed_pin, 200);*

  • Backward();*

  • delay(1000);*

  • writereg();*

  • Stop();*

  • delay(1000);*

  • writereg();*

Serial.println(digitalRead(Sensor_pin)); //for visualization
delay(10);
}
}
//his is an electrical problem, no software issue I presume.
//Here is how to proceed: 1) Make sure both boards have the
//same ground (connect GND together and make sure there is
//no conflict) 2) Connect your output to an input on the
//Arduino board (pin 2 e.g.). This connection is preferably
//done using a resistor, 1 kOhm will be ok.
//On software side, just set this pin as input
//pinMode(2,INPUT); in setup() and then get its value
//status = digitalRead(2); in loop().

And now without italics please

    Forward();
    delay(1000);
    writereg();

Why are you waiting a full second between the time you call 'Forward()' to set values in 'registers' and the time you use 'writereg()' to send the data out?

johnwasser:

    Forward();

delay(1000);
    writereg();



Why are you waiting a full second between the time you call 'Forward()' to set values in 'registers' and the time you use 'writereg()' to send the data out?

Because when i use delay after Forward it give me how long the motor will go forward... But that isn't the problem.

The problem is when I get 0 from my Sensor_pin , motor goes forward and when I get 1 goes forward again and then goes backward, which I seem unusual.

Ok, @Delta_G I will try to change the place of delay and write what happens. But I tried to remove the delay and the motor not working properly.