LusoRobótica - Robótica em Português
Sistemas específicos => Arduino / AVR => Tópico iniciado por: thevasco92 em 28 de Maio de 2010, 23:02
-
Hehe estou a fazer um pequeno projecto escolar mas isto não está a correr nada bem
Estou a tentar fazer um robô bem simples apenas um sensor de distancia sharp gp2d12 e um servo, consegui pôr o sensor a trabalhar e o servo também mas quando vou a por uma função if dá tudo erro
alguém me pode ajudar
Para o servo tenho
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
/*
read_gp2d12_range
Function that reads a value from GP2D12 infrared distance sensor and returns a value in centimeters.
This sensor should be used with a refresh rate of 36ms or greater.
Javier Valencia 2008
float read_gp2d12_range(byte pin)
It can return -1 if something gone wrong.
*/
float read_gp2d12_range(byte pin) {
int tmp;
tmp = analogRead(pin);
if (tmp < 3)
return -1; // invalid value
return (6787.0 /((float)tmp - 3.0)) - 4.0;
}
alguém pode me ajudar a fazer o código tipo quando o valor detectado pelo sensor for entre X<Y ele acciona o servo e vira
Hehe TY
-
if (exemplo > 50)
{
faz isto;
}
Sem as chavetas correctamente colocadas não dá.
-
pelo que percebi era algo assim mas isto não compila
int sharp_IR_reading=0
#include <Servo.h>
Servo myservo;
int pos = 0;
void setup()
{
myservo.attach(9);
}
void loop()
{
if(sharp_IR_reading > 20)
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
}
dá este erro
"C:\Users\PCPCPC\Desktop\download\arduino-0018\libraries\Servo/Servo.h:84: error: expected ',' or ';' before 'typedef'
C:\Users\PCPCPC\Desktop\download\arduino-0018\libraries\Servo/Servo.h:84: error: expected constructor, destructor, or type conversion before ';' token
"
não percebo o k é
-
Boas
A 1ª linha de código acho que não pode ser onde está, experimenta ao pé do outro int e tens de por um ";" no final.
Experimenta assim
#include <Servo.h>
Servo myservo;
int pos = 0;
int sharp_IR_reading=0;//aqui
void setup()
{
myservo.attach(9);
}
void loop()
{
if(sharp_IR_reading > 20)
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
}
-
A primeira linha pode estar onde está, basta meteres um ; no final dessa linha
;)
Edit: Pode estar mas não deve xD por isso é normal que Arknum ache que não pode estar...
Deve ser pela seguinte ordem:
Include
Define
Global variables
Function prototypes
setup
loop
Functions
Se bem que deve-se evitar as variáveis globais... xD mas esqueçam lá isso que é só complicar fiquem só com a ordem das coisas =)
-
hehe obrigado a todos
pronto essa parte já passei agora só tenho um problema é eu já consigo fazer o servo rodar mas não consigo fazer o servo voltar á posição 0 se alguem conseguir dizer onde está o erro agradecia
#include <Servo.h>
Servo myservo;
int pos = 0;
byte sensorPin=0;
int latestVal=0;
int maxVal=0;
int minVal=1024;
void setup(){
myservo.attach(9);
Serial.begin(9600);
Serial.println("in setup");}
void loop(){
//Serial.println("In loop");
latestVal = analogRead(sensorPin);
if(latestVal < minVal) {
minVal = latestVal;
}
Serial.print("Max: ");
Serial.print(200);
Serial.print(" Min: ");
Serial.print(minVal);
Serial.print(" Latest: ");
Serial.println(latestVal);
delay(1000);
if(latestVal > 200)
{
for(pos = 0; pos < 180; pos += 1); // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1);// goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
else
{
for(pos > 0; pos>=1; pos-=1);// goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}}
tou farto de tentar e isto não dá
hahahaha de certo que é uma coisa estupida mas não encntro
-
1º Tens ; a frente dos três For o que não pode estar, apagas isso.
2º Este For (o 2º)
for(pos = 180; pos>=1; pos-=1)
não devia ser
for(pos = 180; pos>=0; pos-=1)
3º Este For ( o 3º)
for(pos > 0; pos>=1; pos-=1)
Não devia ser igual ao 2? É que não se faz pos > 0 naquele sítio do for
Sinceramente fiquei um bocado confuso com o que queres fazer lool :P
-
wow como é que eu demorei tanto nisto isto é msm fácil agora ja percebi bem fixe estes códigos
Como dizem:" o material tem sempre razão"- e é mesmo verdade
aqui fica o codigo finalizado
#include <Servo.h>
Servo myservo;
int pos = 0;
byte sensorPin=0;
int latestVal=0;
int maxVal=0;
int minVal=1024;
void setup(){
myservo.attach(9);
Serial.begin(9600);
Serial.println("in setup");}
void loop(){
//Serial.println("In loop");
latestVal = analogRead(sensorPin);
if(latestVal < minVal) {
minVal = latestVal;
}
Serial.print("Max: ");
Serial.print(200);
Serial.print(" Min: ");
Serial.print(minVal);
Serial.print(" Latest: ");
Serial.println(latestVal);
delay(1000);
if(latestVal > 200)
{
for(pos = 90; pos < 180; pos += 1); // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
else
{
for(pos = 180; pos>=90; pos-=1);// goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}}
~
obrigado a todos que me ajudaram