Loading...
  Show Posts
Pages: 1 [2] 3 4 ... 16
16  Using Arduino / Programming Questions / Re: Program game does not work on chip, whats wrong with program on: May 02, 2013, 08:41:23 pm
Yes. How does the game work?
17  Using Arduino / Programming Questions / Re: Program game does not work on chip, whats wrong with program on: May 02, 2013, 08:22:11 pm
Forget code for a moment. Exactly how do you see your game playing out?
18  Using Arduino / Programming Questions / Re: Reading buttons to count! on: May 01, 2013, 05:15:54 pm
In C and C++ variables are very strictly typed and very tightly scoped. Your issue, as pointed out by Arrch is one of scope.

I highly recommend the tutorial at cplusplus.com. At least read the section on variables and data types.

http://cplusplus.com/doc/tutorial/
19  Using Arduino / Programming Questions / Re: Code help? Buttons? on: April 22, 2013, 08:23:05 pm
Don't use pins 0 or 1 they are used for serial. Move your switches to 2 and 3. I also recommend using the internal pullup resistors. You have no debouncing strategy.
20  Using Arduino / Programming Questions / Re: Numbered Variables In For Loop on: April 13, 2013, 02:39:01 pm
You're trying to use a technique for referencing an array on non-array type variables.

Code:
// an array declaration
int encoder[4];

//

for (byte j = 0; j < 4; j++)
{
     Serial.print("Encoder ");
     Serial.print(j + 1);            // array index start at 0
     Serial.print(" [");
     Serial.print(encoder[j]);    // the encoder value you are looking for
     Serial.print("]  ");             // you may want to make this one println()
}

// spot the difference?


You may want to read up more on using arrays. Try cplusplus.com
21  Using Arduino / Programming Questions / Re: Difficulty appending a local buffer to a string value. on: April 13, 2013, 01:53:33 pm
print() not println() which adds a linefeed

See Arrch's example.
22  Using Arduino / Programming Questions / Re: How to trigger Relays on: April 12, 2013, 02:29:05 pm
It might help you to think of your requirements in simpler terms. Instead of thinking of it as relay A on for 30 then AB on for 30 then B on for 30. I just see two relays running for a minute each with the one starting 30 seconds after the other. Problem is that you can't see them this way until you come to grips with blink without delay.
23  Using Arduino / Programming Questions / Re: LED blink sequence accordingly to array on: April 12, 2013, 12:09:16 pm
I recently breadboarded up a row of ten leds controlled by an UNO. One failed so now it's nine. So I've been playing with this type of thing a bunch lately as a form of mental exercise. I love solving puzzles.

Your sketch looks okay other than it will only run through the pattern once. Which is fine if that's what you want. If it should run over and over it belongs in loop. It is also poor technique with Arduino to use delays like that. Using the chip's clock is preffered.

Here's how I'd take on your problem. (Not tested. Written off the top if my head.)

Code:
// first an array of our led pins declared globally
const byte ledPin[] = {2,3,4};   // I don't like to make the variable plural
// how many leds do we have
byte ledCount = sizeof(ledPin) / sizeof(ledPin[0]);
// now the sequence
const byte sequence[] = {1,2,0,1,0,2};
// it will help to know how many elements this array holds
byte sequenceCount = sizeof(sequence) / sizeof(sequence[0]);
// time between changes
const unsigned long duration = 400; // it's best to use unsigned longs with timing variables

// now we're ready to set our pins
void setup(){
    for (byte index = 0; index < ledCount; index++)
    {
         pinMode(ledPin[index], OUTPUT);
    }
}

// loop will do all the work and display the sequence repeatedly
void loop(){
// we'll need some variables
static byte pointer = 0; // we'll use this to point to the current sequence
static unsigned long lastChangeTime = 0; // for timing purposes
unsigned long currentTime = millis();

// we'll display all the leds then advance the pointer if it's time to
if (currentTime - lastChangeTime > duration)
    {
        // we have to remember this time
         lastChangeTime = currentTime;
        // basically all leds are off except one
        for (byte index = 0; index < ledCount; index++)
        {
            if (index == sequence[pointer]) digitalWrite(ledPin[index], HIGH);
            else  digitalWrite(ledPin[index], LOW);
        }
        // now we'll deal with the pointer
        // use a programming calculator to see how this works. It's cool.
        pointer = ++pointer % sequenceCount; // this will roll the pointer over to display the sequence again
    }
}


