Show Posts
Pages: 1 ... 3 4 [5] 6 7 ... 24
61  Using Arduino / Programming Questions / Re: avoid delay on: December 11, 2013, 11:46:29 am
If you know how long something is supposed to last, like your LED staying on for 20 seconds you can call that Duration.

When the event starts you can write down its StartTime. Then you can keep coming back and check the CurrentTime. Now with some simple math you can know if the event should end.

If the CurrentTime - StartTime < Duration it is NOT time to end the event.

If the CurrentTime - StartTime >= Duration it is time to end the event.

It doesn't matter how time is kept for this to work. Seconds, milli seconds, micro seconds or even minutes the formula is the same. It's only the accuracy that varies.
 
62  Using Arduino / Programming Questions / Re: Problems Using If Statements on: December 09, 2013, 04:17:59 pm
It might be a good exercise to write a sketch just for your switch. Then how it will work with everything else should become more apparent. Joshua makes some good points lets put them to use.

This should be pretty easy to follow. It will (should  smiley-grin) print out the new delay value when you change your switch.

Code:
const long BAUD_RATE = 9600;   // set your preferred  rate here
const int SWITCH_PIN[] = {6, 7, 8, 9, 10, 11}; 
const int PIN_COUNT = sizeof(SWITCH_PIN) / sizeof(SWITCH_PIN[0]);
const unsigned long INTERVAL[] = {1000, 2000, 4000, 8000, 16000, 32000};

// notice that it's the index, i that we want
int readSwitchFunction()
{
  for (int i = 0; i < PIN_COUNT; i++) // start looping through switch pins
  {
    if (digitalRead(SWITCH_PIN[i]) == LOW) return i;  // check for first LOW value
  }
}

void setup()
{
  Serial.begin(BAUD_RATE);
  for (int i = 0; i < PIN_COUNT; i++)
  {
    pinMode(SWITCH_PIN[i], INPUT_PULLUP); // set pinmode
  }
}

void loop()
{
  static unsigned long lastInterval = 0;  // a static variable will keep its value
  unsigned long interval = INTERVAL[readSwitchFunction()];  // get interval value each loop
  if (interval != lastInterval) // act only when interval changes
  {
    lastInterval = interval; // save this value for next loop
    Serial.println(interval);  // print results
  }
}
63  Using Arduino / Programming Questions / Re: Problems Using If Statements on: December 09, 2013, 12:23:13 pm
What's hanging you up? The arrays? Making your own functions?

Learning to use arrays is a must in programming.

We've been sharing concepts that you can take your own code. We haven't been sharing code that you can use directly.
64  Using Arduino / Programming Questions / Re: Problems Using If Statements on: December 09, 2013, 11:35:59 am
I'd write a function to read the pins and return a corresponding value. Then I could have an array of delays. Then I could select the delay duration like this:

Code:
delay(delayArray[readSwitchFunction()]);

With an array of pins the function is simple.

Code:
pin[] = {6, 7, 8, 9, 10, 11};
const int pinCount = sizeof(pin) / sizeof(pin[0]);

int readSwitchFunction(void)
{
     for (int i = 0; i < pinCount; i++)
     {
          if (digitalRead(pin[i]) == LOW) return i;  // check for first LOW value
     }
}
65  Using Arduino / Programming Questions / Re: Help writing a simple program to use with a gear shifter for racing games on: November 27, 2013, 05:19:52 pm
Quote
I could solve the issue by having another button that puts it into 1st gear, whenever it physically goes into neutral on the gearstick. (would have to test if it slowed down gear changes)

Otherwise I could just get into the habit of making sure I put it into 1st whenever I have a bad crash (which is usually my reaction anyway). But I will build it and test both anyway.

If you are worried about how many switches you are reading and maybe slowing it down, don't. I did a similar project and did some timing tests. I found that my sketch was reading somewhere on the order of 76,000 reads per second. It only reads switches and is non-blocking. I used a Leonardo because I'm a PC racing guy and it was easy to do with keyboard control.

Your first solution appears best to me. The second one would attempt to downshift from whatever gear you are in back to first. If the game is in first you need a quick easy way to resynch the state of Arduino with the state of the game.
66  Using Arduino / Programming Questions / Re: Help writing a simple program to use with a gear shifter for racing games on: November 25, 2013, 03:29:56 pm
If you are using an array why not make neutral gear[0] and put reverse at the end?

Also, are you going to just have buttons or are you going to make a shifter of some sort? With a shifter of sorts a normal way for them to work is that a gear is held as long as a switch is closed. As soon as no switches are closed it goes to neutral. Which closely mimics a standard transmission. That would mean not needing a button for neutral. How do you find neutral now with the controller you want to hack?
67  Using Arduino / Programming Questions / Re: simple LED chaser without delay on: November 24, 2013, 09:51:07 am
Once you have advanced i (I would have named it ptr) you can just display all the leds based on it. You could display the leds every time through loop or just when i changes. To display all the leds just cycle through them with a for loop.

Code:
/*
This sketch borrows the array setup from the 5 random blink without sketch
 */
const int numberOfBlades = 3;
int ledPin[numberOfBlades] = {
  6, 7, 9}; // LED pins to use.
unsigned long currentMillis;
unsigned long interval = 83;
unsigned long previousMillis = 0;        // will store last time LED was updated
int i = 0;

