Radíme si navzájem

Datum
Vložil
Titulek

Začátečník

Ahoj potřeboval bych poradit. Jak upravit tento program tak aby mi teplotní čidlo řídilo i servo.
Děkuji.

#include <OneWire.h>
#include <DallasTemperature.h>
#include <Servo.h>
Servo servo; // create servo object to control a servo
// twelve servo objects can be created on most
int pinA = 3; // pin 3 and 10 are PWM output controled by Timer2
int pinB = 9; // connect pinA/B to H-Bridg
int pos = 0; // variable to store the servo position

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 10

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

// Compute linearization constant
// Fan1
float Fan1Min = 0; // PWM is between 0 and 255 for that fan (8 bit pwm)
float Fan1Max = 255;
float Temp1Min = 27.0; // relinearization will be done between 20 Celcius (pwm=Fan1Min)
float Temp1Max = 28.0; // to 27 celcius (pwm=Fan1Max)
float K1 = (Fan1Max - Fan1Min) / (Temp1Max - Temp1Min);

void setup(void) {
// See wxxxxxxxxxxxxxxx
//__________________________________TIMER2_for_Motor_PWM_________________________________
// set TIMER2 for PWM 31 kHz
//
// clear all prescaler bits in TCCR2B = the last 3 Bits
// leave other bits as set by arduino init() in wiring.c
byte mask = B11111000;
TCCR2B &= mask; // TCCR2B is now xxxxx000
//
// set CS22:20 in TCCR2B see p 156 of datasheet
TCCR2B |= (0 << CS22) | (0 << CS21) | (1 << CS20); // same as TCCR2B |= B00000001; TCCR2B is now xxxxx001
//__pinmode

// start serial port
Serial.begin(9600);
Serial.println("Fanduino: DS1820 based temperature fan controller");
Serial.println("Fanduino: DS1820 based temperature fan servo");

// Start up the library
sensors.begin();
{
servo.attach(9); // attaches the servo pin 9 to the servo object
}

}

void loop() {
// call sensors.requestTemperatures() to issue a global temperature request
sensors.requestTemperatures(); // Send the command to get temperatures

// read sensors values
float T1 = sensors.getTempCByIndex(0);

// skip conversion error
if ( ( T1 > -127.00 ));
{
// print them on serial
Serial.print("Temp1: ");
Serial.print(T1);


// normalize values between TempXMin and TempXMax
if (T1 < Temp1Min)
T1 = Temp1Min;
if (T1 > Temp1Max)
T1 = Temp1Max;


// compute pwm value
// when temp will be from TempXMin celcius to TempXMax, pwm will be from FanXMin to FanXMax
float Pwm1 = ((T1 - Temp1Min) * K1) + Fan1Min;


// print pwm values on serial for debugging
Serial.print (" PWM1: ");
Serial.print ( Pwm1 );

// Assign pwm
digitalWrite(pinA, Pwm1 );
digitalWrite(pinB, Pwm1 );
}
{
servo.write(180);
delay(3000);

servo.write(90);
delay(3000);
}
}

Zpět na diskuzi

Vyhledávání

arduino8.cz © 2015 Všechna práva vyhrazena.