Line follower code logical errors - Page 1
Forum section: electronics / beginners

author: newtoneins10
author_rank: Newbie
timestamp: June 05, 2012, 03:36:23 pm
content: so this is my code which is now showing an error...wud u pls check...pls suggest how i can improve dis code..im a beginner nd any help will b appreciated#include <avr/io.h>#include <util/delay.h>void initADC(){DDRC|=0x00;ADMUX|=(1<<REFS0)|(1<<ADLAR);ADCSRA|=(1<<ADPS1)|(1<<ADPS2);}uint16_t readADC(uint8_t ch){ADMUX|=(ADMUX & 0xF0)|(ch & 0x0F);ADMUX|=ch;ADCSRA|=(1<<ADEN);ADCSRA|=(1<<ADSC);while(!(ADCSRA & ADIF));ADCSRA|=(1<<ADIF);return ADC;}void pwm_init(){TCCR1A|=(1<<COM1A1)|(1<<COM1A0)|(1<<WGM11)|(1<<WGM10);TCCR1B|=(1<<CS12);TCNT1=0;}void pwm(uint16_t left, uint16_t right){OCR1A=left;//left motorOCR1B=right;}void enable_motor(){PORTB|=(1<<PORTB1)|(1<<PORTB2); //check}void disable_motor(){PORTB &=~((1<<PORTB1)|(1<<PORTB2));}void motor(){uint8_t ch;int w,right,left,sum=0;unsigned char i;initADC();for(i=0;i<6;i++){w=(readADC(i)/1024)*5; //converwsion into ADC voltageif(w>2.5) ch=1; //inserting 1 or 0 in ch arrayelse ch=0;}for(i=0;i<4;i++){sum+=ch; //summing left (0-3)array elements}left=sum;for(;i<7;i++){sum+=ch;}right=sum; //right array elements (4-6)if(right>left){_delay_ms(10);if(right>1) { //hard rightpwm(2,1024);//checkPORTD=0x09; //0b00001001 right motor in forward dir left reverse}else {pwm(2,1024);}if(right<left){_delay_ms(10);if(left>1) {pwm(1024,2);PORTD=0x06; //0b00000110 right motor reverse left motor forward}else pwm(1024,2);}else {_delay_ms(10);pwm(1024,1024); //left=right} //more modification when all da digits are 1 last direction history}void main(){DDRD=0xFF;DDRC=0xFF;PORTD=PORTC=0x00;initADC();pwm_init();enable_motor();while(1) {motor();}}

author: MikeK
author_rank: Super Contributor
timestamp: June 05, 2012, 04:35:01 pm
content: Do you want to tell us the error, or just keep it to yourself?

author: Mechatrommer
author_rank: Super Contributor
timestamp: June 05, 2012, 05:10:21 pm
content: let me see. wrong indentation, unneccessary spacing. comments can be improved, by explaining what each function do. that the errors/improvement i think. i like the way you bracket each function though (bracket on the right of function name instead of at the bottom), make the code more compact.Code:myfunc(arg) {if (true) {// do something;} else {// do something else;};}is more compact thanCode:myfunc(arg){if (true){// do something;}else{// do something else;};}but that just a personal preferences.

author: olsenn
author_rank: Frequent Contributor
timestamp: June 05, 2012, 05:53:33 pm
content: QuoteYou don't need a semicolon after the closing bracket

author: Mechatrommer
author_rank: Super Contributor
timestamp: June 05, 2012, 07:30:26 pm
content: Quote from: olsenn on June 05, 2012, 05:53:33 pmwont hurt. just for uniformity (wasting a byte though) a good compiler will treat it as blank space. the penalty for missing one ; is an annoying compiler error message. i never got one for extra ;, did this from eternity.

author: Bored@Work
author_rank: Super Contributor
timestamp: June 05, 2012, 08:24:08 pm
content: Looks like homework. I think it was posted here once before and got deleted by some moderator. The poster is blasting it to other sites, too. Google "pls check da error in da code".http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=121589http://www.roboticsindia.com/search.php?s=864e4c65f86b9451f9f360c7e3f5d88d&searchid=1345451India. Now, why am I not surprised?

author: newtoneins10
author_rank: Newbie
timestamp: June 07, 2012, 07:05:07 pm
content: I have modified my post and would like to say that I am grossly confused and utterly shocked at the attitude you are showing

author: Bored@Work
author_rank: Super Contributor
timestamp: June 07, 2012, 08:03:21 pm
content: Please not. Last time you blasted your homework code over multiple sites, including eevblog and avrfreaks. This time againhttp://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&p=963433Really, what is wrong in doing your own homework? And what is wrong with at least following the basic suggestions you got last time, like properly formating your code? Why do you come here again if you didn't listen the last time?

author: grumpydoc
author_rank: Super Contributor
timestamp: June 07, 2012, 09:25:05 pm
content: I'm going to second B@W's comment - posting saying "I've written some code and it doesn't work, please fix it" is not really going to impress. Especially when the code is unreadable.If this is homework then you really need to do it yourself.If this is a pet project then a) format your code and b) show some evidence that you've put some effort in and ask specific questions.

author: grumpydoc
author_rank: Super Contributor
timestamp: June 07, 2012, 09:48:17 pm
content: Oh dear, I read the code. I know I shouldn't have but I couldn't resist it.I know nothing about the AVR but I do know C pretty well and if his understanding of the platform matches his understanding of the C language then I'm not sure he'll ever get it going.I wonder if the bits that look reasonable have simply been copied from elsewhere.To the OP - do some ******* work of your own. You code doesn't (or shouldn't anyway) compile - at least you could start there in terms of asking something specific. Look at the error messages, get a decent book on C. I still think K&R is concise and excellent, and with the book try to figure out what the error message is telling you.

