如何将值返回到要使用的全局变量?

时间:2015-09-02 09:20:51

标签: random arduino global-variables lcd

我遇到了问题。我需要使用randomseed()的void循环中生成的随机数显示在LCD上,并存储在数组中。我根本无法在液晶显示屏上显示随机数。我得到的只是一个空白的液晶屏。我将在下面的代码中发表评论:

int randNumber; 
long randNumber1; 
const int led_pin = 13; 
const int transmit_pin = 12; 
const int receive_pin = 2;  
const int transmit_en_pin = 3;  
LiquidCrystal lcd(9, 11, 7, 6, 5, 4);

void setup() {
  lcd.begin(16, 2);
  lcd.clear();
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin80, INPUT);
  pinMode(led_pin, OUTPUT);
  SetupRFDataTxnLink(transmit_pin, baudRate);
  randomSeed(analogRead(0));
  // generate a random no. if there is no analog input 
}
void loop() {
  float outArray[RF_DATA_ARRAY_SIZE];
  //the number of elements that
  randNumber = random(0, 255);
  //here is where the randNumber are generated.
  randNumber1 = random(90,150);
  // read the state of the pushbutton value:
  buttonState1 = digitalRead(buttonPin80);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState1 == HIGH) {
    // if the button is pressed, send the generated no. to the transmitter.
    outArray[0] = randNumber;
    // Here is where the randNumber generated from the void loop
    outArray[1] = randNumber1;
    outArray[2] = 80;
    outArray[3] = 0;
    union RFData outDataSeq;
    EncodeRFData(outArray, outDataSeq);
    TransmitRFData(outDataSeq);
    lcd.setCursor(0, 0);
    lcd.print(randNumber);
    // Here is where I need the randomNumber for

我的问题是,我是否可以不断生成存储在全局变量中的randNumber,以便在此草图中的任何位置使用?

感谢您的帮助。

0 个答案:

没有答案