Help with this code please

Hello everybody,

I have tried doing this tutorial and I would like to use the V measurement to drive a solenoid meaning that if the incoming signal has value of 255 the solenoid would be driven in full power.

I (probably) understand that the value I need is "incomingAudio" which already is in 8 bits and I need to write something like "digitalWrite(12,incomingAudio);" but I have no idea where.

Help would be greatly appreciated.

The code is as follows

//Audio in with 38.5kHz sampling rate, interrupts, and clipping indicator
//by Amanda Ghassaei
//http://www.instructables.com/id/Arduino-Audio-Input/
//Sept 2012

/*

  • This program is free software; you can redistribute it and/or modify
  • it under the terms of the GNU General Public License as published by
  • the Free Software Foundation; either version 3 of the License, or
  • (at your option) any later version.

*/

//variable to store incoming audio sample
byte incomingAudio;

//clipping indicator variables
boolean clipping = 0;

void setup(){

pinMode(13,OUTPUT);//led indicator pin

cli();//disable interrupts

//set up continuous sampling of analog pin 0

//clear ADCSRA and ADCSRB registers
ADCSRA = 0;
ADCSRB = 0;

ADMUX |= (1 << REFS0); //set reference voltage
ADMUX |= (1 << ADLAR); //left align the ADC value- so we can read highest 8 bits from ADCH register only

ADCSRA |= (1 << ADPS2) | (1 << ADPS0); //set ADC clock with 32 prescaler- 16mHz/32=500kHz
ADCSRA |= (1 << ADATE); //enabble auto trigger
ADCSRA |= (1 << ADIE); //enable interrupts when measurement complete
ADCSRA |= (1 << ADEN); //enable ADC
ADCSRA |= (1 << ADSC); //start ADC measurements

sei();//enable interrupts

//if you want to add other things to setup(), do it here

}

ISR(ADC_vect) {//when new ADC value ready
incomingAudio = ADCH;//store 8 bit value from analog pin 0
if (incomingAudio == 0 || incomingAudio == 255){//if clipping
digitalWrite(13,HIGH);//set pin 13 high
clipping = 1;//currently clipping
}
}

void loop(){
if (clipping){//if currently clipping
clipping = 0;//
digitalWrite(13,LOW);//turn off clipping led indicator (pin 13)
}

delay(100);
}

Once again, thank you so much for your answers and help.

Jane, isn't this pretty much what you were asking in the other thread that you started earlier?
(Except that in this thread you've pasted someone's audio sampling code.)
I think this qualifies as a cross-post, which is frowned upon here.
Electromagnet as VU Meter

Anyway, show us the "solenoid", "electromagnet" or whatever it is that you wish to use as a VU meter. A photo or link would be nice.

Meantime, take a look at the 'analogWrite()' function.

Hi sorry,

I thought different forums for different people so I did cross post a little bit but in the first one I have asked why is the bypass necessary and in this one I am looking for specific answer to my programming problem.

The solenoid is a standard product (12V DC 55 LB 25kg Electric Lifting Magnet Electromagnet Solenoid Lift Holding | eBay). But thank you, I dont know why I thought digitalWrite had more than two values I thought I have remembered the basics well.

I am gonna try it now with the analogWrite() function but just in case I dont figure it out whats the best place to put it in the forementioned code?

Thank you for your answer and once again sorry for the cross-post even though I fear I wont get answer to both of my questions like this.

I have change the code to following (I am now using just LED diode instead of the solenoid) and it sort of works but it almost seems like its doing the opposite is there a reason for it?

//Audio in with 38.5kHz sampling rate, interrupts, and clipping indicator
//by Amanda Ghassaei
//http://www.instructables.com/id/Arduino-Audio-Input/
//Sept 2012

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
*/

//variable to store incoming audio sample
byte incomingAudio;
int ledPin = 12;
//clipping indicator variables
boolean clipping = 0;