author: hlavac
author_rank: Frequent Contributor
timestamp: June 07, 2012, 10:14:27 pm
content: Man, you are seriously fucked up. You can cheat your way thru the whole education, but when you get out with your fake undeserved diploma, who do you think will employ you when you don't know how to do anything? You think other people will do your work for you and not want the money you get for it?

author: Mechatrommer
author_rank: Super Contributor
timestamp: June 07, 2012, 11:41:31 pm
content: worst. this guy like to drop and leave his shit everywhere.

author: newtoneins10
author_rank: Newbie
timestamp: June 08, 2012, 12:16:44 am
content: Quote from: Bored@Work on June 07, 2012, 08:03:21 pmFirstly it is not my homework , its more of a hobby project for me now as I am not participating in the project show of the final year students myself being a myself.Secondly I have posted it here only because I thought I would get the help I did not get from AVR freaks. Anyways, I am not aware of any forum rules which bar someone to post his queries in multiple forums.Thirdly I apologize because I did not go through my last post here. And the message in my inbox said my post was deleted. I do not know if there is any way to retrieve any message that has already been deleted.

author: TerminalJack505
author_rank: Super Contributor
timestamp: June 08, 2012, 01:03:57 am
content: There are probably a lot of people here that are capable of helping you. The problem is that no one wants to try and decipher your code to find the poorly described problem.You might try reformatting the code, describing the problem better and including a schematic.Your code, quite frankly, is completely unreadable. I took one look at it and gave up after about 5 seconds.Here's a Wikipedia article onprogramming style. Here's another onindent style. Pick a style and use it consistently throughout your code. You'll be doing yourself--as well as everyone else that has to read your code--a favor.

author: Mechatrommer
author_rank: Super Contributor
timestamp: June 08, 2012, 08:00:49 am
content: Quote from: newtoneins10 on June 07, 2012, 07:05:07 pmand you dont grossly shocked by the attitude right inside of youyou dont even care to reply in earlier thread.

author: EEVblog
author_rank: Unknown
timestamp: June 08, 2012, 10:30:58 am
content: Quote from: newtoneins10 on June 08, 2012, 12:16:44 amI don't think there is. AFAIK SMF deletes the post from the database and that's it.Dave.

author: GeoffS
author_rank: Unknown
timestamp: June 08, 2012, 10:44:58 am
content: Merged the OP's two threads on the same subject.Original post/thread was deleted as in addition to not saying what the problem is, there was no code included

author: Bored@Work
author_rank: Super Contributor
timestamp: June 08, 2012, 11:18:37 am
content: His second thread wasn't much better, starting with the subject "Line follower code in AVR!!! HELP!!!!!!!!!!!", and he couldn't be arsed to respond in that one.And one should note that the OP has altered the postings others refer too.The third thread also contained completely unformatted code, but finally he could be arsed to respond. However, only to alter his postings and play the "you are so mean" card, now readingQuote from: newtoneins10 on June 07, 2012, 07:05:07 pmWell, dude, do you really want to continue playing that game here? Because, then I would mention that your original third posting also contained a reference to using Proteus. Now, you would be the first Indian student / beginner ever using Proteus with a valid license for 385 Euro. So are you sure you really want to play that "utterly shocked" nonsense card here? Shouldn't we better chalk it up to bad acting from a student who couldn't find some idiots doing his homework with stolen software?

author: Unknown
author_rank: Unknown
timestamp: June 08, 2012, 09:48:39 pm
content: When I scanned the code these lines stuck out to me because the variable "w" was declared as an int and then you're doing a Float condition test with an Int.w=(readADC(i)/1024)*5; //converwsion into ADC voltageif(w>2.5) ch=1; //inserting 1 or 0 in ch arrayBut then again I could be wrong and I've just had far too much coffee today!Btw. really tidy code is a sign of you're taking too long to write it!

author: grumpydoc
author_rank: Super Contributor
timestamp: June 08, 2012, 10:07:18 pm
content: The w > 2.5 test will compile although if w is an int you might as well say "if (w > 2)....."It doesn't really matter though, w is always going to be 0, even if you declare it float.There's a bigger error in the two lines that you quote.

author: krish2487
author_rank: Frequent Contributor
timestamp: June 10, 2012, 06:09:30 pm
content: I agree with all that was said..I am an indian myself, no great guns in electronics, a perennial newbie,but please do not judge ALL indians for the fault of a few.@newtoneins10If you do not have clarity on what you need help for how will others help you even if they can and/or are interested to help you.Web etiquette, my friend, is what you need to learn first.C,programming, microcontrollers, electronics blah blah blah anything and everything you can learn on the net follows later.Your idea of posting the same question on a different forum is wrong in the first place.If you can get the brilliant idea of asking for vague help on any some other forum, dont you think there will be many others(experienced and otherwise) who have hobbies that intersect on many such online communities and forums.they do it as a favour to you. Period.You need their help,you are the newbie here,you request properly, specifically, with clarity.Its like going to a medical doctor and saying "I dont feel fine"He cant help you if you dont have an idea of whats not right with you.It is equally important you respond back.Its basic courtesy and etiquette.Read their comments, they did not comment anything about your code but your presentation style and attitude.Correct it first.Members here help you because of their good nature and you criticize them for YOUR lack of clarity.Show some respect my friend, there are people on forums like this who have accomplished a lot more than you did.If these seem a bit harsh to you,get offline, go to a training institute where you pay half-baked incompetents to learn pretty much nothing.Come back then and tell us that you are shocked.