Arduino:多路复用器代码不进入循环

时间:2014-05-18 00:30:27

标签: c for-loop arduino

在multiplex_read()函数中有一个似乎没有运行的for循环,我不知道它是怎么回事:

int write_pins[] = { 0,1,2,3,4,5,6 };
int max_min_array[16][1];
int multiplex_read_array[16];

int multiplex_read(){
  Serial.println("inside multiplex read");
  // ----------------------------------------------------
  // Having issues with this for loop
  // ----------------------------------------------------
  for (int mux_pin; mux_pin < 5; mux_pin++){
    Serial.println("in for loop");
    mux_pin_select(mux_pin);
    int sensorValue = analogRead(A0);
    Serial.print("# pin: ");
    Serial.print(mux_pin);
    Serial.print(" Value: ");
    Serial.print(sensorValue);
    Serial.println("");
    max_min(mux_pin,sensorValue);
  }
  return 0;
}

int mux_pin_select(int mux_pin){
  Serial.println("inside mux_pin_select");
  // sets select pins based on pin (pin = decimal)
  // write_pins 0,1 = empty
  // write_pins 2 = Disable
  // write_pins 3,4,5,6 = S0,S1,S2,S3
  int select_pin_map[] = { 3,4,5,6 };
  for ( int b; b < 3;b++){
    int bitset = bitRead(mux_pin, b);
    digitalWrite(select_pin_map[b], bitset);
  }
  return 0;
}

int max_min(int pin,int value){
  Serial.println("inside max_min");
  // set to initial reads and then move min_max
  if ( max_min_array[pin][0] == 0 ){
    max_min_array[pin][0] = value;
    max_min_array[pin][0] = value;
  }
  // min
  if ( max_min_array[pin][0] > value ){
    max_min_array[pin][0] = value;
  }
  // max
  if ( max_min_array[pin][1] < value ){
    max_min_array[pin][1] = value;
  }
  return 0;
}



void setup(){
  // clear max_min_array and multiplex_read_array
  for ( int i = 0; i <16; i++){
    max_min_array[i][0] = 0;
    max_min_array[i][1] = 0;
    multiplex_read_array[i] = 0;
  }
  // initialize write pins
  for ( int i = 0; i <= 7; i++){  
    pinMode(write_pins[i], OUTPUT);
  }
  Serial.begin(9600);
}

void loop(){
  if (Serial.available()){
    multiplex_read();
    Serial.println("### Min Max Array #########");
    for (int i; i < 16; i++){
      Serial.print(max_min_array[i][0]);
      Serial.print(" ");
      Serial.println(max_min_array[i][1]);
    }
  }
  delay(2000);
}

输出:

inside multiplex read
### Min Max Array #########
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0

1 个答案:

答案 0 :(得分:1)

初始化变量:

for (int mux_pin = 0 ; mux_pin < 5; mux_pin++){

您有很多硬编码值,因此请适当更改初始值。