Servo And sharp 2y0a21

nevil010

Member
Code:
symbol mot1p = b.7
symbol mot2p = b.5
symbol mot1n = b.6
symbol mot2n = b.4
symbol servop = b.0
symbol sharppin = c.0
symbol dist = b4 
symbol lastturn = b1 
lastturn = 0
 main:
 gosub distance
 if dist > 60 and dist < 105 then
          gosub  driveforward
 else 
if lastturn = 0 then 
gosub driveleft
else
gosub serv
gosub drivert 
 endif
 endif
 goto main  
 
 
 
 
 distance:
readadc sharppin, dist
return

serv:
servo b.0,75
wait 1
servo b.0,225
wait 1
servo b.0,150
pause 100
return

driveforward:
low mot2p high mot2n  low mot1n high mot1p 
return 

drivebk:
high mot1n low mot1p high mot2n low mot2p
return 

driveleft:
low mot2p high mot2n  low mot1p high mot1n 
lastturn = 1
return 

drivert:
high mot2p low mot2n  high mot1p low mot1n 
lastturn = 0
return
servo = hs 55
sensor = sharp 2y0a21

I connected and programmed with the above code...the robo just turns right always..and it is not running forward..the sensor works fine [i tested it by debugging]...and when i tried unplugging the servo from power..then it works the way it is supposed to be..
so my doubts are
1)is it due to the noise from the servo which cause deflection in it?
2)what should i do to get rid of it?
3)is it harmful for my chip?
 

Attachments

geoff07

Senior Member
shouldn't you have colons between multiple commands on one line?
 
Last edited by a moderator:

Technical

Technical Support
Staff member
If it works fine without the servo attached then it probably is a noise issue, put a big (e.g. 220uF) and a little (e.g. 100nF) capacitor across the power rails (ie where the the servo red and black wires connect to the PCB).
 

erco

Senior Member
Sharp recommends a filter cap on the sensor itself. From http://www.pololu.com/catalog/product/136

Note: The GP2Y0A21YK0F model is a lead-free (RoHS-compliant) version of the Sharp GP2Y0A21 Distance Sensor. The manufacturer recommends you insert a bypass capacitor of 10 uF or more between Vcc and GND near this sensor to stabilize your power supply line.
 

geoff07

Senior Member
Well you learn something everyday.

The manual states (current version 7.9 vol2 p5):

Newline
Commands are normally placed on separate lines. However if desired the colon
:)) character can be use to separate multiple commands on a single line e.g.
if pin1 = 1 then : high 1 : else : low 1 : endif


But in fact it is not (always?) necessary as the example in this post shows, which passes the syntax checker and the resulting code is the same length with or without colons.
 

Technical

Technical Support
Staff member
The compiler will always do it's best to work out what a user's code means. But if a single line involves multiple commands more complex than this very simple example the compiler can get confused and you can get into all sorts of trouble! So it is far better to stick to the recommended rules - single line per command or separate via : on same line.
 

Paix

Senior Member
Sticking with the rules aides clarity of intent and saves on doubt or confusion. If both Geoff07 and I didn't immediately recognise that the absence of colons was correct, then what would a newbie necessarily make of it?

While a lot may be acceptable, we should, where possible, be encouraging good practice when it comes to code layout. it makes reading and therefore understanding a lot easier in most cases.

Insider information isn't necessarily a standard, but a trick to make things work in a non-standard way.

Today's tricks have a nasty habit of coming back as tomorrow's snags.
 

nevil010

Member
If it works fine without the servo attached then it probably is a noise issue, put a big (e.g. 220uF) and a little (e.g. 100nF) capacitor across the power rails (ie where the the servo red and black wires connect to the PCB).
i Have one capacitor,in it is written "16v 220uf" does the voltage matters
?
 

Technical

Technical Support
Staff member
16V max. is fine, because your supply is under this at 4.5V.
Just put it across the positive and 0V of the sensor wires, making sure the capacitor is connected the correct way around.
 

JimPerry

Senior Member
16V is fine upto about 9V - 16V is the absolute maximum - polarity is also critical (wrong way and it generally explodes :rolleyes:)
 

nevil010

Member
16V max. is fine, because your supply is under this at 4.5V.
Just put it across the positive and 0V of the sensor wires, making sure the capacitor is connected the correct way around.
ya connected as u said..but the sensor is malfunctioning and it is getting hot :B...the adc reads 225 always...but when i removed it works fine..
 

jinx

Senior Member
you got your sesor working yet?
getting hot is a idication of a short double ,double check your wiring. and post a pic if you can
 

nevil010

Member
ya i was due to the short and i have added a 440uf capacitor..i debugged and i found that it now works fine..but some time the adc variable number suddenly increases to between 115 and 130 even if it is not moving or anything is infront of it..it will be that number for 1 second and goes back to normal..!..
 

westaust55

Moderator
shouldn't you have colons between multiple commands on one line?
Use of colons between the commands would be nice from a presentation/readability persecutive however, Rev Ed's Programming Editor does permit an (undocumented) space to be used as the command separator for some backward compatibility.

From the manual:
Newline
Commands are normally placed on separate lines. However if desired the colon :)) character can be use to separate multiple commands on a single line
 
Top