confusion with analogRead

I have a problem with a sketch not working when I try to run analog function, here's the stripped down test sketch.

If I run it without the readlight (); call, it works fine, I get " Got" whenever I send a wireless code.

As soon as I try to run the readlight routine, there is no incoming message detected - i.e. no "Got "

I have tried various things but I obviously have missed something stupid.

I have tried calling the pin 19 or A5 with no luck.

#include <VirtualWire.h>

int val ;
int analogPin = 19;  //  reads LDR
int dimPin = 9;

void setup()
{
  Serial.begin(9600);	// Debugging only
  Serial.println("setup");
    pinMode ( dimPin, OUTPUT);
        pinMode ( analogPin, INPUT);

  vw_set_rx_pin(17 );        // set Rx   *******pin 9 for arduino rx jig******
  vw_setup(2000);	 // Bits per sec
  vw_rx_start();    
}
 
void loop () 
{ 
    readlight ();
  //        CHECK FOR INCOMING MESSAGE
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
  if (vw_get_message(buf, &buflen)) // Non-blocking
  {      
    Serial.println("Got: ");  // Show on PC for debugging
  }
} 
    void readlight () {  val = analogRead ( analogPin ); //  reads light from LDR
int temp = val/4  ;
 if ( temp <= 20 ) {temp = 1 ; }
  if ( temp >= 240 ) {temp = 200 ; }  // blanking pin higher number for darker  
 //Serial.print("                   temp light = ") ; Serial.println( temp ); 
  analogWrite ( dimPin,  temp );
  
  }

pinMode ( analogPin, INPUT); not needed

Did you know that CTRL-T cleans up the layout of code a bit? :slight_smile:

can it be that the analogWrite() ==> PWM timer interferes with the VW library which might use a timer too?

VirtualWire library uses timer1 so pins 9 and 10 are not available for analogWrite() as they require timer1 to be set up differently.
Use 3,5,6 or 11 instead.

Thanks guys,

I will try that.

and tidying up isnt on the agenda at the moment Rob :slight_smile: you should have seen it before I deleted pages of rubbish !

and tidying up isnt on the agenda

it should always be done before posting, as a simple courtesy to others.

Thats very true, I didnt even think of it at the time I posted, I dont use CTRL-T myself as I cant see as much code per page after it has spread it out, but I should do it when I post code.

I'd suggest if you have messy code, make it a priority to tidy, even if you're not going to show it to anyone else. Just cleaning up the indentation will sometimes show you where a bug (or potential one) lies. Moreover, why make life more difficult for yourself - coding has pitfalls enough - no need to add to your problems by making it hard to read.

Lastly, I'm sure this varies by person, but the last thing I'm going to do once my code is working is to spend time prettifying it so it had better be neat already so that when I come back to it months later, I have some chance to understand it.

I use my own system of indents to see where " loops" are etc. and if I hit tidy, it takes them all away, but I do tidy it up at the end.

The scraps I posted were the code with many lines erased, so it does look a mess. Sorry about that.

Not sure if this would help in your case, but you might take a look at GNU indent - it takes a gazzillion command line options, so you might be able to persuade it to format your code the way you like it indented if the IDE has mangled it for you.

Thanks wildbill, I will have a look at that when I get a minute, ( the reason I can't make a priority of tidying might be better told in a PM or barsports :slight_smile: )
meanwhile I have another problem with my current project, with the remote controls , and see that I am also using pins 9 and 10 for inputs ( and its transmitting with VirtualWire. )

Both remotes have the same problem, where one commutating pin ( 11 ) wont go low to sample switches, and yet a previous identical version worked fine ! The only difference is that I am using the P-UP chips now ?

I am using all the available pins, so I was thinking of rather using EasyTransfer instead of VW.
I used the Softeasytransfer ( a combination of EasyTransfer and NewSoftSerial ) recently and it worked perfectly, but does that use the same timer ? I had a look at the .h file but couldn't spot it.

I suppose I could use the hardware EasyTransfer, using the TXD pin, but I am not sure how that would work when I want to test the chip in the arduino board with the USB connected.

It seems to be working fine now with SoftEasyTransfer, no problems with the analog dimming, and the remote scans all the switches OK.

I havn't tried the wireless range yet, but the last project using ET was excellent ( and at 9600 baud )

I am only using simplex for this project, it seems it will ignore any pattern of data that is not expected, and it has a checksum.....

It wasnt receiving every time, I slowed the baudrate to 4800, no different, but when I compared the Tx and Rx waveforems on the radio modules, I saw there is no preamble on the transmission, so the first couple of bits were sometimes ignored.

I solved it by transmitting twice, the first burst acting as a preamble for the data slicer in the receive module, and it seems to be working fine.

VirtualWire library uses timer1 so pins 9 and 10 are not available for analogWrite() as they require timer1 to be set up differently.
Use 3,5,6 or 11 instead.

I ended up back with VirtualWire, as the transmision was not as reliable with EasyTransfer, and I took Marks advice and cut some tacks to swap pins on the boards, so 18 hours later I have it running, though I have had to add some averaging to the light sensor to stop the dimming pulsating ( which I didnt have to do with the other version )
One day when I get some time I will look into this to understand it better.

Thanks guys

Glad its (mainly) resolved now...

Thanks Mark, I had used all the pins on the remote control unit, and it intermittently missed reading the thumbwheel switch connected to pin 11, ( which VW uses for rxdata - which I am not using, and dont initiate ) so I declared pin 00 ( RXD ) as the VW Rx pin, and all worked.

What a weekend ! 20 yours a day since Thursday to get this finished by last Sunday ! whoops :frowning: