shift register with push button

Hello im sorry if i have posted this issue in the wrong area !!

Just wondering if some would be able the help me with this code as ive been getting an error message when compling, what im trying to achive with it is when the button is pressed it will flash the led, button 1 left, Button 2 right I am using 74HC595 i intend to daisy chain a couple more on to it but i am now stuck, and its been driving me crazy for the last week . I have attach the file and the error message below.

Arduino: 1.8.12 (Windows 7), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"

C:\Users\Doms mpc\Desktop\led\sequence_with_pushbutton\sequence_with_pushbutton.ino: In function 'void loop()':

sequence_with_pushbutton:78:44: error: expected primary-expression before 'for'

for(int i=0; i<2 data-blogger-escaped -for="" data-blogger-escaped-high="" data-blogger-escaped-i="" data-blogger-escaped-int="" data-blogger-escaped-low="" data-blogger-escaped-registers="" data-blogger-escaped-writereg="">=2; i--){

^~~

sequence_with_pushbutton:78:44: error: expected ';' before 'for'

sequence_with_pushbutton:78:44: error: expected primary-expression before 'for'

sequence_with_pushbutton:78:44: error: expected ')' before 'for'

sequence_with_pushbutton:78:47: error: expected '(' before '=' token

for(int i=0; i<2 data-blogger-escaped -for="" data-blogger-escaped-high="" data-blogger-escaped-i="" data-blogger-escaped-int="" data-blogger-escaped-low="" data-blogger-escaped-registers="" data-blogger-escaped-writereg="">=2; i--){

^

sequence_with_pushbutton:78:47: error: expected primary-expression before '=' token

sequence_with_pushbutton:78:76: error: expected primary-expression before '=' token

for(int i=0; i<2 data-blogger-escaped -for="" data-blogger-escaped-high="" data-blogger-escaped-i="" data-blogger-escaped-int="" data-blogger-escaped-low="" data-blogger-escaped-registers="" data-blogger-escaped-writereg="">=2; i--){

^

sequence_with_pushbutton:78:127: error: expected primary-expression before 'int'

for(int i=0; i<2 data-blogger-escaped -for="" data-blogger-escaped-high="" data-blogger-escaped-i="" data-blogger-escaped-int="" data-blogger-escaped-low="" data-blogger-escaped-registers="" data-blogger-escaped-writereg="">=2; i--){

^~~

sequence_with_pushbutton:78:236: error: expected ';' before ')' token

for(int i=0; i<2 data-blogger-escaped -for="" data-blogger-escaped-high="" data-blogger-escaped-i="" data-blogger-escaped-int="" data-blogger-escaped-low="" data-blogger-escaped-registers="" data-blogger-escaped-writereg="">=2; i--){

^

exit status 1
expected primary-expression before 'for'

thanks
Dom

sequence_with_pushbutton.ino (1.73 KB)

Look at the final for() loop. Clean that up and some problems will go away.

    for (int i = 0; i < 2 data - blogger - escaped - for = "" data - blogger - escaped - high = "" data - blogger - escaped - i = "" data - blogger - escaped - int = "" data - blogger - escaped - low = "" data - blogger - escaped - registers = "" data - blogger - escaped - writereg = "" >= 2; i--) {
        registers[i] = LOW;
        writeReg();
      }

You define some constants at the top of your code

#define data
#define blogger
#define high
#define escaped

and then you create this mess

    for(int i=0; i<2 data-blogger-escaped -for="" data-blogger-escaped-high="" data-blogger-escaped-i="" data-blogger-escaped-int="" data-blogger-escaped-low="" data-blogger-escaped-registers="" data-blogger-escaped-writereg="">=2; i--){
        registers[i] = LOW;
        writeReg();
      }

and, since those constants are defined as nothing, you get

    for(int i=0; i<2 - - -for=""  - - - =""  - - -i=""  - - -int=""  - - -low=""  - - -registers=""  - - -writereg="">=2; i--){
        registers[i] = LOW;
        writeReg();
      }

which still makes no sense. What are you trying to do with that for() loop?

Hi thanks for your quick response, it a project that was started over12 mths ago with a good friend of mine who unfortunatly passed away, and he added the loop, the whole code was meant to be for sequential turn signals and what is meant to happen was button 1 is pressed it will run a complete sequence (left ) button 2 would do the same for right

therat69:
Hi thanks for your quick response, it a project that was started over12 mths ago with a good friend of mine who unfortunatly passed away, and he added the loop, the whole code was meant to be for sequential turn signals and what is meant to happen was button 1 is pressed it will run a complete sequence (left ) button 2 would do the same for right

considering what you says you were trying to achive you may want to consider using bit shifting operation instead.

something like this in code maybe: (compiles, not tested!)

/*
  sequence with buttom
*/
uint8_t DS_pin = 8; //data pin
uint8_t STCP_pin = 9; //load/output data pin
uint8_t SHCP_pin = 10; //data clock pin
uint8_t buttonPin1 = 11;
uint8_t buttonPin2 = 12;
uint8_t reg_data, button1Value, button2Value;

void setup()
{
  pinMode(DS_pin, OUTPUT);
  pinMode(STCP_pin, OUTPUT);
  pinMode(SHCP_pin, OUTPUT);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  reg_data = 0;
  writeReg();
}

void writeReg()
{
  //split left and right bit information
  uint8_t right = reg_data & 0b00001111; //b0-b3 (equivalent to outputs Q0-Q3)
  uint8_t left = reg_data & 0b11110000; //b4-b7 (equivalent to outputs Q4-Q7)
  
  uint8_t temp;

  for (uint8_t i = 0; i <= 4; i++)
  {
    temp = left | right; //recombine left an right information

    //output 'temp' to shift register
    digitalWrite(STCP_pin, LOW);

    for (uint8_t j = 0; j < 8; ++j) {
      digitalWrite(SHCP_pin, LOW);
      
      //doing this since MSB should be out first
      if (temp & 0b10000000)digitalWrite(DS_pin, HIGH);
      else digitalWrite(DS_pin, LOW);
      //

      digitalWrite(SHCP_pin, HIGH);

      //left shift 'temp' by one bit
      temp <<= 1;
    }

    digitalWrite(STCP_pin, HIGH);
    
    //right shift 'right' by one bit
    right >>= 1;
    //left shift 'left' by one bit
    left <<= 1;

    delay(100);
  }

}

void loop()
{
  //assumption
  //Q4-Q7 - left (controlled by buttonPin1)
  //Q0-Q3 - right (controlled by buttonPin2)
  button1Value = digitalRead(buttonPin1);
  button2Value = digitalRead(buttonPin2);
  if (button1Value == HIGH && button2Value == LOW) { //sequence left lights
    reg_data = 0b00010000; //Q4 first to be HIGH
    writeReg();
  }
  else if (button1Value == LOW && button2Value == HIGH) { //sequence right lights
    reg_data = 0b00001000; //Q3 first to be HIGH
    writeReg();
  }
  else if (button1Value == HIGH && button2Value == HIGH) { //sequence BOTH left AND right lights
    reg_data = 0b00011000;
    writeReg();
  }
}

hope that helps....

EDIT: code amended as there was an error in 'reg_data' values used. comments updated

thank you very much, for you response and help I will test it later and let you know. I really really appriciate it

Thanks

Thanks alot for all your help it works very well, you guys are legends!!!