Cape Town South Africa
Offline
Edison Member
Karma: 17
Posts: 1123
A newbie with loads of posts, and still so much to learn !
|
 |
« on: November 30, 2012, 05:04:30 am » |
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 ); }
|
|
|
|
|
Logged
|
We live in the era of the smart phones and stupid people.
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 101
Posts: 9551
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #1 on: November 30, 2012, 06:02:38 am » |
pinMode ( analogPin, INPUT); not needed Did you know that CTRL-T cleans up the layout of code a bit?  can it be that the analogWrite() ==> PWM timer interferes with the VW library which might use a timer too?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Tesla Member
Karma: 76
Posts: 6849
Arduino rocks
|
 |
« Reply #2 on: November 30, 2012, 06:31:18 am » |
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
|
|
|
|
|
Cape Town South Africa
Offline
Edison Member
Karma: 17
Posts: 1123
A newbie with loads of posts, and still so much to learn !
|
 |
« Reply #3 on: November 30, 2012, 06:33:53 am » |
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
|
We live in the era of the smart phones and stupid people.
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 143
Posts: 19369
I don't think you connected the grounds, Dave.
|
 |
« Reply #4 on: November 30, 2012, 06:36:37 am » |
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.
|
|
|
|
Cape Town South Africa
Offline
Edison Member
Karma: 17
Posts: 1123
A newbie with loads of posts, and still so much to learn !
|
 |
« Reply #5 on: November 30, 2012, 07:26:39 am » |
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
|
We live in the era of the smart phones and stupid people.
|
|
|
|
New Jersey
Offline
Edison Member
Karma: 26
Posts: 2452
|
 |
« Reply #6 on: November 30, 2012, 07:35:36 am » |
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
Edison Member
Karma: 17
Posts: 1123
A newbie with loads of posts, and still so much to learn !
|
 |
« Reply #7 on: November 30, 2012, 07:49:30 am » |
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
|
We live in the era of the smart phones and stupid people.
|
|
|
|
New Jersey
Offline
Edison Member
Karma: 26
Posts: 2452
|
 |
« Reply #8 on: November 30, 2012, 08:01:53 am » |
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
Edison Member
Karma: 17
Posts: 1123
A newbie with loads of posts, and still so much to learn !
|
 |
« Reply #9 on: November 30, 2012, 09:39:55 am » |
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
|
We live in the era of the smart phones and stupid people.
|
|
|
|
Cape Town South Africa
Offline
Edison Member
Karma: 17
Posts: 1123
A newbie with loads of posts, and still so much to learn !
|
 |
« Reply #10 on: November 30, 2012, 03:32:35 pm » |
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
|
We live in the era of the smart phones and stupid people.
|
|
|
|
Cape Town South Africa
Offline
Edison Member
Karma: 17
Posts: 1123
A newbie with loads of posts, and still so much to learn !
|
 |
« Reply #11 on: December 01, 2012, 11:57:07 am » |
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
|
We live in the era of the smart phones and stupid people.
|
|
|
|
0
Offline
Tesla Member
Karma: 76
Posts: 6849
Arduino rocks
|
 |
« Reply #12 on: December 03, 2012, 04:46:38 pm » |
Glad its (mainly) resolved now...
|
|
|
|
|
Logged
|
|
|
|
|
Cape Town South Africa
Offline
Edison Member
Karma: 17
Posts: 1123
A newbie with loads of posts, and still so much to learn !
|
 |
« Reply #13 on: December 04, 2012, 12:16:48 am » |
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
|
We live in the era of the smart phones and stupid people.
|
|
|
|
|