Show Posts
Pages: [1] 2 3
1  Using Arduino / Programming Questions / Re: problem with receiving variables after parsing on: September 08, 2011, 01:27:05 am
Code:
  gpsdatin=*ptr; // ptr points to $
You are assigning the address of the pointer to gpsdatin, not the contents pointed to. Get rid of the *.

This is nonsense. ptr is the address, *ptr is the char pointed to. Removing the * is an error of the hopefully-doesn't-compile variety.
2  Using Arduino / Programming Questions / Re: how to define 3 Inputs for switch/case on: May 18, 2011, 01:08:17 pm
Not that there's any need for a switch here... addition would do the job just as well.
3  Using Arduino / Project Guidance / Re: millis() overflow when? (UNO+22) on: March 19, 2011, 09:32:45 pm
Brain fart. Actually had meant to say "10 minutes a week" (around a 1000ppm error, although most seem to be within a few hundred) vs. the previous post's "10 minutes a day", but lost the thought before I posted smiley
4  Using Arduino / Project Guidance / Re: millis() overflow when? (UNO+22) on: March 18, 2011, 10:53:22 pm
Well, there's no calibration done for millis() -- it assumes that the CPU frequency is its nominal value. And the crystals on the Arduino boards aren't very good ones. But no, they're not that bad. A lousy one might be out by as much as 1 second per week.
5  Using Arduino / Installation & Troubleshooting / Re: Tried 5+ times to update Atmega8U2 on SMD Uno with no luck on: March 13, 2011, 05:48:25 pm
Sounds like you programmed the 8U2 just fine (and yes, it's reasonable for it to program very quickly) -- but that doesn't fix the bootloader bug at all, which is a completely unrelated issue.
6  Using Arduino / Programming Questions / Re: Tasks with no delay on: March 12, 2011, 12:58:38 pm
KirAsh4, in the code sample in your first post, you have a variable buttonCounter and a function that uses switch/case on that variable to set a different delay before changing the LEDs, and when the timeout expires, buttonCounter advances from 1, 2, 3, 4, back to 1, 2, 3, etc. This is very good, a basic example of a finite state machine that advances to a new state every time the button is pressed. Now, imagine a few changes to your code.

Change #1: Rename the "buttonCounter" variable to "state" or "animationState" or something -- pretty soon it won't only be counting button presses.

Change #2: Right now your switch/case is running every single time blinkLeds is called. Right now that's a harmless waste, but after Change #3 and Change #4 it will be a real problem. Move the entire switch block inside of the if (millis() - pause > prevMillis) if block. That will make it so that after running through the switch block, it won't run again until after the next delay finishes.

Change #3: Move the code that does "digitalWrite(blinker, digitalRead(blinker) == HIGH ? LOW : HIGH)" into each state -- or move it into a function and then call that function from each state. This seems like you're repeating yourself, but later on you'll make it so that each state isn't just blinking a single LED -- instead, each state will set a pattern of LEDs according to whatever animation you design.

Change #4: Make it so that a button press isn't the only thing that changes the state variable -- at the end of each state, right before the "break;", each state can set a new next state.

If you don't add any next-state assignments right away in Change #4, you should have code that still acts just like your original sample, even though it will be in a very different shape. But now there's something new you can do: have more states than button presses. For example, maybe State 1 goes to State 2 goes to State 3 which goes back to State 1, and they turn on three LEDs in a cycle. But when you press the button it will jump to State 4, which goes to State 5, which goes back to State 4. And when you press the button again it will go to State 6, which does something different. So each "pattern" can be multiple states that will keep repeating until you press the button and jump into a different pattern.

By adding logic inside of the states you can also make them more complicated -- you don't need to have one state for every combination of LEDs you want to light up. For example you could have a row of 8 LEDs with the lit LED "bouncing" back and forth with only two states. State 1 would do

Code:
case 1: /* Move right */
    Turn off LED n;
    n = n + 1;
    Turn on LED n;
    if (n == 8) {
      state = 2; /* Start moving left instead */
    } /* Else keep moving right */
    pause = 250; /* Or whatever */
    break;

And state 2 would do
Code:

case 2:
    Turn off LED n;
    n = n - 1;
    Turn on LED n;
    if (n == 1) {
        state = 1; /* Start moving right */
    } /* Else keep moving left */
    pause = 250; /* Or whatever, again */
    break;

So instead of the states being 1, 2, 1, 2, 1, 2, 1, 2, they'd be 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, ... 1 every time it moves right, and 2 every time it moves left. And the logic inside of the states changes between 1 and 2 when it's needed, saving you a bunch of code.
7  Using Arduino / Programming Questions / Re: Understanding this line " ? HIGH : LOW; " on: March 12, 2011, 12:25:04 pm
So, what is the difference between something being a method or just a function? Is it a method BECAUSE it is a function that is part of a class? Or are there other reasons? Would a C++ guide perhaps spell this part out?

In the general world of programming (not C++ specific), yes, a "method" is a function that is part of a class, and we can more specifically speak of "instance methods" (methods that are called on objects, i.e. instances of some class) and "class methods" (methods that are called on the class itself, without reference to any specific object). C++ also calls methods "member functions" (or to be painfully accurate "virtual member functions") and class methods "static member functions" for reasons that are historical and painfully silly. Java and C# also abuse the word "static" in this way. You should know the terms that your language uses, and use them when you need to be specific about things that are specific to your language, but in general you should be comfortable with the general terms. smiley
8  Using Arduino / Project Guidance / Re: built in serial to midi? on: March 11, 2011, 08:18:23 pm
http://arduino.cc/en/Tutorial/Midi

This works, I have made MIDI controllers with it and its fine. the issue is that you get non usb based MIDI so u need a MIDI in for the computer.

Eek. That may work because of tolerant hardware, but it's definitely wrong. The MIDI interface is a current-loop one and that tutorial is voltage-driving it. On the transmit side the worst that's likely to cause is that it just plain won't work, but if the device on the other end tries to send a response back and it has a particularly strong driver, and the arduino's resistance is too high, it could fry the arduino handily.
9  Using Arduino / Programming Questions / Re: How do I stop and exit out of a 'for loop' on: March 10, 2011, 09:04:40 pm
"counter = 5001" is silly. If you want to break out of the loop, use break. You should say what you mean, not any random thing that happens to work. smiley
10  Using Arduino / Programming Questions / Re: while(1){} - whaaaaaaat??? on: March 10, 2011, 09:02:27 pm
On a semi-related note, does anyone know if there's any reason why the Arduino IDE prevents loop() from being declared inline? Done right, it should save several microseconds per loop (function call overhead, and stack-manip overhead if loop uses no non-static vars), which could make a real difference on tight loops. As it is, people who want this have to write their own while(1) loop inside of loop().
11  Using Arduino / Programming Questions / Re: Understanding this line " ? HIGH : LOW; " on: March 10, 2011, 12:58:47 pm
Read it and find out. You have the book with the code in it, so you know more than we do. By the looks of it, it seems like it's meant to return true when the button state has changed, but without seeing the definition, I can only guess. You have a book with the definition in it, and probably a discussion in plain english of what it does, so you unlike me, you shouldn't need to guess.
12  Using Arduino / Project Guidance / Re: built in serial to midi? on: March 10, 2011, 12:44:48 pm
If the solution you have right now works, it's probably the best one. There are other ways, but one of them involves hardware work (adding drivers to convert between TTL serial on the Arduino and the current-loop mode that conventional MIDI uses), and the other one requires one of the newest models of Arduino and makes it much more difficult to upload new sketches (reprogramming the Atmega8U2 on an Uno or a Mega2560 board to act as a USB MIDI device instead of a USB CDC ACM device). Right now you have a solution that doesn't require changing anything, so as long as it's not giving you trouble or restricting what you can do, why not stick with it?
13  Using Arduino / Programming Questions / Re: turn vin pin on and off on: March 10, 2011, 12:32:59 pm
To clarify what James is saying here -- the power connector on the edge of the board and the Vin pin are electrically connected, with no kind of switch or logic between them, so there's nothing you can do in code to make them not-connected. You should only hook up to one of them.

As for your actual problem -- you could use a relay to turn the power to the speakers on and off, without interrupting the Arduino's power. You just need to put the relay in the right place smiley
14  Using Arduino / Programming Questions / Re: Understanding this line " ? HIGH : LOW; " on: March 10, 2011, 12:29:24 pm
I am not sure why they chose to implement such a seemingly advanced command in a book for beginners.

I agree.  I think the only time I've needed a bit-wise-and is when manipulating hardware registers.  There are certainly other examples: driving an LED matrix; displaying a graphic; unpacking boolean values.  But I can't think of any beginner examples.

Flag bitfields come up constantly: if (alarms & ZONE3_FIRE) or open(fd, O_WRONLY | O_EXCL), etc. etc. and unpacking structures, when some helpful protocol designer has decided to save bits (or registers) by putting the value you need in the third-through-fifth bits of the second byte of whatever. I certainly wouldn't recommend ignoring them.

In the real world, there are only two possible outcomes for a system that intends to "make it easy for non-programmers to program". Either the system is simple, in which case the users are at the mercy of the developers who decide what they can and can't do with it, or the system grows complex to the point where anything is possible in it -- in which case learning to use it becomes exactly as complex as learning to program the system it "replaces". Programming isn't a collection of mystic rituals for scaring outsiders; it's just the art of telling the computer what you want done in a way that will actually get it done. You can't remove what Fred Brooks would call "fundamental complexity" without removing the ability to get things done.
15  Using Arduino / Programming Questions / Re: unexpected result with millis on: March 10, 2011, 11:53:53 am
Two things:

1. Are you sure that your conditions using % are really doing what you want them to? They could be, but it seems very strange to have a loop that's running with so many different intervals at once.

2. The whole thing is broken if the loop ever takes more than one millisecond to run, which is very possible with so many conditions and so many port writes. What you need is to take into account all of the time that has passed between the previous loop and the current one, and change any pins that should have had their values changed in the intervening time. Better yet would be to get some help from the hardware counters -- but you're working with too many pins to make that easy. Ideally, you would only have 4 or 5 per Arduino, and you could use the PWM pins directly.
Pages: [1] 2 3