void setup() {
  for(int i = 0; i< numberOfBlades; i++)
  {
    pinMode(ledPin[i],OUTPUT);
    digitalWrite(ledPin[i], LOW); // all LEDs off
  }
}

void loop() {
  currentMillis = millis();   
  if( currentMillis - previousMillis >= interval) // Arduino run time - changeTime value greater then ledDelay variable? Yes then
  { 
    previousMillis = currentMillis;  // remember the time now
    i = ++i % (numberOfBlades - 1);        // advance i handle rollover
    for (int n = 0; n < numberOfBlades; n++) // display with new i
    {
      if ( n == i ) digitalWrite(ledPin[n], HIGH);
      else digitalWrite(ledPin[n], LOW);
    }
  }
}
68  Development / Other Software Development / Re: Library generator on: November 22, 2013, 03:26:44 pm
Awesome. Works as promised. Thanks.
69  Using Arduino / Programming Questions / Re: Help writing a simple program to use with a gear shifter for racing games on: November 22, 2013, 03:03:22 pm
Code:
  if (gearnumber < 3) {
                 geardown = 3 - gearnumber
                         geardownchanger(geardown, 3);
          }


You're missing semi colons in all the places like the above.

Should be.

Code:
  if (gearnumber < 3) {
                 geardown = 3 - gearnumber;
                 geardownchanger(geardown, 3);
          }

The reason I asked if you have a switch for neutral is that would be an easy way to always make sure the state of your Arduino and the state of the game matched. You could go neutral then find the gear you want. Might be clumsy in actual game play though.
70  Using Arduino / Programming Questions / Re: Creating a header file. on: November 20, 2013, 05:53:09 pm
Doing a save as on the IDE actually creates a whole new folder with a copy of the entire project.

I sort of cheat. I fire up Eclipse where I have a project called scratch that I experiment with. I go to it and add a new class. This generates both the .cpp and .h files with all the includes, constructors, etc, all with perfect syntax. Then I copy those files to my sketchbook project folder and they'll open up on tabs in the IDE. They can be moved to the libraries when done.
71  Using Arduino / Programming Questions / Re: Keypad entry for a 24 hour time variable on: November 20, 2013, 11:58:15 am
You can use a single int for this.

Say you want the time of 15:45 just use an int value of 1545.


int time = 1545;

Then
int hours = time / 100;
int minutes = time % 100;

That last fancy operator is modulo and it returns the remainder portion of an integer divide. Once you receive a value you can break it apart into hours and minutes then check that it's a valid time before using it. Hours range from 0 - 23 and minutes from 0 - 59.

Does that help?
72  Using Arduino / Programming Questions / Re: Help writing a simple program to use with a gear shifter for racing games on: November 20, 2013, 11:25:25 am
Is this for PC or console?

The shifter you have, up shift switch / down shift switch is called a sequential shifter. Does it use a switch to select neutral or do you have to shift down to neutral? The racing games I play use a switch to select neutral when using a sequential shifter.
73  Using Arduino / Programming Questions / Re: Unable to read pushbutton press properly. on: November 14, 2013, 05:07:29 pm
You're incrementing count while the button is pressed. Your sketch is working exactly how it's coded. You probably should only increment count when the button is pressed and released. So you need to detect state changes and then act on the state. When a button goes from open to pressed do nothing until it is released (goes from pressed to open) then increment count.

Code:
const int buttonPin = 2;
const int ledPin = 13;
const int OPEN = LOW;
int count = 0;
void setup(){
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);     
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);   
  Serial.begin(9600);
  digitalWrite(ledPin,LOW);
}

void loop(){
  static int lastButtonState = OPEN;
  static int lastCount = 0;
  int buttonState = digitalRead(buttonPin);
  // now just check for state change
  if (lastButtonState != buttonState) // detect state change
  {
    lastButtonState = buttonState; // remember it
    // now we'll use the state to decide what to do
    if (buttonState == OPEN) // the button is released now
    {
      count = count + 1; 
    }
  }
  if (count != lastCount) // only print count if it changed
  {
    lastCount = count;
    Serial.print(count);
  }
}

I took the led stuff out. I leave that as an exercise for you to put back in. It could also use some debouncing.

If I detect a state change and the new state is OPEN it had to have made a transition from PRESSED to OPEN.
74  Using Arduino / Programming Questions / Re: GUI using C# - Serial lags? on: November 14, 2013, 02:29:08 pm
There's a good chance it's your Strings. Arduino, with its limited RAM, can have issues with Strings. It's best to use strings ( NULL terminated char arrays). Though I see you building your String with literals right before using it. I'd just change some of the logic to only use literals.

Instead of:
Code:
digital2 = "D2" + digitalRead(2) ? "high" : "low";

I'd do:
Code:
if (digitalRead(2) == HIGH) Serial.println(F("D2 high"));
else  Serial.println(F("D2 low"));

Far more RAM friendly than Strings. I'd eliminate the Strings and see what happens.

75  Using Arduino / Programming Questions / Re: switch toggle on: November 12, 2013, 06:13:12 pm
All that denouncing is, is a way to ensure you have a valid state change. It eliminates possible noise while activating the switch. When a state change is detected you pause for a very brief time, then check the switch again, if it's still in it's new state then you have a valid state change to work with. It can be done with delay() but timing it is preferred as it is non-blocking.
Pages: 1 ... 3 4 [5] 6 7 ... 24