Форум

Добро пожаловать, гость 

Показать / спрятать

Добро пожаловать, гость! Для участия в форуме требуется регистрация.

Страниц: [1]
Автор Тема: трекер на ардуино с кодом
pelingator-
x
Администратор
Сообщения: 1625
Permalink
Сообщение трекер на ардуино с кодом
 June 14, 2014, 19:12
Цитата

тема взята с сайта для дальнейшего осмысления : http://duino4projects.com/arduino-2-axis-servo-solar-tracker/

код

#define TILTL 2
#define TILTH 3
#define BOTTOM 2
#define TOPLEFT 0
#define TOPRIGHT 1
#include <Servo.h>
#include "math.h"

Servo hservo;
Servo vservo;
int tlsense;
int trsense;
int bsense;
int tavg;
int diff;
int spd;
int divisor;
int sensitivity;
int tiltl;
int tilth;

void setup () {
vservo.attach(9); // attaches the servo on pin 9 to the servo object
hservo.attach(10); // attaches the servo on pin 10 to the servo object
divisor = 10; // this controls the speed of the servo. lower number = higher speed
sensitivity = 5; // this controls the sensitivity of the tracker. lower number = higher sensitivity. if your tracker is constantly jittering back and forth increase the number
Serial.begin(19200); // open serial com
Serial.print("SolarTracker ready!");
pinMode(BOTTOM, INPUT); // set the inputs
pinMode(TOPLEFT, INPUT);
pinMode(TOPRIGHT, INPUT);
pinMode(TILTL, INPUT);
pinMode(TILTH, INPUT);
}

void loop () {

tiltl = digitalRead(TILTL); // read the tilt sensor
tilth = digitalRead(TILTH);
tlsense = analogRead(TOPLEFT); // read the light sensors
trsense = analogRead(TOPRIGHT);
bsense = analogRead(BOTTOM);
//bsense = bsense * 1.05; // I had to adjust the value of this sensor to make it more accurate. you might have to do the same but start by leaving it alone
tavg = (tlsense + trsense)/2; // get an average value for the top 2 sensors
diff = abs(tavg - bsense); // this judges how far the tracker must turn
spd = diff/divisor; // and adjusts the speed of the reaction accordingly
spd = max(spd, 1); // sets the minimum speed to 1
Serial.print("\nTOP: "); Serial.print(tavg, DEC); // print the sensor values to the serial com
Serial.print("\tBOTTOM:"); Serial.print(bsense, DEC);
Serial.print("\tLEFT:"); Serial.print(tlsense, DEC);
Serial.print("\tRIGHT:"); Serial.print(trsense, DEC);

if((tavg < bsense) && (diff > sensitivity) && (tiltl == LOW) && (tilth == LOW)){ // if the average value of the top sensors is smaller (more light) than the bottom sensor and the tilt sensor is in the correct range
vservo.write(90 - spd); // send servo command to turn upward plus add speed
Serial.print("\tState: "); Serial.print("UP!");
}else if((tavg < bsense) && (diff > sensitivity) && (tiltl == HIGH) && (tilth == LOW)){ // if the average value of the top sensors is smaller (more light) than the bottom sensor and the tilt sensor is in the correct range
vservo.write(90 - spd); // send servo command to turn upward plus add speed
Serial.print("\tState: "); Serial.print("UP!");
}else if((tavg > bsense) && (diff > sensitivity) && (tiltl == HIGH) && (tilth == LOW)){ // if the value of the bottom sensor is smaller (more light) than the average value of the top sensors and the tilt sensor is in the correct range
vservo.write(90 + spd); // send servo command to turn downward plus add speed
Serial.print("\tState: "); Serial.print("DOWN!");
}else if((tavg > bsense) && (diff > sensitivity) && (tiltl == LOW) && (tilth == HIGH)){ // if the value of the bottom sensor is smaller (more light) than the average value of the top sensors and the tilt sensor is in the correct range
vservo.write(90 + spd); // send servo command to turn downward plus add speed
Serial.print("\tState: "); Serial.print("DOWN!");
}else{ // for every other instance
vservo.write(90); // stop the y-axis motor
Serial.print("\tState: "); Serial.print("STOP!");
}

tlsense = analogRead(TOPLEFT); // read the top 2 sensors again because they have probably changed
trsense = analogRead(TOPRIGHT);
//trsense = trsense * 1.03; // again I had to adjust the value of one sensor to make the tracker more accurate
diff = abs(tlsense - trsense); // reset the diff variable for the new values
spd = diff/divisor; // and generate a speed accordingly
spd = max(spd, 1); // set the minimum speed to 1

if((tlsense < trsense) && (diff > sensitivity)){ // if the top left sensor value is smaller (more light) than the top right sensor
hservo.write(90 + spd); // send servo command to turn left
Serial.print("\tState: "); Serial.print("LEFT!");
}else if((tlsense > trsense) && (diff > sensitivity)){ // if the top left sensor value is larger (less light) than the top right sensor
hservo.write(90 - spd); // send servo command to turn right
Serial.print("\tState: "); Serial.print("RIGHT!");
}else{ // for every other instance
hservo.write(90); // stop the x-axis motor
Serial.print("\tState: "); Serial.print("STOP!");
}

delay(10); // delay 10ms
}
Image
Image

Страниц: [1]


Mingle Forum by cartpauj
Версия: 1.0.34 ; Страница загружена за 0.005 секунд.
Один комментарий к “Форум”
  1. Тут публиковать комментарии нельзя! Отвечать на них не буду. Только удалять! Для того чтобы оставить комментарий воспользуйтесь рубрикой выше. И там создайте тему. Проще некуда. Таким образом ваш вопрос, и ответ может помочь другим людям. Которые могли столкнутся с подобной проблемой.

Добавить комментарий