Show Posts
Pages: 1 2 [3] 4 5 ... 25
31  Using Arduino / Programming Questions / Re: I want to remove this delay(); before I freak on: March 21, 2014, 09:14:26 am
I was just about to say what Keith said. 5 millis is a pretty short time. I'm not sure you'd notice just taking it out.

Also it's not a void() it's a function. void in front of a function indicates that the function doesn't return a value. It's the return type for the function.
32  Using Arduino / Programming Questions / Re: analogRead() Problem on: March 20, 2014, 10:53:40 pm
Here is your code doing what you wanted. Notice the use of constants for values that won't change. As sketches grow the more thankful you will be taking this approach.

const int POT_PIN = A0;
const long BAUD_RATE = 9600L;
const unsigned long PAUSE_TIME = 1000UL;

void setup() {
  pinMode(POT_PIN, INPUT);

void loop() {
  int potVal = analogRead(POT_PIN);

Suggested reading:
The read this before posting thread.
The Arduino style guide.
As much as you can about variables in C++. Pay attention to type and scope.
33  Using Arduino / Programming Questions / Re: SWITCH DEBOUNCE on: March 20, 2014, 09:48:45 am
While it's often convenient there is no need to use delay for debouncing. Here's a little snippet of code that I used recently for a button to switch the LCD backlight on or off. All it watches for is a state change. When that happens it starts debouncing. If it passes debouncing the actual state is used to decide the action to take. I know that debouncing is occurring when the debounceTime variable is non-zero.

  // handle button for backlight 
  static int buttonState_1 = HIGH;
  static unsigned long debounceTime_1 = 0;
  int currentButtonState = digitalRead(BUTTON_PIN_1);
  if (buttonState_1 != currentButtonState)
    buttonState_1 = currentButtonState;
    debounceTime_1 = millis();
  if (debounceTime_1 && millis() - debounceTime_1 >= DEBOUNCE_MILLIS)
    debounceTime_1 = 0;
    if (buttonState_1 == LOW)
      if (backLightState)
        backLightState = false;
        backLightState = true;
        backLightOnTime = now();

That only takes an action on a LOW (pressed) value. It ignores releases only because it has nothing to do on release. Code could be added to take an action on release (HIGH) just as easily. I think the key is that you don't need to worry about the button's state when debouncing only that the state has changed. If it passes debouncing, then worry about the state.

ETA: That snippet is part of the loop() function.
34  Using Arduino / Programming Questions / Re: Right adjust number on LCD on: March 15, 2014, 10:10:00 am
Like Paul points out sprintf() is elegant but when I use it in Bob's code it compiles to 3398 bytes space and 191 bytes ram. Bob's uses 2304 bytes space and 189 ram. So it appears to me that sprintf() has a fairly high overhead. If program space is tight that's something to think about.

However, will you get negative values? That means displaying a sign. Bob's approach breaks on negative values but is easily fixed and is still a good space saving approach. sprintf() has a width modifier that makes it elegant of course.

void setup()
  char stg[5];
  for (int aNumber = -110; aNumber < 110; aNumber++)
    sprintf(stg, "%4.3d", aNumber);

void loop()

In my own projects I take Bob's approach.
35  Using Arduino / Programming Questions / Re: function argument passing a string or text on: March 10, 2014, 08:05:31 pm
I'd say there's a good chance that LED_GREEN is actually a #define or a const value that would be found in the library.

Try putting a line in setup() to serial.print(LED_GREEN) and you'll probably get a value that would make an RGB LED green. It's unlikely that a function that displays an RGB LED would take a string for a value.
36  Using Arduino / Programming Questions / Re: testing code on: March 10, 2014, 07:59:03 pm
When I first got into Arduino I had the same idea and went looking for an emulator. I was unable to find a free or open source one that worked very well. The ones that didn't look too bad were for sale for about the same price as an Arduino. Jimmy's conclusion was an actual Arduino was the best solution.
37  Using Arduino / Programming Questions / Re: Comparing charAt and integer not working on: March 08, 2014, 01:02:08 pm
What are you trying to do? My guess is you want a something like a combination lock that uses a potentiometer. I'm guessing that a pot is the mystery device at pin A0. But I can only guess.
38  Using Arduino / Programming Questions / Re: Please Help Me to improve my program project. on: March 07, 2014, 12:09:49 am
int leds[] = {3, 4, 5, 6, 7, 8, 9, 10}; //command code selection

int button = 12;   // Switch no. 1
int buttonCurrentState;
int buttonPrevState;
int counter = 7;
int i;

int ledPin = 11; // Output to Off the Motorcycle with in 40 sec.
int buttonPin = 2;   // switch no. 2  Triger to Anti car jacking
int val = 0;     // variable for reading the pin status

void setup() {

  for (i = 0; i < 8; i = i + 1) {
    pinMode(leds, OUTPUT);

  pinMode(ledPin, OUTPUT);  // output triger to off the motorcycle
  pinMode(buttonPin, INPUT);    // switch no. 2 to command to off the motorcycle

  pinMode(button, INPUT_PULLUP);  // switch no. 1 command


void loop() {

  buttonCurrentState = digitalRead(button);

  if (buttonCurrentState == LOW && buttonPrevState == HIGH) {


    if (counter == 8) {
      counter = 0;

    for (i = 0; i < 8; i = i + 1) {

      if (i == counter) {
        digitalWrite(leds, HIGH);
      } else {
        digitalWrite(leds, LOW);


  buttonPrevState = buttonCurrentState;  // Firts Program

  val = digitalRead(buttonPin);  // read the switch no. 2 command

  if (val == LOW) {         // switch no. 2 command
    digitalWrite(ledPin, LOW); // turn  OFF

  } else {
    digitalWrite(ledPin, LOW);  // turn LED OFF // delay the triger before off the motorcycle
    digitalWrite(ledPin, HIGH);  // turn on to off the motorcycle

There's your code in code tags. It doesn't compile even after I fix the error in the first line. Your biggest problem is how you are using arrays. Go read up on arrays. When it compiles it will be good enough to say it doesn't work.
39  Using Arduino / Programming Questions / Re: Please Help Me to improve my program project. on: March 06, 2014, 10:07:45 pm
Code tags! The # button right above the smilies.

click it .. insert code .. live well
40  Using Arduino / Programming Questions / Re: Using #define for math function on: March 05, 2014, 07:51:30 pm
I agree with the crowd that says don't use define for constants. It avoids what you were worried about happening with define. What type will my number be? Declared constant variables are typed.

constant long rise = 32450L;
constant long run = 400L;
constant long slope = (rise/run);

This way you'll know what type they are.
41  Using Arduino / Programming Questions / Re: Can my program be made better? on: February 26, 2014, 08:32:29 pm
I added some comments

#include <Time.h>
#include <TimeAlarms.h>
//#include "Wire.h"
#include <LiquidCrystal.h>

// So why do you declare so many constants with the same value?
// especially when they point to a pin. There's only one pin A1.

const int MorningAlarmPin1 =  A1;    // Blue Lights ON   

// why not blueLightPin = A1 ?
// For how these variables get used these aren't good names.
// see setup() for more

const int EveningAlarmPin1 =  A1;    // Blue Lights OFF
const int MorningAlarmPin2 =  A2;
const int EveningAlarmPin2 =  A2;
const int MorningAlarmPin3 =  A3;
const int EveningAlarmPin3 =  A3;             
const int ledPin = A1;
const int ledPin2 = A2;
const int ledPin3 = A3;

LiquidCrystal lcd( 8, 9, 4, 5, 6, 7);

/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  D E F I N E  :  H E A T E R    O F F |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/

void setup()
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  setTime(12,59,30,2,23,14); // set time to Saturday 8:29:00am Jan 1 2011
  // create the alarms
  Alarm.alarmRepeat(8,30,0, MorningAlarm);  // Time Blue's come on every day
  Alarm.alarmRepeat(10,32,0,EveningAlarm);   // Time Blue's shut off every day

// Here you use all those variables to set the pinmode on each pin 3 times.

  pinMode(MorningAlarmPin1, OUTPUT);     // set the digital pin as output:
  pinMode(EveningAlarmPin1, OUTPUT);     // set the digital pin as output:
  pinMode(MorningAlarmPin2, OUTPUT);
  pinMode(EveningAlarmPin2, OUTPUT);
  pinMode(MorningAlarmPin3, OUTPUT);
  pinMode(EveningAlarmPin3, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);

void  loop(){
  Alarm.delay(1000); // wait one second between clock display

// I'm also not sure I would have used function names like this
// I might have used blueOn() blueOff(). The time you set the alarm
// with reminds you that it's morning.

// functions to be called when an alarm triggers:
void MorningAlarm(){
  lcd.print("BLUE ON ");

void EveningAlarm(){
  lcd.print("BLUE OFF");
void MorningAlarm2(){
  lcd.print("WHITE ON ");

void EveningAlarm2(){
  lcd.print("WHITE OFF");
void MorningAlarm3(){
  lcd.print("M ON ");

void EveningAlarm3(){
  lcd.print("M OFF ");

// The messages you print to the LCD are all different lengths.
// Are you having any trouble with stray characters?

  void digitalClockDisplay()
  // digital clock display of the time

void printDigits(int digits)
  if(digits < 10)

So the only things I'd change are mostly names and only set pin modes once in setup(). Make a copy and use search and replace. I think it might read better if you moved setup() and loop() to the end.

Oh, there in the, nicely named,  printDigits function find out the difference between ":" and '0' Its an important concept.

42  Using Arduino / Programming Questions / Re: Multiple LEDs without me teach my student! on: February 22, 2014, 09:41:21 am
AWOL is putting you on the right track. You are performing two timing tests each time. You should do one test against interval and just change interval when you toggle the led on or off. When he says assign a different value that's because = is the assignment operator. Different from == comparison for equality.


interval = someNewValue;


interval = 10000;

will change the value of interval.
43  Using Arduino / Programming Questions / Re: Feasibility of a temperature gauge on: February 20, 2014, 07:33:56 pm
Also curious as to what you want to do. I'm imagining a big thermometer with a servo driven hand.

What I would do is deal with each piece of hardware separately at first. I'd write a sketch to output the thermometer values to the serial monitor. Then I'd do a sketch to deal with the servo. Then I'd go back and combine them, using what I learned about them.
44  Using Arduino / Programming Questions / Re: Blink without delay independent offtime control on: February 18, 2014, 10:19:57 pm
Change interval when you change the led state.

const unsigned long ONTIME = 3000UL;
const unsigned long OFFTIME = 5000UL;

void loop()
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      interval = ONTIME;
      ledState = HIGH;
     interval = OFFTIME;
     ledState = LOW;
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
45  Using Arduino / Programming Questions / Re: help with simple if..then program on: February 15, 2014, 09:24:51 am
You have two temperature sensors? You have a lot of magic numbers in your code. Your functions that return the high or low temps could easily be combined into a single function.

The way I see it is if the fan is on you only need to check for turning it off. If the fan is off you only need to check if you should turn it on.

  valHIGH=analogRead(0);      //Read the analog port 0 and store the value in val

  valLOW=analogRead(1);      //Read the analog port 0 and store the value in val

Notice the difference between your code and the comments. This may be your problem. This is why you shouldn't use magic numbers.
Pages: 1 2 [3] 4 5 ... 25