0

我正在制作在 3x 7 段显示器和 3x 74HC595 移位寄存器上制作的一键强盗老虎机。问题是我想要某种你赢了的指标(或者你部分赢了 - 2个数字是相同的)。

 losLiczb(latchPin3, dataPin3, clockPin3, data3, liczba3);  // liczba3 = in eg. 4
 Serial.println("Wylosowana liczba to");                    // "Generated number:"
 Serial.println(liczba3);                                   // liczba3 = 0

所以,liczba1/liczba2/liczba3 是在函数 losLiczb 中生成的数字。主要问题是 liczba1/2/3 在函数 losLiczb 内分配了数字(我可以通过串行读取检查正确性),但在数字 liczba1/2/3 之外是 0,因此程序将始终指示您赢了。如果您愿意,这是整个程序(一些评论是波兰语并且是不必要的,:

int latchPin1 = 5; //Pin connected to ST_CP of 74HC595
int clockPin1 = 4; //Pin connected to SH_CP of 74HC595
int dataPin1 = 6;  //Pin connected to DS of 74HC595

int latchPin2 = 8;
int clockPin2 = 7;
int dataPin2 = 9;

int latchPin3 = 11; //11
int clockPin3 = 12; //12
int dataPin3 = 10;   //8

int liczba1;
int liczba2;
int liczba3;

const int buttonPin = 2;
int statusPin = 3;
int buttonState = 0;


//holders for infromation you're going to pass to shifting function
byte data;
byte data1;
byte data2;
byte data3;

byte dataArray[10];
byte animacja[8];
byte dummy[1];


void setup() {
  //set pins to output because they are addressed in the main loop
  pinMode(latchPin1, OUTPUT);
  pinMode(latchPin2, OUTPUT);
  pinMode(latchPin3, OUTPUT);
  pinMode(statusPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);

  //Arduino doesn't seem to have a way to write binary straight into the code 
  //so these values are in HEX.  Decimal would have been fine, too. 
  // 00000000 - trzeba zapisywać w translatorze hex-bin w takiej postaci.
  // Kolejno od prawej wartości odpowiadają kropce, a, b , c , d, e , f , g

 // dataArray[2] = 0xB6; //11111100  FC

  dataArray[0] = 0x7F; 
  dataArray[1] = 0x0C;
  dataArray[2] = 0xB6;
  dataArray[3] = 0x9E; 
  dataArray[4] = 0xCC; 
  dataArray[5] = 0xDA;
  dataArray[6] = 0xFA; 
  dataArray[7] = 0x0E;
  dataArray[8] = 0xFE; 
  dataArray[9] = 0xDE; 

  animacja[0] = 0x80; //g
  animacja[1] = 0x20; //e
  animacja[2] = 0x10; //d
  animacja[3] = 0x08; //c
  animacja[4] = 0x80; //g
  animacja[5] = 0x40; //f
  animacja[6] = 0x02; //a
  animacja[7] = 0x04; //b

  dummy[0] = 0x00;

 randomSeed(analogRead(0));
}

void loop() {

  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {
   anim();


  } else {
    idle();
    digitalWrite(statusPin, LOW);
  }

}


void idle(){
  data = dummy[0];
      digitalWrite(latchPin1, 0);
      shiftOut(dataPin1, clockPin1, data);
      digitalWrite(latchPin1, 1);

      digitalWrite(latchPin2, 0);
      shiftOut(dataPin2, clockPin2, data);
      digitalWrite(latchPin2, 1);

      digitalWrite(latchPin3, 0);
      shiftOut(dataPin3, clockPin3, data);
      digitalWrite(latchPin3, 1); 
  }



// Funkcja odpowiedzialna za animację
void anim(){

  for (int p =0; p < 3; p++){
    for (int i = 0; i < 8; i++) {
      data = animacja[i];
      digitalWrite(latchPin1, 0);
      shiftOut(dataPin1, clockPin1, data);

      digitalWrite(latchPin2, 0);
      shiftOut(dataPin2, clockPin2, data);

      digitalWrite(latchPin3, 0);
      shiftOut(dataPin3, clockPin3, data);

      digitalWrite(latchPin1, 1);

      digitalWrite(latchPin2, 1);

      digitalWrite(latchPin3, 1);

      delay(100);
    }

  }
 losLiczb(latchPin1, dataPin1, clockPin1, data1, liczba1);
 Serial.println("Wylosowana liczba to");
 Serial.println(liczba1);

 for (int p =0; p < 2; p++){
    for (int i = 0; i < 8; i++) {
      data = animacja[i];
      digitalWrite(latchPin2, 0);
      shiftOut(dataPin2, clockPin2, data);

      digitalWrite(latchPin3, 0);
      shiftOut(dataPin3, clockPin3, data);

      digitalWrite(latchPin2, 1);
      digitalWrite(latchPin3, 1);
      delay(100);
    }
  }

  losLiczb(latchPin2, dataPin2, clockPin2, data2, liczba2);
  Serial.println("Wylosowana liczba to");
 Serial.println(liczba2);

  for (int p =0; p < 2; p++){
    for (int i = 0; i < 8; i++) {
      data = animacja[i];
      digitalWrite(latchPin3, 0);
      shiftOut(dataPin3, clockPin3, data);
      digitalWrite(latchPin3, 1);
      delay(100);
    }
  }

 losLiczb(latchPin3, dataPin3, clockPin3, data3, liczba3);
 Serial.println("Wylosowana liczba to");
 Serial.println(liczba3);



  if (data1 == data2){
   digitalWrite(statusPin, HIGH);
   } else {
    digitalWrite(statusPin, LOW);
  }




  delay(5000);

}

//Funckja odpowiedzialna za wylosowanie liczby
int losLiczb(int myLatchPin, int myDataPin, int myClockPin, int myData, int myLiczba) {

    //load the light sequence you want from array
   int j = random(0, 9);
   myLiczba = j;
    myData = dataArray[j];
    //ground latchPin and hold low for as long as you are transmitting
    digitalWrite(myLatchPin, 0);
    //move 'em out
    shiftOut(myDataPin, myClockPin, myData);
    //return the latch pin high to signal chip that it 
    //no longer needs to listen for information
    digitalWrite(myLatchPin, 1);
    Serial.println("Wylosowana liczba to");
    Serial.println(myLiczba);
} 



// the heart of the program
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
  // This shifts 8 bits out MSB first, 
  //on the rising edge of the clock,
  //clock idles low

  //internal function setup
  int i=0;
  int pinState;
  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, OUTPUT);

  //clear everything out just in case to
  //prepare shift register for bit shifting
  digitalWrite(myDataPin, 0);
  digitalWrite(myClockPin, 0);

  //for each bit in the byte myDataOut�
  //NOTICE THAT WE ARE COUNTING DOWN in our for loop
  //This means that %00000001 or "1" will go through such
  //that it will be pin Q0 that lights. 
  for (i=7; i>=0; i--)  {
    digitalWrite(myClockPin, 0);

    //if the value passed to myDataOut and a bitmask result 
    // true then... so if we are at i=6 and our value is
    // %11010100 it would the code compares it to %01000000 
    // and proceeds to set pinState to 1.
    if ( myDataOut & (1<<i) ) {
      pinState= 1;
    }
    else {  
      pinState= 0;
    }

    //Sets the pin to HIGH or LOW depending on pinState
    digitalWrite(myDataPin, pinState);
    //register shifts bits on upstroke of clock pin  
    digitalWrite(myClockPin, 1);
    //zero the data pin after shift to prevent bleed through
    digitalWrite(myDataPin, 0);
  }

  //stop shifting
  digitalWrite(myClockPin, 0);
}
4

1 回答 1

0

通过引用或指针传递您希望函数输出的值。

void Wrong(int a) {
  a = 10;
}

void Right(int& a) {
  a = 20;
}

void Right(int* pA) {
  *pA = 30;
}

int a = 0;
Wrong(a);  printf("%d ", a);
Right(a);  printf("%d ", a);
Right(&a);  printf("%d\n", a);

输出将是 0 20 30

于 2014-06-17T20:59:03.743 回答