Show Posts
Pages: 1 ... 20 21 [22] 23 24 ... 283
316  Using Arduino / Programming Questions / Re: How to make a variable stay true without delay on: June 29, 2014, 11:33:04 am
Quote
But how can I combine this with the mills() function?
So that it says: If button initially changes its state from
unpressed to pressed, set motion=true and set the timer.
Both my (pseudo)code and Rob's code deal with the timing.  The motion variable will remain true for the duration of period in my code (buttonTime in Rob's).  If you want an action to take place only when the button becomes pressed, ie the timing starts, then set another boolean variable to true once the action has been taken and don't take the action again until the new variable is false.  Reset the variable to false at the end of the timing test
Code:
start of loop()
  if the button is pressed and motion is false
    set startTime to millis()
    set motion to true
    set actionTaken to false
  end if
 
  if millis() - startTime >= period
    set motion to false
  end if

  if motion == true and actionTaken == false
    take action
    actionTaken = true
  end of if
end of loop()
317  Using Arduino / Programming Questions / Re: Reading serial data without 'terminator' on: June 29, 2014, 09:41:05 am
Quote
It's an old machine, it's not common, there is no much information about it.
What is this mysterious machine ?
318  Using Arduino / Programming Questions / Re: Arduino Coin Bank Question (counting time) on: June 29, 2014, 09:37:52 am
Quote
The problem is that a can't find an easy way to count the time from a given point.
Code:
if (something == true)
{
  startTime = millis();
}
That has saved the start time.

Code:
if (millis() - startTime >= period)
{
  //code to be executed after a period of time has passed
}
319  Using Arduino / Programming Questions / Re: How to make a variable stay true without delay on: June 29, 2014, 08:08:53 am
BlinkWithoutDelay will show you how to do timing without using delay() so that the program does not halt for 3 seconds which allows you to do other things like read inputs but people find it difficult to apply the BWD principle to things other than turning a pin on and off.

Something like this will do what you want
Code:
start of loop()
  if the button is pressed and motion is false
    set startTime to millis()
    set motion to true
  end if
 
  if motion is true and millis() - startTime >= period
    set motion to false
  end if
  //any code that does not block loop() from running goes here
end of loop()
320  Using Arduino / Programming Questions / Re: ls20031 GPS chip cant receive NMEA messages continously on: June 29, 2014, 07:52:30 am
Quote
i am following this tutorial

https://www.sparkfun.com/tutorials/176

and it said, RX to RX , tx to TX
Where does it say that ?  What I can see is
Quote
IF you have TX on the LS20031 going to TX on the Arduino then TinyGPS will give you a bunch of zeros and a headache.

Quote
GPS pin 2 to RX
GPS pin 3 to TX
How have you got the SoftSerial connection defined ?  Its first parameter is the Tx pin and the second the Rx pin
321  Using Arduino / Programming Questions / Re: Forming data block for sending over IridiumSBD on: June 29, 2014, 01:35:16 am
Research the snprintf() function.
It will allow you to format a string from variables and text.

322  Using Arduino / Programming Questions / Re: Need to fade LEDs while still monitoring other inputs on: June 29, 2014, 01:32:36 am
I have just noticed a problem with my suggested code.  As it stands timingStarted will be set to millis() each time through loop() if the trigger condition is still true.  Another variable is needed to indicate that timing is in progress to stop this happening.

My other mistake was to copy your analogWrite() code and forgetting to modify it to fade the LEDs as you noticed.

Try this
Code:
if (sensorReading >= threshold && sensorReading > sensorReading2 && sensorReading > sensorReading3)
{
  if (timerRunning == false)
  {
    state = 1;
    timingStarted = millis();
    timerRunning = true;
    counter = 0;
  }
}

if (state == 1)
{
  if (millis() - timingStarted >= 15)
  {
    analogWrite(ledPin2, counter);
    analogWrite(ledPin3, 0);
    analogWrite(ledPin, 0);
    timingStarted = millis();
    counter++;
    if (counter > 255)
    {
      state = 0;
    }
  }
}
I suggest that you write this as a standalone program to test the principle before trying to incorporate it into your main program.
323  Using Arduino / Programming Questions / Re: Need to fade LEDs while still monitoring other inputs on: June 28, 2014, 03:41:38 pm
Code:
  if (sensorReading >= threshold && sensorReading > sensorReading2 && sensorReading > sensorReading3)
  {
    sensorReading2 = 0;
    sensorReading3 = 0;

    for (int i = 255; i >= 0; i = i - 5)
    {
      analogWrite(ledPin, i);
      analogWrite(ledPin2, 0);
      analogWrite(ledPin3, 0);
      delay(15);
    }
  }
