Go Down

Topic: PWM - Peltier working code (Read 1 time) previous topic - next topic

wildbill

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

S_Flex


That'll get you 15. I assume you want 1523. In which case, use this in your snippet:
Code: [Select]
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: [Select]
/*
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);
}

S_Flex

I still had bugs in the display, but finaly worked them out.
Heres the code and pic , take a look and comment.
Code: [Select]

/*
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);
}

Go Up