Re: Adruino Perikelen
Geplaatst: 31 aug 2016, 17:04
Code: Selecteer alles
/*-----( Library's )-----*/
#include <U8glib.h>
#include <OneWire.h>
#include <Wire.h>
#include <DallasTemperature.h>
/*-----( Library's )-----*/
U8GLIB_PCD8544 u8g(13, 11, 10, 9, 8); // Constructor For Connection SPI Nokia 5110 Display: CLK = 13, DIN = 11, CE = 10, DC = 9, RST = 8
int fan = 6; // the pin where fan is
int led = 7; // led pin
int tempMin = 35; // the temperature to start the fan
int tempMax = 55; // the maximum temperature when fan is at 100%
int fanSpeed;
int fanLCD;
int ticks = 0, Speed = 0;
int hallsensor = 2;
typedef struct{
char fanntype;
unsigned int fanndiv; }
fannspec;
//Definitions of the fans
//This is the varible used to select the fan and it's divider,
//set 1 for unipole hall effect sensor
//and 2 for bipole hall effect sensor
fannspec fannspace[3]={{0,1},{1,2},{2,8}}; char fann = 1;
#define VT_PIN A0 // connect VT
#define AT_PIN A1 // connect AT
#define ONE_WIRE_BUS 4 // Data wire is plugged into pin 4 on the Arduino
#define T_PRECISION 12 // Set variable T_PRECISION to 12 (12 bit resolution on the 18D20)
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.
// Assign the addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html
DeviceAddress insideThermometer = { 0x28, 0xFF, 0xFC, 0x7F, 0xC1, 0x15, 0x01, 0x48 };
void pickrpm ()
//This is the function that the interupt calls
{ ticks++; }
void setup(void)
{
sensors.begin(); // Start up the library
sensors.setResolution(insideThermometer, T_PRECISION); // Resolution
pinMode(fan, OUTPUT);
pinMode(led, OUTPUT);
pinMode(hallsensor, INPUT);
attachInterrupt(0, pickrpm, RISING);
digitalWrite(hallsensor, HIGH);
}
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00) {
u8g.print("ERROR");
}
else {
u8g.print(tempC);
}
if(tempC < tempMin) { // if temp is lower than minimum temp
fanSpeed = 0; // fan is not spinning
digitalWrite(fan, LOW);
}
if((tempC >= tempMin) && (tempC <= tempMax)) { // if temperature is higher than minimum temp
fanSpeed = map(tempC, tempMin, tempMax, 58, 255); // the actual speed of fan
fanLCD = map(tempC, tempMin, tempMax, 0, 100); // speed of fan to display on LCD
analogWrite(fan, fanSpeed); // spin the fan at the fanSpeed speed
}
if(tempC > tempMax) { // if temp is higher than tempMax
digitalWrite(led, HIGH); // turn on led
}
else { // else turn of led
digitalWrite(led, LOW);
}
}
void loop(void)
{
int vt_read = analogRead(VT_PIN);
int at_read = analogRead(AT_PIN);
float voltage = vt_read * (5.0 / 1023.0) * 11;
float current = at_read * (5.0 / 1023.0);
float watts = voltage * current;
ticks = 0; // Make ticks zero before starting interrupts.
interrupts(); // or use sei(); to Enables interrupts
delay (1000); //Wait 1 second
noInterrupts(); // or use cli(); to Disable interrupts
//Times sensorclicks (which is apprioxiamately the fequency the fan
//is spinning at) by 60 seconds before dividing by the fan's divider
// this gives reasonable accuracy in upto few 10s of RPM
// for more accurate and fast measurements, other algorithms is required.
Speed = ((ticks * 60)/fannspace[fann].fanndiv);
u8g.setContrast(130);
sensors.requestTemperatures(); // Ask temp
u8g.firstPage();
do {
u8g.setFont(u8g_font_6x12);
u8g.drawStr(0, 7, "Ubuf");
u8g.setPrintPos(27, 7);
u8g.print(voltage);
u8g.setPrintPos(61, 7);
u8g.print("Volt");
u8g.setFont(u8g_font_6x12);
u8g.drawStr(0, 15, "Ibuf");
u8g.setPrintPos(27, 15);
u8g.print(current);
u8g.setPrintPos(61, 15);
u8g.print("Amp");
u8g.setFont(u8g_font_6x12);
u8g.drawStr(0, 23, "Pbuf");
u8g.setPrintPos(27, 23);
u8g.print(watts);
u8g.setPrintPos(61, 23);
u8g.print("Watt");
u8g.setFont(u8g_font_6x12);
u8g.drawStr(0, 31, "Fan");
u8g.setPrintPos(19, 31);
u8g.print(fanLCD);
u8g.setPrintPos(31, 31);
u8g.print("%");
char SpeedString[4]; // Buffer to store string of 4 chars + 0 termination
sprintf(SpeedString, "%4d", Speed); // change this to %3, 4 ,5 etc depending upon your max speed
u8g.setPrintPos(39, 31);
u8g.print(SpeedString);
u8g.setPrintPos(67, 31);
u8g.print("RPM");
u8g.setFont(u8g_font_6x12);
u8g.drawStr(0, 39, "Temp");
u8g.setPrintPos(27, 39);
printTemperature(insideThermometer);
u8g.setPrintPos(61, 40);
u8g.print("\260C");
}
while( u8g.nextPage() );
delay(100);
}
Code: Selecteer alles
/*
*((C))PA3BNX CTCSS Encoder for my TM421 es
* @31-05-2017
* led13 is default 293 Hz
*/
const byte pinCTCSS=2; //Audio
const byte pinButton=3; //Button
const byte pinLed1=4;
const byte pinLed2=5;
const byte pinLed3=6;
const byte pinLed4=7;
const byte pinLed13=13;
//Tone Hz values
const int T0=0;
const int T1=193; //PI2NOS
const int T2=72;
const int T3=77;
const int T4=83;
const int T5=89;
//Integer
int x;
void CheckLeds()
{
int i=75;
digitalWrite(pinLed1,HIGH);
delay(i);
digitalWrite(pinLed2,HIGH);
delay(i);
digitalWrite(pinLed3,HIGH);
delay(i);
digitalWrite(pinLed4,HIGH);
delay(i);
digitalWrite(pinLed13,HIGH);
delay(1000);
digitalWrite(pinLed1,LOW);
digitalWrite(pinLed2,LOW);
digitalWrite(pinLed3,LOW);
digitalWrite(pinLed4,LOW);
digitalWrite(pinLed13,LOW);
}
void SetTone(int t)
{
noTone(pinCTCSS);
digitalWrite(pinLed13,LOW);
digitalWrite(pinLed1,LOW);
digitalWrite(pinLed2,LOW);
digitalWrite(pinLed3,LOW);
digitalWrite(pinLed4,LOW);
if (t!=0){tone(pinCTCSS,t);}
}//Last SetTone
void setup() {
// put your setup code here, to run once:
//Serial.begin(9600);
//Input
pinMode(pinButton,INPUT_PULLUP);
//Led Output
pinMode(pinLed1,OUTPUT);
pinMode(pinLed2,OUTPUT);
pinMode(pinLed3,OUTPUT);
pinMode(pinLed4,OUTPUT);
pinMode(pinLed13,OUTPUT);
//Check Leds
CheckLeds();
//Default tone setup
x=-0;
SetTone(T1);
digitalWrite(pinLed13,HIGH);
}
void loop() {
// put your main code here, to run repeatedly:
//Read the Push Button
while (digitalRead(pinButton)==HIGH)
{
delay(150);//Debounce hi
//Do nothing
}
x++;
if (x > 4) {x=-1;}
//Serial.print(x);
switch(x)
{
case -1:
SetTone(T0);
break;
case 0:
SetTone(T1);
digitalWrite(pinLed13,HIGH);
break;
case 1:
SetTone(T2);
digitalWrite(pinLed1,HIGH);
break;
case 2:
SetTone(T3);
digitalWrite(pinLed2,HIGH);
break;
case 3:
SetTone(T4);
digitalWrite(pinLed3,HIGH);
break;
case 4:
SetTone(T5);
digitalWrite(pinLed4,HIGH);
break;
default:
//Nothing ?
break;
}
delay(500); //Delay for next CTCSS
}//Last Bracket Loop