Pages: [1]   Go Down
Author Topic: confusion with analogRead  (Read 1070 times)
0 Members and 1 Guest are viewing this topic.
Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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.


Code:
#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 );
 
  }
Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13469
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

    pinMode ( analogPin, INPUT);  not needed

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

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

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

0
Offline Offline
Shannon Member
****
Karma: 198
Posts: 11625
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

[ I won't respond to messages, use the forum please ]

Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks guys, 

I will try that.

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

With my mobile phone I can call people and talk to them -  how smart can you get ?

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 285
Posts: 25630
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
and tidying up isnt on the agenda
it should always be done before posting, as a simple courtesy to others.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: November 30, 2012, 07:30:27 am by Boffin1 » Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3638
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3638
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 :-) )
 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.
Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: December 01, 2012, 12:37:50 am by Boffin1 » Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Quote
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

Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

0
Offline Offline
Shannon Member
****
Karma: 198
Posts: 11625
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Glad its (mainly) resolved now...
Logged

[ I won't respond to messages, use the forum please ]

Cape Town South Africa
Offline Offline
Edison Member
*
Karma: 19
Posts: 1217
A newbie with loads of posts, and still so much to learn !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 :-(
Logged

With my mobile phone I can call people and talk to them -  how smart can you get ?

Pages: [1]   Go Up
Jump to: