Show Posts
Pages: 1 2 [3] 4
31  Using Arduino / Programming Questions / Re: PWM - Peltier working code on: December 03, 2012, 06:28:42 am
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.
32  Using Arduino / Programming Questions / Re: PWM - Peltier working code on: December 01, 2012, 04:07:04 pm
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.
33  Using Arduino / Programming Questions / Re: PWM - Peltier working code on: December 01, 2012, 09:39:24 am

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()?
34  Using Arduino / Programming Questions / Re: Parse temperature values to a webpage. on: December 01, 2012, 09:23:39 am
Im just guessing but You may have to turn the float value from the sensor into a string.
Code:
char tBuffer[16];
 String stringOne =  dtostrf(temp,8,2,tBuffer);

Then use the value...
35  Using Arduino / Programming Questions / PWM - Peltier working code on: December 01, 2012, 09:16:16 am
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);
}
36  Using Arduino / Project Guidance / Re: 5 buttons on one pin. on: November 29, 2012, 08:53:52 am

The resistors are connected to the buttons in a daisy chain see picture.
Can I ask if I can use 5 pins on the Arduino to go to the same transistor or would that cause problems. I have no trouble controlling 5 separate pins with the buttons like this but would like to know if they all connected to the same output and say 2 buttons were pressed at the same time.


  if (val >850 && val < 865 ) digitalWrite (Pin8,  HIGH); // turn on test led 1
  if (val >890 && val < 910 ) digitalWrite (Pin9,  HIGH); // turn on test led 2
  if (val >930 && val < 940 ) digitalWrite (Pin10, HIGH); // turn on test led 3
  if (val >970 && val < 980 ) digitalWrite (Pin11, HIGH); // turn on test led 4
  if (val >1015 && val < 1030)digitalWrite (Pin12, HIGH); // turn on test led 5
Jeremy



Shouldn't that 68k resistor be 10k to protect the pin?
37  Development / Suggestions for the Arduino Project / Re: Interest in overhauling the IDE user interface? on: November 29, 2012, 07:34:02 am
sounds like you all are updating the Arduino IDE now and taking suggestions?

Wish list suggestions...
- Option for numbered lines, maybe have the background for the number light gray
- Search option in "Edit" add "Find and Replace" with check able options to replace all or ask to replace.
- Option to drag & drop selected text
- Option to ask to save the sketch if it was edited before Uploading or Require the sketch to be saved before uploading
- Change font type


FYI: I'm using 1.0.1 on win7 64-bit
38  Using Arduino / Programming Questions / Re: PWM - Fade a pin on: November 18, 2012, 06:21:27 am
Using ARY_LEN(commands) is IMHO more convenient than having to update a constant every time you add or remove a string from the array.

I agree!
39  Using Arduino / Programming Questions / Re: PWM - Fade a pin on: November 16, 2012, 05:19:12 am
I have another question.

Since  the function millis() will reset to zero in about 50 days.

Wont I have to reset the Fade_prevMillis values of the program when the millis function resets?
40  Using Arduino / Programming Questions / Re: PWM - Fade a pin on: November 12, 2012, 07:13:33 am
Quote
Mistake?
Maybe. Maybe not. The loop index will run from 0 to one less than the value output by the ARY_LEN() function. Suppose that ARY_LEN() returned 10. The loop indices would then be 0 to 9, which are the valid index values for a 10 element array.

Quote
This is the way I think it should be, but Im not sure how sizeof works...
If ARY_LEN returns 10 for a 10 element array, i would be 0 to 10. The last value is NOT a valid index into a 10 element array.

Quote
Or would this be the right way if <= was used then a -1 would be needed?
Yes, but that's essentially the same as the less than loop without the -1. The values that i takes on are 0 to 9, if ARY_LEN returns 10.

Thanks I had to be sure. I know i will be using array's and getting to know C++ has some major differences from the other languages I have worked with in that past. In programing being one number off can be a factor in things working or not working 100%. I'm not saying his code was broke because i have not ran it yet and I'm still new to C++. So expect newbie questions like that from me and suspicions of code that I wright having bugs or not....

Thanks for everyone's help!
41  Using Arduino / Programming Questions / Re: PWM - Fade a pin on: November 11, 2012, 04:26:27 pm
Code:
Doesn't the for need <= to get to the last number?

What exact piece of code are you referring to ?

Im refering to any for in that last 2 code post dealing with an array in the for. I was the one that started that mistake with the code post fading more than one pin and asked how to get the array numbers.

Mistake?
Code:
for (unsigned int i = 0; i < ARY_LEN(faders); i++) {

This is the way I think it should be, but Im not sure how sizeof works...
Code:
for (unsigned int i = 0; i <= ARY_LEN(faders); i++) {

Or would this be the right way if <= was used then a -1 would be needed?
Code:
for (unsigned int i = 0; i <= ARY_LEN(faders) -1; i++) {
42  Using Arduino / Programming Questions / Re: PWM - Fade a pin on: November 11, 2012, 06:39:42 am
Doesn't the for need <= to get to the last number?

I'm sure I will have more question later, but for now you gave me something to play with.

Thanks for the help.
43  Using Arduino / Programming Questions / Re: PWM - Fade a pin on: November 10, 2012, 11:25:19 am
Using that last code you posted I'm starting to work on a program that will control more than one PWM pin. What I have now hasn't been tested and uses a lot of arrays, but is readable.
If I can get it into an array of arrays it would be less readable, but may cut down on the clutter.
Also doing more than one at a time I think the program may have some timing issues where pins are left on or off longer than set by Fade_delaysMs because of the delays before and after each other.
I may just make each delay the same but its kinda nice to have that control.

One thing about the code is I'm not sure if sizeof will return the array number or whats the way to do that?
Code:
const int PWM_pins[]                = {3,   4,   8,   3,   6};   // Pin numbers used
const unsigned long Fade_delaysMs[] = {500, 500, 500, 500, 500}; // with just 50ms I could not read the numbers on the lcd ;-)
const int Fade_starts[]             = {175, 175, 175, 175, 175}; // This can be 0 to 254
const int Fade_ends[]               = {255, 255, 255, 255, 255}; // this can be PWMstart+1..255
const int Fade_steps[]              = {8,   8,   8,   8,   8};   // this example lets you increase or decrease the pwm value by more than 1 count at a time
int Fade_directions[]               = {+1,  +1,  +1,  +1,  +1};  // Fade direction for each pin

int Fade_values[] = {0,0,0,0,0};
unsigned long Fade_prevMillis[] = {0,0,0,0,0};

void Fade_outFunc(int array_number) {
    unsigned long currentMillis = millis();

    if(currentMillis - Fade_prevMillis[array_number] > Fade_delaysMs[array_number]) {
        Fade_prevMillis[array_number] = currentMillis;

        Fade_values[array_number] += Fade_directions[array_number] * Fade_steps[array_number];
        if (Fade_values[array_number] >= Fade_ends[array_number]) {
            Fade_values[array_number] = Fade_ends[array_number];
            Fade_directions[array_number] = -Fade_directions[array_number];
        }
        else if (Fade_values[array_number] <= Fade_starts[array_number]) {
            Fade_values[array_number] = Fade_starts[array_number];
            Fade_directions[array_number] = -Fade_directions[array_number];
        }

        analogWrite(PWM_pins[array_number], Fade_values[array_number]);
    }   
}


void setup() {
 for (int i = 0; i < sizeof(PWM_pins) - 1; i++){
    Fade_values[i] = Fade_starts[i];
    analogWrite(PWM_pins[i], Fade_values[i]);
 }
}

int a = 0;
void loop() {
 for (a = 0; a < sizeof(PWM_pins) - 1; a++){
    Fade_outFunc(a);
 }
}
44  Using Arduino / Programming Questions / Re: PWM - Fade a pin on: November 10, 2012, 08:11:30 am
Ok, I'm away from the bench right now, and I just have an arduino+lcd shield to test things on...

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

int numCols = 16;
int numRows = 2;

const int PWM_pin  = 3;
const unsigned long Fade_delayMs = 500;    // with just 50ms I could not read the numbers on the lcd ;-)
const int Fade_start  = 175; // This can be 0 to 254
const int Fade_end    = 255; // this can be PWMstart+1..255

int Fade_value;
int Fade_direction = +1;
int Fade_step = 8;        // this example lets you increase or decrease the pwm value by more than 1 count at a time

unsigned long Fade_prevMillis;


/* to be used with the real circuit */
void pwmOut(int value) {
    analogWrite(PWM_pin, value);
}

/* to run a simulation */
void lcdOut(int value) {
    lcd.clear();
    lcd.print(value, DEC);
}

void Fade_outFunc(int value) {
    // run one or both output functions...
    lcdOut(value);
    pwmOut(value);
}


void setup() {
    lcd.begin(numCols, numRows);
    lcd.clear();

    Fade_value = Fade_start;
    Fade_outFunc(Fade_value);
}


void loop() {
    unsigned long currentMillis = millis();

    if(currentMillis - Fade_prevMillis > Fade_delayMs) {
        Fade_prevMillis = currentMillis;

        Fade_value += Fade_direction * Fade_step;
        if (Fade_value >= Fade_end) {
            Fade_value = Fade_end;
            Fade_direction = -Fade_direction;
        }
        else if (Fade_value <= Fade_start) {
            Fade_value = Fade_start;
            Fade_direction = -Fade_direction;
        }

        Fade_outFunc(Fade_value);
    }
}

All the variables and functions that I use for the fading algorithm are prefixed with Fade_, to ease integration in a bigger sketch.

The fading algorithm is decoupled from the hardware-related code. This makes it easy to change the sketch behaviour from writing to and lcd to changing a fan speed by just enabling one or more "out" functions inside Fade_outFunc().

This all cries to be turned into a Fade() class, but that's another topic on its own... ;-)

To make it behave like your previous sketch, change Fade_delayMs to 50 and Fade_step to 1.

Thanks!!! that saved me a week and a lot of questions.

I have one question. Would my Arduino Leonardo be maxed out if I added 2 fans, 2 peltiers, 4 TMP36 temp sensor and a 7-segment Serial Display? Can I fit more maybe 1 to 4 more push buttons?

I also had an idea to use one analog pin for all my push buttons and put the push buttons on different resistance to use parts of one analog rang for all my switching. Do you know what i mean?
45  Using Arduino / Programming Questions / Re: PWM - Fade a pin on: November 09, 2012, 08:43:11 pm
After going through the code to check that the numbers didn't go out of rang and didn't have any repeating numbers.
I used this code for a fan on a mosfet and it works!!!!!!!!
I made it so the settings can be changed at the constants. I'm starting the PWM at 175 because I have a fan that will make noise on low RPM's but it was just a small test for something bigger that will most likely use this code.
Thanks to all that helped.
Code:
const int fadePin3  = 3;
const int DelayTime = 50;
const int PWMstart  = 175; // This can be 0 to 254

int i             = PWMstart;
int pwm           = PWMstart;
int PWMdirection  = +1;    // either +1 or -1

void setup(){
  // Not needed
}

void loop(){
  // Fade pin loop
  for (i = PWMstart; i < 255; i++) {   
    analogWrite(fadePin3, pwm);
    delay(DelayTime);    // see "blink without delay" example for how to get rid of this!
    pwm += PWMdirection;
  }
  // Switch fade direction
  if (pwm == 255) PWMdirection = -1;
  if (pwm == PWMstart) PWMdirection = +1;
}
Pages: 1 2 [3] 4