Pages: [1]   Go Down
Author Topic: PWM - Peltier working code  (Read 1100 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 1
Posts: 47
I'm glad I bought an Arduino Leonardo
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I been working on this project for a few weeks. in the code I'm using I turning a float into a string to cut out the decimal point for the 4 diget 7-segment display. I'm kinda wondering if there is a another way to do it...
Any other suggestions or questions welcomed!

Code:
/*
TMP36 - temperature sensor
For more details on this circuit: http://tinyurl.com/c89tvd
I used a 10k ohm resistor on Vout to-> ground to protect the arduino pin

Serial 7 Segment Display Datasheet
https://github.com/sparkfun/Serial7SegmentDisplay/wiki/Serial-7-Segment-Display-Datasheet

#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);    // nuelectronics lcd-shield v1.1

int numCols = 16;
int numRows = 2;
/*

/* This is a simple program to test the 4 diget Sparkfun 7 segment serial LED display */
#include <SoftwareSerial.h>

#define rxPin 255 // 255 because we don't need a receive pin
// Connect the Arduino pin 3 to the rx pin on the 7 segment display
#define txPin 3

// set up a new serial port
SoftwareSerial LEDserial=SoftwareSerial(rxPin, txPin);

#define ARY_LEN(a) (sizeof(a)/sizeof(a[0]))

/* Pin 3 for display- delay only,
   Pin 8 for CoolDown- delay only,
   Pin 5 PWM Peltier- full function */
const int PWM_pin[]                = { 3,   8,    5   };   // Pin numbers used
const unsigned long Fade_delayMs[] = {1000, 1000, 80  };
const int Fade_start[]             = { 175, 175,  250 }; // This can be 0 to 254
const int Fade_end[]               = { 255, 255,  255 }; // this can be PWMstart+1..255
const int Fade_step[]              = { 1,   1,    1   };
unsigned int Fade_direction[]      = { +1,  +1,   +1  };  // Fade direction for each pin
int Fade_value[]                   = {  0,   0,   0   };
unsigned long Fade_prevMillis[]    = {  0,   0,   0   };
unsigned long currentMillis;

/* TMP36 temperature sensor on Analog-in A0 & A1 Pin's */
int temperaturePin = 0;
int temperaturePin2 = 1;
/* Temp Logic veriables */
int nexttempPin = 0;
String stringOne;
float temperature = 0;
float ConvertTemp = 0;
int StartCooldown = 0;

/* Push Button values */
const int buttonPin = 2;     // the number of the pushbutton pin
unsigned int currentTemp = 2; // controles, 1 = celcius, 2 = farenheit
int buttonState = 0;         // variable for reading the pushbutton status

void GetTemp() {
 temperature = getVoltage(temperaturePin);
 temperature = (((temperature - .5) * 100)*1.8) + 32;
}
/* to be used with the real circuit */
void CoolDown() {
 GetTemp();
 if (temperature <= 80) StartCooldown = 0;
}

void pwmOut(int fadeNum, int value) {
 GetTemp();
 if (temperature >= 100) {
  analogWrite(5, 0);
  StartCooldown = 1;
 }
  else if (temperature >= 95) {
  analogWrite(5, 0);
 }
  else if (temperature <= 95){
  analogWrite(PWM_pin[fadeNum], value);
 }
}

/* to run a simulation 
void lcdOut(int fadeNum, int value) {
    int col, row;
    if (fadeNum <= 3) {
        row = 0;
        col = fadeNum * 4;
    }
    else {
        row = 1;
        col = (fadeNum - 4) * 4;
    }
    lcd.setCursor(col, row);
    lcd.print("    ");
    lcd.setCursor(col, row);
    lcd.print(value, DEC);
}
*/

/* 4 diget, 7-segment serial display, SparkFun
 Set as PWM_pin 3 to only use a 1000ms delay and not fading part of the method */
void LEDdisplay() {
 LEDserial.print('v'); // this is the reset display command
 
 if (nexttempPin == temperaturePin) nexttempPin = temperaturePin2;
 else nexttempPin = temperaturePin; 
 
 temperature = getVoltage(nexttempPin);  //getting the voltage reading from the temperature sensor
 
 if (currentTemp == 2) ConvertTemp = (((temperature - .5) * 100)*1.8) + 32;
 else ConvertTemp = (temperature - .5) * 100;

   
 LEDserial.print('w'); // send the command char for decimal control
 
 if (ConvertTemp >= 100 || ConvertTemp >= -10) {
  SetTemp(1, ConvertTemp);
  if (nexttempPin == temperaturePin2) LEDserial.write(0b00100100); // turn on apostrophe for temp 2
  else if (nexttempPin == temperaturePin) LEDserial.write(0b00000100);
 }
  else if (ConvertTemp >= 1000 || ConvertTemp >= -100) {
  SetTemp(3, ConvertTemp);
  if (nexttempPin == temperaturePin2) LEDserial.write(0b00100000);
 }
 else {
  SetTemp(2, ConvertTemp);
  if (nexttempPin == temperaturePin2) LEDserial.write(0b00100010);
  else if (nexttempPin == temperaturePin) LEDserial.write(0b00000010);
 }   
 LEDserial.print(stringOne);
}

void SetTemp(int value, float temp) {
 // float to string
 char tBuffer[16];
 stringOne =  dtostrf(temp,8,2,tBuffer);
 
 if (value == 1) // 999.9 or -99.9 numbers
  stringOne = stringOne.substring(2,5)+stringOne.substring(6,7);
 else if (value == 3) // 9999 or -999 numbers
  stringOne = stringOne.substring(2,6);
 else // 99.99 or -9.99 numbers
  stringOne = stringOne.substring(3,5)+stringOne.substring(6,8);
}

/* converting from a 0 to 1023 digital range
 to 0 to 5 volts (each 1 reading equals ~ 5 millivolts */
float getVoltage(int pin) {
 return (analogRead(pin) * .004882814);                                       
}

void Fade_outFunc(int fadeNum, int value) {
  // run one or both output functions...
  //lcdOut(fadeNum, value);
  if (StartCooldown == 0 && PWM_pin[fadeNum] == 5) pwmOut(fadeNum, value);
 
  if (StartCooldown == 1 && PWM_pin[fadeNum] == 8) CoolDown();
  if (PWM_pin[fadeNum] == 3) LEDdisplay();
}


void setup() {
  //lcd.begin(numCols, numRows);
  //lcd.clear();
 
  // 4 diget, 7-segment serial display, SparkFun
  pinMode(txPin, OUTPUT);
  LEDserial.begin(9600);

  for (unsigned int i = 0; i < ARY_LEN(PWM_pin); i++){
    Fade_value[i] = Fade_start[i];
    Fade_outFunc(i, Fade_value[i]);
  }
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}


void Fade_run(int fadeNum) {
  currentMillis = millis();
  if(currentMillis - Fade_prevMillis[fadeNum] > Fade_delayMs[fadeNum]) {
    Fade_prevMillis[fadeNum] = currentMillis;

    Fade_value[fadeNum] += Fade_direction[fadeNum] * Fade_step[fadeNum];
    if (Fade_value[fadeNum] >= Fade_end[fadeNum]) {
      Fade_value[fadeNum] = Fade_end[fadeNum];
      Fade_direction[fadeNum] = -Fade_direction[fadeNum];
    }
    else if (Fade_value[fadeNum] <= Fade_start[fadeNum]) {
      Fade_value[fadeNum] = Fade_start[fadeNum];
      Fade_direction[fadeNum] = -Fade_direction[fadeNum];
    }
    Fade_outFunc(fadeNum, Fade_value[fadeNum]);
  }
}

unsigned int i = 0;
void loop() {
 /* Push Button */
 buttonState = digitalRead(buttonPin);
 if (buttonState == HIGH && currentTemp == 2) currentTemp = 1; 
 else if (buttonState == HIGH && currentTemp == 1) currentTemp = 2;

 /* Fade Loop */
 for (i = 0; i < ARY_LEN(PWM_pin); i++) Fade_run(i);
}
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 549
Posts: 46082
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void GetTemp() {
 temperature = getVoltage(temperaturePin);
 temperature = (((temperature - .5) * 100)*1.8) + 32;
}
GetTemp() is a poor name for this function. I expect a function with Get in the name to return a value. This one does not.

Quote
I'm kinda wondering if there is a another way to do it.
Of course there is. Ditch the String class. The dtostrf() function puts the decimal point in a fixed position in tBuffer. You don't need the overhead and problems of the String class to move the characters to the right of the decimal point one place to the left.

What does this issue have to do with the post title?
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 47
I'm glad I bought an Arduino Leonardo
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Quote
I'm kinda wondering if there is a another way to do it.
Of course there is. Ditch the String class. The dtostrf() function puts the decimal point in a fixed position in tBuffer. You don't need the overhead and problems of the String class to move the characters to the right of the decimal point one place to the left.

What would be the other way without using dtostrf()?
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 549
Posts: 46082
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What would be the other way without using dtostrf()?
You could split the float into two parts - the whole number and the fractional part. Then, multiply the fractional part by some amount and truncate. But why? There is nothing wrong with using dtostrf(). It is what you are doing AFTER calling dtostrf() that is the problem.
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 47
I'm glad I bought an Arduino Leonardo
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What would be the other way without using dtostrf()?
You could split the float into two parts - the whole number and the fractional part. Then, multiply the fractional part by some amount and truncate. But why? There is nothing wrong with using dtostrf(). It is what you are doing AFTER calling dtostrf() that is the problem.

It's easyer said than done that way. If you dont want to show me an example its makes me think you may not know how the 4 diget 7-segmant serial display works.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 549
Posts: 46082
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
It's easyer said than done that way.
What is? You said that the String part was simply to strip out the decimal point from the string. And that's what I see happening.

Code:
char tBuffer[16];
 stringOne =  dtostrf(temp,8,2,tBuffer);
 
 if (value == 1) // 999.9 or -99.9 numbers
  stringOne = stringOne.substring(2,5)+stringOne.substring(6,7);
 else if (value == 3) // 9999 or -999 numbers
  stringOne = stringOne.substring(2,6);
 else // 99.99 or -9.99 numbers
  stringOne = stringOne.substring(3,5)+stringOne.substring(6,8);

You can copy the elements of tBuffer to another char array array the same way.
Code:
char cBuffer[16];
int j=0;
if (value == 1) // 999.9 or -99.9 numbers
{
   for(int i=2; i<5; i++)
   {
      cBuffer[j++] = tBuffer[i];
   }
   cBuffer[j++] = tBuffer[6];
   cBuffer[j] = '\0';
}
// The other cases work exactly the same
Now, you have a static array of characters instead of dynamic String with all it's overhead and issues.

If the downstream use of the data requires a String object, I wouldn't be using whatever library REQUIRES a String.
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 47
I'm glad I bought an Arduino Leonardo
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you. I was thinking of a way to do it with math but I would always come up with a number that would force me to have to truncate a part of the number out. If I could use a regular expressions engine I could get it done in one line, but that just tells me I need to know C++ better.

Thanks for the help.
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 47
I'm glad I bought an Arduino Leonardo
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I started to dig around C++ tutes and I seen a few places that say sprintf() can format the value, but i cant figure out how to get it to work on the arduino.

Code:
char tBuffer[5];
float temp = 15.2304;
sprintf(tBuffer, "%2.2f", temp);
// Not sure what to do with tBuffer

I think one of my biggest problems is i dont have an IDE that I can run some test codes on to sort out the logic before i upload it to the arduino.
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 48
Posts: 3417
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The version of sprintf that the arduino uses does not support floats to reduce the amount of memory the library consumes.
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 47
I'm glad I bought an Arduino Leonardo
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The version of sprintf that the arduino uses does not support floats to reduce the amount of memory the library consumes.

so maybe???
Code:
char tBuffer[5];
float temp = 15.2304;
 int x = int(temp);
 sprintf(tBuffer, "%4d", x);
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 48
Posts: 3417
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That'll get you 15. I assume you want 1523. In which case, use this in your snippet:
Code:
int x = int(temp*100.0);
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 47
I'm glad I bought an Arduino Leonardo
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That'll get you 15. I assume you want 1523. In which case, use this in your snippet:
Code:
int x = int(temp*100.0);

Thats what I ended up with was do a small amout of math to move the decimal point then use int() to cut out the remainder. Nice thanks.
Code:
/*
TMP36 - temperature sensor
For more details on this circuit: http://tinyurl.com/c89tvd
I used a 10k ohm resistor on Vout to-> ground to protect the arduino pin

Serial 7 Segment Display Datasheet
https://github.com/sparkfun/Serial7SegmentDisplay/wiki/Serial-7-Segment-Display-Datasheet

#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);    // nuelectronics lcd-shield v1.1

int numCols = 16;
int numRows = 2;
/*

/* This is a simple program to test the 4 diget Sparkfun 7 segment serial LED display */
#include <SoftwareSerial.h>

#define rxPin 255 // 255 because we don't need a receive pin
// Connect the Arduino pin 3 to the rx pin on the 7 segment display
#define txPin 3

// set up a new serial port
SoftwareSerial LEDserial=SoftwareSerial(rxPin, txPin);

#define ARY_LEN(a) (sizeof(a)/sizeof(a[0]))

/* Pin 3 for display- delay only,
   Pin 8 for CoolDown- delay only,
   Pin 5 PWM Peltier- full function */
const int PWM_pin[]                = { 3,   8,    5   };   // Pin numbers used
const unsigned long Fade_delayMs[] = {1000, 1000, 80  };
const int Fade_start[]             = { 175, 175,  250 }; // This can be 0 to 254
const int Fade_end[]               = { 255, 255,  255 }; // this can be PWMstart+1..255
const int Fade_step[]              = { 1,   1,    1   };
unsigned int Fade_direction[]      = { +1,  +1,   +1  };  // Fade direction for each pin
int Fade_value[]                   = {  0,   0,   0   };
unsigned long Fade_prevMillis[]    = {  0,   0,   0   };
unsigned long currentMillis;

/* TMP36 temperature sensor on Analog-in A0 & A1 Pin's */
int temperaturePin = 0; // Hot sensor
int temperaturePin2 = 1; // Cold sensor
/* Temp Logic veriables */
int nexttempPin = 0;
float temperature = 0;
float ConvertTemp = 0;
int StartCooldown = 0;
int tempSet;

/* Push Button values */
const int buttonPin = 2;     // the number of the pushbutton pin
unsigned int currentTemp = 2; // controles, 1 = celcius, 2 = farenheit
int buttonState = 0;         // variable for reading the pushbutton status

void CheckTemp() {
 temperature = getVoltage(temperaturePin);
 temperature = (((temperature - .5) * 100)*1.8) + 32;
}
/* Over temperature protection */
void CoolDown() {
 CheckTemp();
 if (temperature <= 80) StartCooldown = 0;
}

void pwmOut(int fadeNum, int value) {
 CheckTemp();
 if (temperature >= 100) {
  analogWrite(5, 0);
  StartCooldown = 1;
 }
  else if (temperature >= 95) {
  analogWrite(5, 0);
 }
  else if (temperature <= 95){
  analogWrite(PWM_pin[fadeNum], value);
 }
}

/* to run a simulation 
void lcdOut(int fadeNum, int value) {
    int col, row;
    if (fadeNum <= 3) {
        row = 0;
        col = fadeNum * 4;
    }
    else {
        row = 1;
        col = (fadeNum - 4) * 4;
    }
    lcd.setCursor(col, row);
    lcd.print("    ");
    lcd.setCursor(col, row);
    lcd.print(value, DEC);
}
*/

/* 4 diget, 7-segment serial display, SparkFun
 Set as PWM_pin 3 to only use a 1000ms delay and not fading part of the method */
void LEDdisplay() {
 
 LEDserial.print('v'); // this is the reset display command
 
 if (nexttempPin == temperaturePin) nexttempPin = temperaturePin2;
 else nexttempPin = temperaturePin; 
 
 temperature = getVoltage(nexttempPin);  //getting the voltage reading from the temperature sensor
 
 if (currentTemp == 2) ConvertTemp = (((temperature - .5) * 100)*1.8) + 32;
 else ConvertTemp = (temperature - .5) * 100;

   
 LEDserial.print('w'); // send the command char for decimal control
 
 if (ConvertTemp >= 100 && ConvertTemp <= 999
  || ConvertTemp <= -10 && ConvertTemp >= -99.9) {
  SetTemp(1, ConvertTemp);
  if (nexttempPin == temperaturePin2) LEDserial.write(0b00100100); // turn on apostrophe for temp 2
  else if (nexttempPin == temperaturePin) LEDserial.write(0b00000100);
 }
  else if (ConvertTemp >= 1000 && ConvertTemp <= 9999
        || ConvertTemp <= -100 && ConvertTemp >= -999) {
  SetTemp(3, ConvertTemp);
  if (nexttempPin == temperaturePin2) LEDserial.write(0b00100000);
 }
 else if (ConvertTemp < -999 || ConvertTemp > 9999) {
  tempSet = 9999;
 }   
 else {
  SetTemp(2, ConvertTemp);
  if (nexttempPin == temperaturePin2) LEDserial.write(0b00100010);
  else if (nexttempPin == temperaturePin) LEDserial.write(0x02);
 }   
 LEDserial.print(tempSet);
}

void SetTemp(int value, float temp) {
 if (value == 1) { // 999.9 or -99.9 numbers
 temp = temp * 10;
 }
 else if (value == 2) {// 99.99 or -9.99 numbers
 temp = temp * 100;
 }
 // 9999 or -999 numbers too
 tempSet = int(temp);
}

/* converting from a 0 to 1023 digital range
 to 0 to 5 volts (each 1 reading equals ~ 5 millivolts */
float getVoltage(int pin) {
 return (analogRead(pin) * .004882814);                                       
}

void Fade_outFunc(int fadeNum, int value) {
  // run one or both output functions...
  //lcdOut(fadeNum, value);
  if (StartCooldown == 0 && PWM_pin[fadeNum] == 5) pwmOut(fadeNum, value);
 
  if (StartCooldown == 1 && PWM_pin[fadeNum] == 8) CoolDown();
  if (PWM_pin[fadeNum] == 3) LEDdisplay();
}


void setup() {
  //lcd.begin(numCols, numRows);
  //lcd.clear();
 
  // 4 diget, 7-segment serial display, SparkFun
  pinMode(txPin, OUTPUT);
  LEDserial.begin(9600);

  for (unsigned int i = 0; i < ARY_LEN(PWM_pin); i++){
    Fade_value[i] = Fade_start[i];
    Fade_outFunc(i, Fade_value[i]);
  }
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}


void Fade_run(int fadeNum) {
  currentMillis = millis();
  if(currentMillis - Fade_prevMillis[fadeNum] > Fade_delayMs[fadeNum]) {
    Fade_prevMillis[fadeNum] = currentMillis;

    Fade_value[fadeNum] += Fade_direction[fadeNum] * Fade_step[fadeNum];
    if (Fade_value[fadeNum] >= Fade_end[fadeNum]) {
      Fade_value[fadeNum] = Fade_end[fadeNum];
      Fade_direction[fadeNum] = -Fade_direction[fadeNum];
    }
    else if (Fade_value[fadeNum] <= Fade_start[fadeNum]) {
      Fade_value[fadeNum] = Fade_start[fadeNum];
      Fade_direction[fadeNum] = -Fade_direction[fadeNum];
    }
    Fade_outFunc(fadeNum, Fade_value[fadeNum]);
  }
}

unsigned int i = 0;
void loop() {
 /* Push Button */
 buttonState = digitalRead(buttonPin);
 if (buttonState == HIGH && currentTemp == 2) currentTemp = 1; 
 else if (buttonState == HIGH && currentTemp == 1) currentTemp = 2;

 /* Fade Loop */
 for (i = 0; i < ARY_LEN(PWM_pin); i++) Fade_run(i);
}
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 47
I'm glad I bought an Arduino Leonardo
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I still had bugs in the display, but finaly worked them out.
Heres the code and pic , take a look and comment.
Code:
/*
4 - 10k resistors

2 - TMP36 - temperature sensor - 5volt
For more details on this circuit: http://tinyurl.com/c89tvd
I used a 10k ohm resistor on Vout to-> ground to protect the arduino pin

1 - Serial 7 Segment Display Sparkfun Datasheet - 3.3volt
https://github.com/sparkfun/Serial7SegmentDisplay/wiki/Serial-7-Segment-Display-Datasheet

1 - MOSFET RFP30N06LE or RFP30N06L
http://bildr.org/2012/03/rfp30n06le-arduino/

1 - TEC1-12709 - Sealed Peltier - 12volt
maxVolts - 15.4
maxAmps  - 9
maxWatts - 138.6

1 - Pushbutton - 5volt
http://arduino.cc/en/Tutorial/Button

#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);    // nuelectronics lcd-shield v1.1

int numCols = 16;
int numRows = 2;
/*

/* This is a simple program to test the 4 diget Sparkfun 7 segment serial LED display */
#include <SoftwareSerial.h>

#define rxPin 255 // 255 because we don't need a receive pin
// Connect the Arduino pin 3 to the rx pin on the 7 segment display
#define txPin 3

// set up a new serial port
SoftwareSerial LEDserial=SoftwareSerial(rxPin, txPin);

#define ARY_LEN(a) (sizeof(a)/sizeof(a[0]))

/* Pin 3 for display- delay only,
   Pin 8 for CoolDown- delay only,
   Pin 5 PWM Peltier- full function */
const int PWM_pin[]                = { 3,   8,    5   };   // Pin numbers used
const unsigned long Fade_delayMs[] = {1000, 1000, 80  };
const int Fade_start[]             = { 175, 175,  250 }; // This can be 0 to 254
const int Fade_end[]               = { 255, 255,  255 }; // this can be PWMstart+1..255
const int Fade_step[]              = { 1,   1,    1   };
unsigned int Fade_direction[]      = { +1,  +1,   +1  };  // Fade direction for each pin
int Fade_value[]                   = {  0,   0,   0   };
unsigned long Fade_prevMillis[]    = {  0,   0,   0   };
unsigned long currentMillis;

/* TMP36 temperature sensor on Analog-in A0 & A1 Pin's */
int temperaturePin = 0; // Hot sensor
int temperaturePin2 = 1; // Cold sensor
/* Temp Logic veriables */
float temperature = 0;
int nexttempPin = 0;
int StartCooldown = 0;
int tempSet;

/* Push Button values */
const int buttonPin = 2;     // the number of the pushbutton pin
unsigned int currentTemp = 2; // controles, 1 = celcius, 2 = farenheit
int buttonState = 0;         // variable for reading the pushbutton status

void CheckTemp() {
 temperature = getVoltage(temperaturePin);
 temperature = (((temperature - .5) * 100)*1.8) + 32;
}
/* Over temperature protection */
void CoolDown() {
 CheckTemp();
 if (temperature <= 80) StartCooldown = 0;
}

void pwmOut(int fadeNum, int value) {
 CheckTemp();
 if (temperature >= 100) {
  analogWrite(5, 0);
  StartCooldown = 1;
 }
  else if (temperature >= 95) {
  analogWrite(5, 0);
 }
  else {
  analogWrite(PWM_pin[fadeNum], value);
 }
}

/* to run a simulation 
void lcdOut(int fadeNum, int value) {
    int col, row;
    if (fadeNum <= 3) {
        row = 0;
        col = fadeNum * 4;
    }
    else {
        row = 1;
        col = (fadeNum - 4) * 4;
    }
    lcd.setCursor(col, row);
    lcd.print("    ");
    lcd.setCursor(col, row);
    lcd.print(value, DEC);
}
*/

/* 4 diget, 7-segment serial display, SparkFun
 Set as PWM_pin 3 to only use an ajustable 1000ms delay and not fading part of the method */
void LEDdisplay() {
 
 LEDserial.print('v'); // this is the reset display command
 
 if (nexttempPin == temperaturePin) nexttempPin = temperaturePin2;
 else nexttempPin = temperaturePin; 
 
 temperature = getVoltage(nexttempPin);  //getting the voltage reading from the temperature sensor
 
 if (currentTemp == 1) temperature = (temperature - .5) * 100;
 else temperature = (((temperature - .5) * 100)*1.8) + 32;
 
 LEDserial.print('w'); // send the command char for decimal control
 temperature += 0.001f; // ensure hundredth place
 
 if (temperature < 100 && temperature >= 0
  || temperature > -10 && temperature <=  0){
  if (nexttempPin == temperaturePin2) LEDserial.write(0b00100010);
  else LEDserial.write(0b00000010);
  tempSet = int(temperature*100);
 }
 else if (temperature >= 100 && temperature <= 1000
       || temperature <= -10 && temperature >= -100) {
  if (nexttempPin == temperaturePin) LEDserial.write(0b00000100);
  else LEDserial.write(0b00100100); // turn on apostrophe for temp 2
  tempSet = int(temperature*10);
 }
 else { // some error
  LEDserial.write(0b00111111);
  tempSet = 9999; 
 }
 // 4 place format to fix absent diget alignment.
 if(tempSet<10 && tempSet>=0) LEDserial.print("000");
 else if(tempSet<100&&tempSet>=10) LEDserial.print("00");
 else if(tempSet<1000&&tempSet>=100) LEDserial.print("0");
 else if(tempSet<0 &&tempSet>-10) LEDserial.print("--");
 else if(tempSet>-100&&tempSet<=-10) LEDserial.print("-");
 
 LEDserial.print(tempSet);
}

/* converting from a 0 to 1023 digital range
 to 0 to 5 volts (each 1 reading equals ~ 5 millivolts */
float getVoltage(int pin) {
 return (analogRead(pin) * .004882814);                                       
}

void Fade_outFunc(int fadeNum, int value) {
  // run one or both output functions...
  //lcdOut(fadeNum, value);
  if (StartCooldown == 0 && PWM_pin[fadeNum] == 5)
  pwmOut(fadeNum, value);
 
  if (StartCooldown == 1 && PWM_pin[fadeNum] == 8)
  CoolDown();
 
  if (PWM_pin[fadeNum] == 3)
  LEDdisplay();
}


void setup() {
  //lcd.begin(numCols, numRows);
  //lcd.clear();
 
  // 4 diget, 7-segment serial display, SparkFun
  pinMode(txPin, OUTPUT);
  LEDserial.begin(9600);
 
  // Fade & delay
  for (unsigned int i = 0; i < ARY_LEN(PWM_pin); i++){
    Fade_value[i] = Fade_start[i];
    Fade_outFunc(i, Fade_value[i]);
  }
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}


void Fade_run(int fadeNum) {
  currentMillis = millis();
  if(currentMillis - Fade_prevMillis[fadeNum] > Fade_delayMs[fadeNum]) {
    Fade_prevMillis[fadeNum] = currentMillis;

    Fade_value[fadeNum] += Fade_direction[fadeNum] * Fade_step[fadeNum];
    if (Fade_value[fadeNum] >= Fade_end[fadeNum]) {
      Fade_value[fadeNum] = Fade_end[fadeNum];
      Fade_direction[fadeNum] = -Fade_direction[fadeNum];
    }
    else if (Fade_value[fadeNum] <= Fade_start[fadeNum]) {
      Fade_value[fadeNum] = Fade_start[fadeNum];
      Fade_direction[fadeNum] = -Fade_direction[fadeNum];
    }
    Fade_outFunc(fadeNum, Fade_value[fadeNum]);
  }
}

void loop() {
 /* Push Button */
 buttonState = digitalRead(buttonPin);
 if (buttonState == HIGH && currentTemp == 2) currentTemp = 1; 
 else if (buttonState == HIGH && currentTemp == 1) currentTemp = 2;

 /* Fade & Delay Loop */
 for (unsigned int i = 0; i < ARY_LEN(PWM_pin); i++)
 Fade_run(i);
}


* peltier.png (1185.77 KB, 900x675 - viewed 51 times.)
Logged

Pages: [1]   Go Up
Jump to: