|
www.elektronik.si Forum o elektrotehniki in računalništvu
|
Poglej prejšnjo temo :: Poglej naslednjo temo |
Avtor |
Sporočilo |
nnovartis19 Neznanec
Pridružen-a: Pet 06 Nov 2015 14:16 Prispevkov: 4 Aktiv.: 0.04 Kraj: maribor
|
Objavljeno: Čet Jan 21, 2016 12:41 pm Naslov sporočila: C++ v Arduinu |
|
|
Imam težavo v C++ za arduino in sicer :
Koda: |
#include <Servo.h>
Servo servo_Vertical; // create servo object to control a servo (Vertical)
Servo servo_Horizonal; // create servo object to control a servo (Horizontal)
int val_Vertical=90; // variable to read the value from the analog pin
int val_Horizonal=90; // variable to read the value from the analog pin
//===============================================================================
const int referenceVolts = 5;
const float R1 = 51000.0; //Resistor #1 (51K)
const float R2 = 100000.0; //Resistor #2 (100K)
const float Ratio = (R1/R2);
const float resistorFactor = ((referenceVolts/Ratio)/1023.0); // eq 0.014471088
const int TolarencePin = A4; // Use Potentiometer for Tolarence Settings
const int solarPin = A5; // +V from Solar Panel connected to analog pin
float v = 0;
//
const int LDR_1_Left = A0; // LDR connected to Analog Pin 0 Left
const int LDR_2_Right = A1; // LDR connected to Analog Pin 1 Right
const int LDR_3_Down = A2; // LDR connected to Analog Pin 2 Down
const int LDR_4_Up = A3; // LDR connected to Analog Pin 3 Up
//
int LDR_1_Left_val = 0;
int LDR_2_Right_val = 0;
int LDR_4_Up_val = 0;
int LDR_3_Down_val = 0;
//
//LEDs 10=UP; 11=Right; 12=Left; 13=Down
int ledPins[] = {10,11,12,13};
//
int dtime = 25;
int Tolarence = 0;
void setup()
{
servo_Vertical.attach(5); // attaches the servo on pin 5 (Elevation) to the servo object
servo_Horizonal.attach(6); // attaches the servo on pin 6 (Horizontal) to the servo object
//Setup LED Pins as OUTPUT
for (int x = 0; x < 4; x++){
pinMode(ledPins[x], OUTPUT);
}
Serial.begin(9600);
}
void loop()
{
reset_leds();
read_potentiometer();
read_voltage();
read_LDRs();
adjust_Servos();
display_status();
delay(dtime); // waits for the servo to move
}
//=========================================================================
// Subs....
//=========================================================================
void reset_leds(){
for (int x = 0; x < 4; x++){
digitalWrite(ledPins[x], LOW);
}
}
//=========================================================================
void read_potentiometer(){
// Use Potentiometer for Tolarence Settings
Tolarence = analogRead(TolarencePin)/4;
}
//=========================================================================
void read_voltage(){
v = analogRead(solarPin);
v *= resistorFactor;
} // end read_voltage
//=========================================================================
void read_LDRs(){
// Set the LDRs in an x config and label the LDRs from 1-4
// \ 4 /
// 1 X 2
// / 3 \
///arduino LDR Pins 10=UP; 11=Right; 12=Left; 13=Down
LDR_1_Left_val = analogRead(LDR_1_Left);
LDR_2_Right_val = analogRead(LDR_2_Right);
LDR_3_Down_val = analogRead(LDR_3_Down);
LDR_4_Up_val = analogRead(LDR_4_Up);
} //end read_LDRs
//=========================================================================
void adjust_Servos(){
int avg_Left_Top = (LDR_1_Left_val + LDR_4_Up_val) / 2; // average value Left_Top
int avg_Left_Botton = (LDR_1_Left_val + LDR_3_Down_val) / 2; // average value Left_Botton
int avg_Right_Top = (LDR_2_Right_val + LDR_4_Up_val) / 2; // average value Right_Top
int avg_Right_Botton = (LDR_2_Right_val + LDR_3_Down_val) / 2; // average value Right_Botton
int dvert = (avg_Left_Top + avg_Right_Top) - (avg_Left_Botton + avg_Right_Botton); // check the diffirence of up and down
int dhoriz = (avg_Left_Top + avg_Left_Botton) - (avg_Right_Top + avg_Right_Botton);// check the diffirence og left and rigt
// Using the tolarence value stop servo seeking
// Check Vertical
if (-1*Tolarence > dvert || dvert > Tolarence){ // check if the diffirence is in the tolerance else change vertical angle
if ((avg_Left_Top + avg_Right_Top) > (avg_Left_Botton + avg_Right_Botton)){
//adjust Vertical Servo Down
digitalWrite(ledPins[3], HIGH); //Blink the Bottom LED
val_Vertical = ++val_Vertical;
if (val_Vertical > 179){
val_Vertical = 179;
}
}
else if ((avg_Left_Top + avg_Right_Top) < (avg_Left_Botton + avg_Right_Botton)){
//adjust Vertical Servo Up
val_Vertical= --val_Vertical;
digitalWrite(ledPins[0], HIGH); //Blink the Top LED
if (val_Vertical < 1){
val_Vertical = 1;
}
}
servo_Vertical.write(val_Vertical);
}
//Check Horizonal
if (-1*Tolarence > dhoriz || dhoriz > Tolarence){ // check if the diffirence is in the tolerance else change horizontal angle
if ((avg_Left_Top + avg_Left_Botton) > (avg_Right_Top + avg_Right_Botton)){
//adjust Horizonal Servo Left
val_Horizonal = --val_Horizonal;
digitalWrite(ledPins[1], HIGH); //Blink the Left LED
if (val_Horizonal < 1){
val_Horizonal = 1;
}
}
else if ((avg_Left_Top + avg_Left_Botton) < (avg_Right_Top + avg_Right_Botton)){
//adjust Horizonal Servo Right
val_Horizonal = ++val_Horizonal;
digitalWrite(ledPins[2], HIGH); //Blink the Right LED
if (val_Horizonal > 179){
val_Horizonal = 179;
}
}
else if ((avg_Left_Top + avg_Left_Botton) == (avg_Right_Top + avg_Right_Botton)){
// nothing
}
servo_Horizonal.write(val_Horizonal);
}
} // end adjust servos
//========================================================================
void display_status(){
Serial.print("Solar Panel Volts: ");
Serial.print(v);
Serial.print(" Servo Horizontal: ");
Serial.print(val_Horizonal);
Serial.print(" Servo Vertical: ");
Serial.print(val_Vertical);
Serial.print(" LDR_1_L: ");
Serial.print(LDR_1_Left_val);
Serial.print(" LDR_2_R: ");
Serial.print(LDR_2_Right_val);
Serial.print(" LDR_3_D: ");
Serial.print(LDR_3_Down_val);
Serial.print(" LDR_4_U: ");
Serial.print(LDR_4_Up_val);
Serial.print(" Tolarence: ");
Serial.println(Tolarence);
} //end display status
|
Vendar ko program zaženem mi začne pri float V=0; javljati napako "expected unqalifield_id befor "float" " in potem pri vseh vrsticah naprej enako.
Mi lahko kdo pomaga kje sem naredil napako?
|
|
Nazaj na vrh |
|
|
Jakey Član
Pridružen-a: Sre 11 Jul 2012 0:38 Prispevkov: 651 Aktiv.: 4.53 Kraj: Ravne/LJ/Kobarid
|
Objavljeno: Čet Jan 21, 2016 2:12 pm Naslov sporočila: Re: C++ v Arduinu |
|
|
Kjer imaš "float v = 0;" poskusi dati raje "double v = 0;"
_________________ Predlagam, da kar napišem, si čimprej skopirat, saj bodo šerifi tega foruma verjetno pobrisali s predpostavko, da ne prispeva (dosti) k vrednosti foruma. |
|
Nazaj na vrh |
|
|
Obelix Član
Pridružen-a: Pon 19 Maj 2008 13:59 Prispevkov: 1862 Aktiv.: 9.60 Kraj: Maribor
|
Objavljeno: Čet Jan 21, 2016 5:06 pm Naslov sporočila: |
|
|
Poskusi s "float v = 0.0"
|
|
Nazaj na vrh |
|
|
nnovartis19 Neznanec
Pridružen-a: Pet 06 Nov 2015 14:16 Prispevkov: 4 Aktiv.: 0.04 Kraj: maribor
|
Objavljeno: Sob Jan 23, 2016 3:37 pm Naslov sporočila: |
|
|
Sem zgornjo napako resil
Sedaj pa mi javlja pri :
void reset_leds(){
for (int x = 0; x < 4; x++){
digitalWrite(ledPins[x], LOW);
javlja napako ledPins was not declared in this scope
Kako resiti to ?
Se zahvlajujem v naprej za nasvet
|
|
Nazaj na vrh |
|
|
nnovartis19 Neznanec
Pridružen-a: Pet 06 Nov 2015 14:16 Prispevkov: 4 Aktiv.: 0.04 Kraj: maribor
|
Objavljeno: Sob Jan 23, 2016 3:39 pm Naslov sporočila: |
|
|
oziroma to so napake ki mi jih še javlja:
Koda: |
sketch_solarni_sledilnik.ino: In function 'void setup()':
sketch_solarni_sledilnik.ino:58:13: error: 'ledPins' was not declared in this scope
sketch_solarni_sledilnik.ino:60:5: error: 'serial' was not declared in this scope
sketch_solarni_sledilnik.ino: In function 'void loop()':
sketch_solarni_sledilnik.ino:67:16: error: 'read_LDR' was not declared in this scope
sketch_solarni_sledilnik.ino: In function 'void reset_leds()':
sketch_solarni_sledilnik.ino:81:24: error: 'ledPins' was not declared in this scope
sketch_solarni_sledilnik.ino: In function 'void read_potentiometer()':
sketch_solarni_sledilnik.ino:97:23: error: expected ';' before numeric constant
sketch_solarni_sledilnik.ino: In function 'void read_LDRs()':
sketch_solarni_sledilnik.ino:118:17: error: expected ';' before 'LDR_2_Right_val'
sketch_solarni_sledilnik.ino:119:17: error: 'LDR_3_Down_val' was not declared in this scope
sketch_solarni_sledilnik.ino:119:45: error: 'LDR_3_Down' was not declared in this scope
sketch_solarni_sledilnik.ino:120:17: error: 'LDR_4_Up_val' was not declared in this scope
sketch_solarni_sledilnik.ino:120:43: error: 'LDR_4_Up' was not declared in this scope
sketch_solarni_sledilnik.ino: In function 'void adjust_Servos()':
sketch_solarni_sledilnik.ino:127:55: error: 'LDR_4_Up_val' was not declared in this scope
sketch_solarni_sledilnik.ino:132:102: error: 'kontrola' was not declared in this scope
sketch_solarni_sledilnik.ino:132:111: error: expected ';' before 'razlike'
sketch_solarni_sledilnik.ino:134:112: error: expected ';' before 'razlike'
sketch_solarni_sledilnik.ino:199:29: error: expected '}' at end of input
'ledPins' was not declared in this scope |
|
|
Nazaj na vrh |
|
|
Jaka57 Moderator
Pridružen-a: Ned 12 Dec 2004 21:47 Prispevkov: 5773 Aktiv.: 25.89 Kraj: Grosuplje
|
Objavljeno: Sob Jan 23, 2016 4:03 pm Naslov sporočila: |
|
|
Hm, nnovartis19, kot lahko vidiš na spodnji sliki, mi prevede gornji sketch iz prvega prispevka, brez napak v Arduinu IDE v.1.6.5.
Tudi za deklaracijo float v=0 ne javi napake.
Torej, ker nimamo steklene krogle ne moremo vedeti, kateri Arduino IDE uporabljaš za prevajanje, kakor tudi ne katerega Arduina misliš uporabiti za tole.
Daj, prosim, malo bolj se potrudi pri objavljanju prispevkov, ne da je vse nametano, kakor pač pride s copy/paste!
Opis: |
|
Velikost datoteke: |
50.84 KB |
Pogledana: |
6 krat |
|
_________________ Lp, Jaka |
|
Nazaj na vrh |
|
|
Branez Moderator
Pridružen-a: Pon 14 Apr 2003 7:21 Prispevkov: 7772 Aktiv.: 34.85 Kraj: Koprivnica HR
|
|
Nazaj na vrh |
|
|
Jakey Član
Pridružen-a: Sre 11 Jul 2012 0:38 Prispevkov: 651 Aktiv.: 4.53 Kraj: Ravne/LJ/Kobarid
|
Objavljeno: Sob Jan 23, 2016 9:08 pm Naslov sporočila: |
|
|
nnovartis19 je napisal/a: |
Sem zgornjo napako resil
Sedaj pa mi javlja pri :
void reset_leds(){
for (int x = 0; x < 4; x++){
digitalWrite(ledPins[x], LOW);
javlja napako ledPins was not declared in this scope
Kako resiti to ?
Se zahvlajujem v naprej za nasvet |
popravi na:
digitalWrite(13, HIGH);
void reset_leds(){
for (int x = 0; x < 4; x++){
digitalWrite(x, LOW);
indeksi se ne pišejo v oglatih oklepajih v tem primeru
_________________ Predlagam, da kar napišem, si čimprej skopirat, saj bodo šerifi tega foruma verjetno pobrisali s predpostavko, da ne prispeva (dosti) k vrednosti foruma. |
|
Nazaj na vrh |
|
|
|
|
Ne, ne moreš dodajati novih tem v tem forumu Ne, ne moreš odgovarjati na teme v tem forumu Ne, ne moreš urejati svojih prispevkov v tem forumu Ne, ne moreš brisati svojih prispevkov v tem forumu Ne ne moreš glasovati v anketi v tem forumu Ne, ne moreš pripeti datotek v tem forumu Ne, ne moreš povleči datotek v tem forumu
|
Uptime: 76 dni
Powered by phpBB © 2001, 2005 phpBB Group
|