0
votes

Im working on a fun (for me at least) project to better my skills with Arduino and C. Using an 8ch relay board and an Arduino Uno I've turned my christmas tree into a simple equalizer.

The problem I'm having is that every time I digital write HIGH to a pin that is already lit it blinks creating a strobe effect. This effect gets more noticeable the faster and more accurate the loop. I suspect my problem is my long drawn out code. I would prefer to update pins that need updating rather than constantly writing all HIGH or LOW. Is there a digital Update? Is there an existing function that remembers or checks the state of a pin I can utilize in an update function of my own? Or should I simply create and utilize variables for each channel?

int DO = 2; //Pin for Digital Output - DO
int DA = A0; // Pin for Analog Output - AO
int mult = 5;
int threshold1 = 1 * mult; //Set minimum threshold for LED lit
int threshold2 = 2 * mult; //Set minimum threshold for LED lit
int threshold3 = 3 * mult; //Set minimum threshold for LED lit
int threshold4 = 4 * mult; //Set minimum threshold for LED lit
int threshold5 = 5 * mult; //Set minimum threshold for LED lit
int threshold6 = 6 * mult; //Set minimum threshold for LED lit
int threshold7 = 7 * mult; //Set minimum threshold for LED lit
int threshold8 = 8 * mult; //Set minimum threshold for LED lit
int threshold9 = 9 * mult; //Set minimum threshold for LED lit
int sensorvalue = 0;

//pinout
int white1 = 3;
int white2 = 4;
int white3 = 5;
int white4 = 6;
int blue1  = 7;
int blue2  = 8;
int blue3  = 9;
int red1   = 10;

void loop() {
    sensorvalue = ReadSens_and_Condition();  //Read the analog value

    //Troubleshooting
    //sensorvalue = 3;
    Serial.print("Analog: ");
    Serial.print(sensorvalue);  //Print the analog value
    Serial.print("  ");
    Serial.print("Threshold: ");
    Serial.print(threshold1);  //Print the analog value
    Serial.print("  ");
    Serial.print(threshold2);  //Print the analog value
    Serial.print("  ");
    Serial.print(threshold3);  //Print the analog value
    Serial.print("  ");
    Serial.print(threshold4);  //Print the analog value
    Serial.print("  ");
    Serial.print(threshold5);  //Print the analog value
    Serial.print("  ");
    Serial.print(threshold6);  //Print the analog value
    Serial.print("  ");
    Serial.print(threshold7);  //Print the analog value
    Serial.print("  ");
    Serial.print(threshold8);  //Print the analog value
    Serial.print("  ");
    Serial.print(threshold9);  //Print the analog value
    Serial.print("  ");
    Serial.print('\n');
    Serial.print('\n');

//mode 3 is best so far
int mode = 3;

int ReadSens_and_Condition(){
  int i;
  int sval = 0;

  for (i = 0; i < 50; i++){
    sval = sval + analogRead(DA);    // sensor on analog pin 0
  }

  sval = sval / 50;    // average
  sval = sval / 4;    // scale
  return sval;
}

void setup() {
  Serial.begin(9600);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);

}

//equalizer
if(mode == 3){

    //delay
    int pause = 5;

    if(sensorvalue >= threshold9){
      digitalWrite(white3, HIGH);
      digitalWrite(blue3, HIGH);
      digitalWrite(white2, HIGH);
      digitalWrite(blue2, HIGH);
      digitalWrite(white1, HIGH);
      digitalWrite(blue1, HIGH);
      digitalWrite(white4, HIGH);
      digitalWrite(red1, HIGH);
      delay(pause);
    }

    else if(sensorvalue >= threshold8){
      digitalWrite(white3, HIGH);
      digitalWrite(blue3, HIGH);
      digitalWrite(white2, HIGH);
      digitalWrite(blue2, HIGH);
      digitalWrite(white1, HIGH);
      digitalWrite(blue1, HIGH);

      delay(pause);

      digitalWrite(white4, LOW);
      digitalWrite(red1, LOW);

    }

    else if(sensorvalue >= threshold6){
      digitalWrite(white2, HIGH);
      digitalWrite(blue2, HIGH);
      digitalWrite(white1, HIGH);
      digitalWrite(blue1, HIGH);

      delay(pause);

      digitalWrite(white3, LOW);
      digitalWrite(blue3, LOW);
      digitalWrite(white4, LOW);
      digitalWrite(red1, LOW);
    }

    else if(sensorvalue >= threshold4){
      digitalWrite(white1, HIGH);
      digitalWrite(blue1, HIGH);

      delay(pause);

      digitalWrite(white2, LOW);
      digitalWrite(blue2, LOW);
      digitalWrite(white3, LOW);
      digitalWrite(blue3, LOW);
      digitalWrite(white4, LOW);
      digitalWrite(red1, LOW);

    }



    else if(sensorvalue >= threshold2){
      digitalWrite(white1, LOW);
      digitalWrite(white2, LOW);
      digitalWrite(white3, LOW);
      digitalWrite(white4, LOW);
      digitalWrite(blue1, LOW);
      digitalWrite(blue2, LOW);
      digitalWrite(blue3, LOW);
      digitalWrite(red1, LOW);
      delay(pause);

    }

  else {
      digitalWrite(white1, LOW);
      digitalWrite(white2, LOW);
      digitalWrite(white3, LOW);
      digitalWrite(white4, LOW);
      digitalWrite(blue1, LOW);
      digitalWrite(blue2, LOW);
      digitalWrite(blue3, LOW);
      digitalWrite(red1, LOW);
      delay(pause);
  }
}


}
1

1 Answers

1
votes

The problem I'm having is that every time I digital write HIGH to a pin that is already lit it blinks creating a strobe effect.

This is not the case with Atmel (or AFAIK any other) microcontrollers. If you set an output high which is already high, then it will remain high without any glitch. Furthermore, it takes only a few cycles to set the output, so if it was glitching then you would not see it without an oscilloscope - blinking a LED off for 0.00001 seconds is too fast to notice.

I have loaded your code ( with minor modifications so it is valid - you've put some function definitions into your loop function ) into an arduino ng with nothing else connected and all the outputs are stable when probed with a scope ( except for the serial out, which has data on it). By connecting a pot between 5V, GND and the analogue input I can cause each of the outputs to turn on or off, and when they do so they do not glitch or strobe.

I suspect you are seeing the result of noise in the analogue signal and this is causing the state to change back and forth. Try smoothing the value between iterations. Also you have delays half way through setting the output values; just put the delay once in the loop then the timing won't be as confused.