void setup(){
  
  pinMode(13,OUTPUT);//led indicator pin

  
  cli();//disable interrupts
  
  //set up continuous sampling of analog pin 0
  
  //clear ADCSRA and ADCSRB registers
  ADCSRA = 0;
  ADCSRB = 0;
  
  ADMUX |= (1 << REFS0); //set reference voltage
  ADMUX |= (1 << ADLAR); //left align the ADC value- so we can read highest 8 bits from ADCH register only
  
  ADCSRA |= (1 << ADPS2) | (1 << ADPS0); //set ADC clock with 32 prescaler- 16mHz/32=500kHz
  ADCSRA |= (1 << ADATE); //enabble auto trigger
  ADCSRA |= (1 << ADIE); //enable interrupts when measurement complete
  ADCSRA |= (1 << ADEN); //enable ADC
  ADCSRA |= (1 << ADSC); //start ADC measurements
  
  sei();//enable interrupts

   pinMode(ledPin, OUTPUT);   // sets the pin as output

}

ISR(ADC_vect) {//when new ADC value ready
  incomingAudio = ADCH;//store 8 bit value from analog pin 0


   
  if (incomingAudio == 0 || incomingAudio == 255){//if clipping
    digitalWrite(13,HIGH);//set pin 13 high
    clipping = 1;//currently clipping
  }
}

void loop(){

 analogWrite(ledPin,incomingAudio);  
 
  if (clipping){//if currently clipping
    clipping = 0;//
    digitalWrite(13,LOW);//turn off clipping led indicator (pin 13)
      
  }


}

it sort of works but it almost seems like its doing the opposite

You will have to come up with a better description of what the code is currently doing and what you expect it (or would like it) to do.

JaneDawwon:
Hi sorry,

I thought different forums for different people so I did cross post a little bit

Don't.

AWOL:
Don't.

Its still a different question. I was just being polite.

cattledog:
You will have to come up with a better description of what the code is currently doing and what you expect it (or would like it) to do.

I am now trying to just get the LED work. I get a sound signal and based on the amplitude it should respond with the same strength from 0 to 255. Now it seemed like whenever there was stronger signal it lost some power and otherwise.

After some experimentation it seems like a get a lot of noise in there, I dont know why. So far I would like if somebody could verify that the code is right, especialy if I placed the "analogWrite(ledPin,incomingAudio);" in the right place.

Also I was wondering. Could the noise be from unsoldered wires to the jack? So for I have just connected it without soldering and I couldnt find any answer if there could be a problem.

Thank you for your interest and your answers!

Also I was wondering. Could the noise be from unsoldered wires to the jack? So for I have just connected it without soldering and I couldnt find any answer if there could be a problem.

Soldering would be better.

I see several things possibly wrong with the code.

 analogWrite(ledPin,incomingAudio);

This statement is located properly in the loop, but depending upon the Arduino you are using, it may not be correct in that analogWrite() does not work on all pins. analogWrite() - Arduino Reference

You have not said what Arduino you are using, but pin 12 will only work on a Mega. If that is not what you are using, then you will need to move the signal strength led to another pin with PWM.

//byte incomingAudio;
volatile byte incomingAudio;

This value is set within an ISR and then used within the body of the code, so it should be declared as volatile.

I'n not sure that the clipping indicator on led pin 13 is implemented correctly, in that the led is turned on within the ISR and then immediately turned off in the loop. I don't think you will see the clipping indicator led illuminated.

JaneDawwon:
Its still a different question. I was just being polite

No, the question in "Project Guidance" was word-for-word identical to the question in "Audio".
So, I deleted the latter.

Don't cross-post.

I have ever-so-slightly-less-than-zero tolerance to cross-posting.

cattledog:
Soldering would be better.

I see several things possibly wrong with the code.

 analogWrite(ledPin,incomingAudio);

This statement is located properly in the loop, but depending upon the Arduino you are using, it may not be correct in that analogWrite() does not work on all pins. analogWrite() - Arduino Reference

You have not said what Arduino you are using, but pin 12 will only work on a Mega. If that is not what you are using, then you will need to move the signal strength led to another pin with PWM.

Thank you so much for your response. I had no idea about the pin 12. The clipping code isnt mine and it does seem to work.

This value is set within an ISR and then used within the body of the code, so it should be declared as volatile.

I am sorry could you expand on this one a little bit please?

https://www.arduino.cc/en/Reference/Volatile

clipping should also be
volatile boolean