mic&leds

int speak=A0;
int arrLeds[7]={3,5,6,9,10,11};

void setup() {

pinMode(speak,INPUT);
pinMode(arrLeds,OUTPUT);

}

void loop() {
if (digitalRead(speak)==HIGH){
for(int i=0; i<=6; i=i+1){
analogWrite(arrLeds*,0);*

  • }*
    }
  • if(digitalRead(speak)==LOW){*
  • for(int i=6; i>=0; i=i-1){*
    _ analogWrite(arrLeds*,255);_
    _
    }_
    _
    }_
    _
    }*_
    Ive seen a lot of these made but are really long. Here is a shorter version.

Here is a shorter version.

complete with half of the code in italics because you did not use code tags and badly formatted.

As a matter of interest why did you use analogWrite() when digitalWrite() would have done the same thing ?
Why reverse the order of the for loops when they will run so quickly that it makes no difference ?
Why use int variables for pin numbers when const byte would be better ?
Why a 7 element array for 6 values ?

There were a few errors made. When i plugged 6 into my array, it would light my last LED very dimly and was not sure what was going on. Im sure i could have cleaned this up better, but posted because some people were posting programs that were unnecessarily long. If you do not like it, feel free to copy it and fix it.

How about something like this

const byte speak = A0;
const byte arrLeds[6] = {3, 5, 6, 9, 10, 11};

void setup()
{
  pinMode(speak, INPUT);
  for (int i = 0; i < 6; i++)
  {
    pinMode(arrLeds[i], OUTPUT);
  }
}

void loop()
{
  byte state = (digitalRead(speak)) ? LOW : HIGH;
  for (int i = 0; i < 6; i++)
  {
    digitalWrite(arrLeds[i], state);
  }
}

Not tested, but at least this version compiles.
Whether it does anything useful is a different matter.

Do you really have a microphone (an analog device) connected to a digital pin? A0 IS a digital pin. 0 is an analog pin (on the analog side of the board). You do not use digitalRead() to read an analog pin. You should NOT expect HIGH or LOW from an analog pin.

You do not use digitalRead() to read an analog pin.

Sorry Paul, but you have lost me. Are you saying that after

pinMode(A0, INPUT);

the pin cannot be used as a digital input ?

Are you saying that after ... the pin cannot be used as a digital input ?

No. pinMode affects the digital nature of the pin. Analog pins are input only. No need to tell the software something that it already knows.

One needs to choose a pin appropriately. Using a digital pin (and digitalRead()) to read an analog device (like a microphone) doesn't make sense.

Thanks for the clarification.

PaulS:
Analog pins are input only.

this this seems to contradict something in the tutorials regarding analog pins:

Arduino users is to read analog sensors, the analog pins also have all the functionality of general purpose input/output (GPIO) pins (the same as digital pins 0 - 13).

Consequently, if a user needs more general purpose input output pins, and all the analog pins are not in use, the analog pins may be used for GPIO.

Pin mapping

The analog pins can be used identically to the digital pins, using the aliases A0 (for analog input 0), A1, etc. For example, the code would look like this to set analog pin 0 to an output, and to set it HIGH:

pinMode(A0, OUTPUT);
digitalWrite(A0, HIGH);
Pullup resistors

The analog pins also have pullup resistors, which work identically to pullup resistors on the digital pins. They are enabled by issuing a command such as

digitalWrite(A0, INPUT_PULLUP); // set pullup on analog pin 0
Be aware however that turning on a pullup will affect the values reported by analogRead().

Details and Caveats

The analogRead command will not work correctly if a pin has been previously set to an output, so if this is the case, set it back to an input before using analogRead. Similarly if the pin has been set to HIGH as an output, the pullup resistor will be set, when switched back to an input.

so what gives?

I suspect that what Paul meant is that analogue pins are are for analogue input only unless defined as digital pins by the use of pinMode()

On most boards, all analog pins can act as digital pins, too. Not all pins on all boards, though.

The analog pins can not write analog values. That's why analogWrite() is such a lousy name for a function that does PWM.

Yes I understand that text could be misinterpreted. It means that all ports that can be used as analog, can still be used as normal (digital) input/output ports. Basically, all pins in an Arduino are digital I/O, and some of them are aditionally capable of Analog/In.

No Arduino board that I know of has any ANALOG OUT port. You usually need an external IC for that, or under certain circumstances you can use a resistor chain (and several digital pins) to get the same functionality

Basically, all pins in an Arduino are digital I/O, and some of them are aditionally capable of Analog/In.

Analog pins 6 and 7 on a Nano can NOT be used as digital pins.