Loading
Offline
Зарегистрирован: 25.01.2016
С Arduino работаю первый раз. До этого был опыт только на Pascal
Вообщем, пытаюсь просто заставить сервак крутиться вперед-назад. Нашел небольшой гайд (http://robocraft.ru/files/books/arduino_notebook_rus_v1-1.pdf) (Этот кусок кода есть в этой книжке на 38 странице, его я и переписал). Вот код:
#include <Servo.h> int servoPin = 3; int myAngle; int PulseWidth; void setup() { // put your setup code here, to run once: pinmode(ServoPin, OUTPUT); } void servoPulse(int ServoPin, int myAngle) { pulseWidth = (myAngle * 10) +600; digitalWrite(ServoPin,HIGH); delayMicroseconds(pulseWidth); digitalWrite(ServoPin, LOW); } void loop() { // put your main code here, to run repeatedly: for (myAngle = 10; myAngle<=170; myAngle++) { servoPulse(ServoPin,myAngle); delay(20); } for (myAngle=170; myAngle>=10; myAngle--) { servoPulse(ServoPin, myAngle); delay(20); } }
При запуске ругается вообще на всю программу, чуть ли не на каждую строку.
C:Users1DocumentsArduinosketch_jan25csketch_jan25c.ino: In function 'void setup()': sketch_jan25c:8: error: 'ServoPin' was not declared in this scope pinmode(ServoPin, OUTPUT); ^ sketch_jan25c:8: error: 'pinmode' was not declared in this scope pinmode(ServoPin, OUTPUT); ^ C:Users1DocumentsArduinosketch_jan25csketch_jan25c.ino: In function 'void servoPulse(int, int)': sketch_jan25c:13: error: 'pulseWidth' was not declared in this scope pulseWidth = (myAngle * 10) +600; ^ C:Users1DocumentsArduinosketch_jan25csketch_jan25c.ino: In function 'void loop()': sketch_jan25c:23: error: 'ServoPin' was not declared in this scope servoPulse(ServoPin,myAngle); ^ sketch_jan25c:26: error: 'myangle' was not declared in this scope for (myAngle=170; myangle>=10; myAngle--) ^ sketch_jan25c:28: error: 'ServoPin' was not declared in this scope servoPulse(ServoPin, myAngle); ^ exit status 1 'ServoPin' was not declared in this scope Неверная библиотека найдена в C:Users1DocumentsArduinolibrariesBlink: C:Users1DocumentsArduinolibrariesBlink Неверная библиотека найдена в C:Users1DocumentsArduinolibrariesBlink: C:Users1DocumentsArduinolibrariesBlink
Как я понял, ему не хватает каких-то библиотек? Это моя единственная догадка. Вообщем, помогите нубу)))
I’m trying to follow this this tutorial to program an ATtiny85 micro-controller using my Arduino Uno r3 as the ISP.
I am up to the final step on the tutorial, but get an error when attempting to compile my sketch:
'OUTPUT' was not declared in this scope
I have tried multiple «Blink» scripts, but none have seemed to work.
My full error log:
Blink.ino:10:21: error: Arduino.h: No such file or directory
Blink.ino: In function 'void setup()':
Blink:15: error: 'OUTPUT' was not declared in this scope
Blink:15: error: 'pinMode' was not declared in this scope
Blink.ino: In function 'void loop()':
Blink:20: error: 'HIGH' was not declared in this scope
Blink:20: error: 'digitalWrite' was not declared in this scope
Blink:21: error: 'delay' was not declared in this scope
Blink:22: error: 'LOW' was not declared in this scope
I have also tried including Arduino.h, no help, and changing «OUTPUT» to «0x01», as it is defined to said value in Arduino.h, but instead got this error:
error: 'pinMode' was not declared in this scope
Any ideas as to what is happening?
Loading
-
Так все компилируется:
#define DISPLAY#ifdef DISPLAY
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x20,16,2);
#else
Serial.begin(9600);
#endifvoid setup(){
pinMode(13, OUTPUT);
}void loop(){
}Так не компилируется (отличие только в первой строке):
//#define DISPLAY#ifdef DISPLAY
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x20,16,2);
#else
Serial.begin(9600);
#endifvoid setup(){
pinMode(13, OUTPUT);
}void loop(){
}Почему?
-
Если я верно понял то тут проблема вот в чем.
Во втором случае код для компилятора после препроцессора получается идентичным этому:Serial.begin(9600);void setup(){
pinMode(13, OUTPUT);
}void loop(){
}На данном этапе объект Serial еще не существует, отсюда и беда. Я не знаю точно в какой момент именно создается объект Serial, но по всей видимости где то в промежутке межу данными объявленными пользователем в глобальной области видимости и вызовом функции setup.
Решить эту проблему можно как вариант так:
#define DISPLAY#ifdef DISPLAY
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x20,16,2);
#endifvoid setup(){
#ifndef DISPLAY
Serial.begin(9600);
#endif
pinMode(13, OUTPUT);
}void loop(){
} -
Потому что знак // этот означает, что после этого знака идёт комментарий, а комментарий проверять не надо.
-
Суть не в Serial… Так тоже не работает:
//#define DISPLAY#ifdef DISPLAY
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x20,16,2);
#else
#include <Wire.h>
#endifvoid setup(){
pinMode(13, OUTPUT);
}void loop(){
} -
Это может быть связано с багом Arduino IDE, который возникает в моиент преобразования скетча в програииу на си для скармливания компилятору.
Если это оно, то перед первой директивой условной компиляции надо вставить хитрую комбинацию.
Где-то на форуме уже упоминалось. -
если вы про эту магию:
// BOF preprocessor bug prevent — insert me on top of your arduino-code
#if 1
__asm volatile («nop»);
#endifто не срабатывает, хотя некоторые уже сталкивались с аналогичной проблемой.
Даже если максимально упростить, все равно ругается:
//#define DISPLAY#ifdef DISPLAY
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x20,16,2);
#endifvoid setup(){
pinMode(13, OUTPUT);
}void loop(){
}v2_0.ino: In function ‘void setup()’:
v2_0:14: error: ‘OUTPUT’ was not declared in this scope
v2_0:14: error: ‘pinMode’ was not declared in this scope -
Поробовал на Arduino IDE 1.0.6 и 1.6.1.
Только
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x20,16,2);
Заменил на аналогичное для простого LiquidCrystal.Без «магии» выдает именно такие ошибки, с ней ошибка пропадает.
«Магию» применять сразу после //#define DISPLAY -
у меня 1.0.5. Попробую 1.0.6 скачать.
-
Добавьте первой строчкой в скетч
#include <Arduino.h>
и не используйте макрос DISPLAY (выберите другое имя, это уже используется внутри Wiring) -
В данном случае ошибки в IDE нет, просто при склеивании скетча с main.cpp получается неверная структура кода, #include и код, использующий библиотеки, получаются перемешаны.