I haven't compiled or tested this but it should work. Notice that you could add leds or sequences to those arrays and the sketch will adjust because I calculate the array size. The way you've done it with constants means that you would have to edit any for loop that accesses those arrays with the new max value. In mine the only constant I use is 0. When the code gets large a constant can be a magic number or a number with no context. It could be a mystery where 6 comes from rather than sequenceCount which gives the number context.

Does that make sense or am I rambling? LOL
24  Using Arduino / Programming Questions / Re: I Get These Errors? on: April 12, 2013, 10:09:47 am
You are also attempting to return a value in a function you have declared as void. A void function isn't expecting to return a value. You are trying to return pingdec which is already global.

A good way to avoid mismatched brackets is to always type them out at the start. For example when I start a for statement I often type the following:

for()
{}

Then I hit ctrl-t for formatting, then I go back and put in code.
25  Using Arduino / Programming Questions / Re: Update LCD display when potentiometer changes on: April 11, 2013, 07:51:22 pm
Try using the sprintf() function to format your output. If you only want to update on a reading value change you just need to remember the last reading. If the last reading and the current reading aren't equal then update the display.

I went back and read the code. You should declare lastoutputValue globally. Currently it's scoped only to that display loop. You tried but that int spoils it all.
26  Using Arduino / Programming Questions / Re: What am I doing wrong with my code? on: April 06, 2013, 04:24:50 pm
Code:
   if digitalRead(PIR == HIGH){
      digitalWrite(PUMP, HIGH)};
      delay(1000);
    }

One of those closing braces doesn't belong. Use the IDE's format feature as you work to avoid this. Your code also needs some decent formatting to aid in readability.
27  Using Arduino / Programming Questions / Re: declaring multiple pins on include libraries on: March 26, 2013, 02:27:36 pm
You were already offered very clear examples of the syntax. If you didn't understand them it's because you don't grasp the concept. That tutorial I linked to clearly shows how to define an object and how to pass arguments to the constructor (complete with proper syntax). All you need to do is pass the pin values to the constructor when you instance your object. However, with Arduino you don't want to initialize the pins in the constructor you want to have a begin method called during setup.

If you are clear on the concept that's all you should need to know. I think you should read the tutorial. In about an hour all that would make sense. Including the syntax.
28  Using Arduino / Programming Questions / Re: declaring multiple pins on include libraries on: March 26, 2013, 12:30:32 pm
You should probably head over to cplusplus.com and read the tutorial on classes (http://cplusplus.com/doc/tutorial/classes/). In fact you should probably read the tutorials from the start.

When you get a grip on how to handle object oriented programming your problem will become simpler. I think this is one of those things where examples won't help much without knowledge of the underlying concepts.
29  Using Arduino / Programming Questions / Re: button trouble? on: March 16, 2013, 11:54:57 am
Use a variable to hold the state of the LED. When a button is pressed just invert the state of that variable and use it to control the LED each time through loop.

Code:
// I'd probably take this sort of approach

int ledState = LOW;  // declare a variable to keep our led's state

// In setup, right after setting the ledpin's mode write the ledState to it

digitalWrite (ledPin, ledState); //synchronise the variable to the real world

// Then in loop when a button press is valid flip the state of the variable

ledState = !ledState;

// then during loop just write the ledState each time

digitalWrite (ledPin, ledState);  //



Hope that makes sense and is helpful.
30  Using Arduino / Programming Questions / Re: Having trouble "bailing out" of a loop? on: February 28, 2013, 09:21:23 am
Quote
and it works! Over and over again. UNLESS i push that button. It exits the loop perfectly...just when i try to run the loop again, some reason, it immediately bails out. Now I have a 1 second delay before this loop executes so no buttons are being pressed...so whats going on here? J is reset to zero...ScanSteps is declaired far before the J= 0...what gives?

I think buttonState24 stays with the value LOW as you don't read the button for a return to the HIGH state.
Pages: 1 [2] 3 4 ... 16