As you have correctly described, during the code above combination of the for loop and the delay() prevents inputs being read.  The BlinkWithoutDelay principle uses millis() to not and check the time at intervals until the required time has passed.  If it has not passed than you can read other inputs until it has.  You will need to refactor your code to use this method but once the principle is grasped it is simple.

Firstly, when the condition to start the fade start is true set a variable to a value.  This state variable will be used each time through loop() to trigger a timing check like this.
Code:
  if (sensorReading >= threshold && sensorReading > sensorReading2 && sensorReading > sensorReading3)
  {
    state = 1;
    timingStarted = millis();
    counter = 0;
  }

  if (state == 1)
  {
    if (millis() - timingStarted >= 15)
    {
      analogWrite(ledPin2, i);
      analogWrite(ledPin3, 0);
      analogWrite(ledPin, 0);
      timingStarted = millis();
      counter++;
      if (counter > 255)
      {
        state = 0;
      }
    }
  }
I have left out the niceties of declaring variables.  The timing variables should be declared as unsigned longs and the 15 can be replaced by a variable.

Because the code does not block operation anywhere the loop() function will continue to run.  To stop the fade at any time just set state = 0 and set the LEDs to the required value.
324  Using Arduino / Programming Questions / Re: ls20031 GPS chip cant receive NMEA messages continously on: June 28, 2014, 02:07:11 pm
Quote
for arduino i put the RX pin with RX GPS and so do TX
Shouldn't the GPS Tx go to the Arduino Rx and vice versa ?
When the GPS transmits the Arduino receives.
325  Using Arduino / Programming Questions / Re: Growing numbers to the left on: June 28, 2014, 10:27:31 am
The thread title sounds like you are trying to right justify the number when printed.  If so then printing 3 spaces after each number will not work.

Peter's suggestion will though.  The snprintf() function formats a char array containing the number, then you can print the formatted buffer just like any other char.  Try Peter's example, play with the number being printed and see what it does.
326  Using Arduino / Programming Questions / Re: ls20031 GPS chip cant receive NMEA messages continously on: June 28, 2014, 10:22:19 am
You were a bit quick with the bump, don't you think ?
Can you please be as quick posting your code so that we can see what you are doing ?
327  Using Arduino / Programming Questions / Re: Controling servos with buttons on: June 28, 2014, 10:08:25 am
Quote
How about now?
Closer, but still no cigar.
You have no bounds checking on the value of pos.  Its value should stay between 0 and 180.

Also, compare
Code:
  if (digitalRead(buttonleft) == HIGH);
and
Code:
  if (digitalRead(buttonright) == HIGH)
Spot the difference.

Have you got any pulldown resistors on the button pins to stop their voltage floating and providing spurious inputs ?

As written, holding down a button will rapidly cause the servo to move one way or the other (once the code is fixed, of course).  Is that what you want or should the servo only move a little each time a button becomes pressed and not move any further until it is pressed again ?
328  Using Arduino / Programming Questions / Re: Controling servos with buttons on: June 28, 2014, 02:35:40 am
Quote
Whats wrong with it?
Code:
 else (digitalRead(buttonleft) == HIGH);
What is that supposed to do ?  It is not testing the state of buttonleft which is presumably what you want it to do.
Code:
   myservo.write(5);
    myservo.write(-5);
These 2 lines are presumably supposed to move the servo in increments of 5 either way.  However, msyservo.write() takes an absolute position as its argument and not an increment from where the servo currently is.  You need to read the buttons and if pressed update a position variable and use that as the argument to myservo.write() making sure that the value stays in bounds which -5 is not by the way.
329  Using Arduino / Programming Questions / Re: Using Unsigned Chars in Arduino on: June 28, 2014, 02:24:23 am
Quote
as Strings cannot contain unsigned chars.
Do you mean Strings or do you mean strings ?
330  Using Arduino / Programming Questions / Re: push buttons to change frequency on: June 28, 2014, 01:40:46 am
I suggest that you post the code that you have that uses the pot. 
Presumably moving the pot causes a variable to change.  If so then using buttons to change the variable should be easy.
Pages: 1 ... 20 21 [22] 23 24 ... 283