My first Project, Did I bite off more than I can Chew?

I am attempting my first Arduino project. I have gone through the Sparkfun Inventors Kit and successfully completed all the circuits. I now want to make/create/construct my first actual working ‘thing’.

I am unable to find anyplace to ask questions specific to my needs.

Is there anyway to attach a pdf file to a post in order to ask a question more clearly?

The project has three inputs and four outputs. but there are several/many (if,then, else) loops and I have ‘zero’ programming knowledge.

Can anyone give me any starting point?

Thank you very much.

There is tons of Arduino code examples and tutorials on the web. Did you search?

Check AdaFruit’s web site and there is a least one Arduino web site in addition to the tutorials at SparkFun.

If you need expert help, I recommend the Arduino official forum. Many members there with tons of knowledge.

http://arduino.cc/forum/index.php

I’d say Google is your best friend in this situation, though there are many resources I can think of. The [Arduino website has a lot of information, tutorials and examples regarding their libraries. [Adafruit has a rather extensive tutorial library for beginners.

Googling turns up a [NYU website that gives a tutorial on getting started with an Arduino. It also turns up another tutorial on [Lady Ada’s website, for getting your first sketch working.

And, if nothing else succeeds, you could post more detailed questions either on the Sparkfun forum, the Adafruit Forum, or the Arduino Forum (and there are plenty of other forums you could post to for more specific topics).

If you could discuss more about what your project goals/needs are, maybe we can give you a more useful push in the right direction.](Arduino Tutorial - Lesson 2 - Modifying the first sketch)](http://itp.nyu.edu/physcomp/Labs/DigitalInOut)](http://learn.adafruit.com/)](http://www.arduino.cc/)

Thank you for your assistance, I will describe what I am attempting to do. And how I am trying to go about it. Perhaps just some specific directions may be helpful.

I am attempting to ‘build’ a basic animal training contraption. A LED lights up, A tone sounds, A dog is supposed to hit a lever with it’s paw to get a treat. Basic lab rat training.

I want have the option to make the dog hit the lever multiple times, multiple speeds, multiple wait times, etc…)

My first part is to get the electronics to work with the programming, then build the physical item. One step at a time.

I have been able to make each of the individual components work ‘almost’ as I want them to, but when I combine them, I get terrible results. I am certain it has to do with naming my constants and my variables in the right location and order.

It’s basic process is this:

I have a reset switch (resetSwitch - digitalPin 7) that sets all to a zero state.

A potientiometer (delayPot - Analog Pin A0) determines the ‘delayTime’

After the ‘delayTime’ the ‘pawLight’ (Red LED - DigitalPin 13) lights up and the speaker (spk1 - digital pin 10) give three low pitched tones (“Ready”)

In the first iteration;

If the dog presses the ‘pawButton’ (Digital Pin 6) one yellow LED lights up (Shift Register Qa)and the Speaker gives one medium pitched tone. (“Good First Step”)

Next Iteration:

After the ‘delayTime’ the ‘pawLight’ lights up and the speaker give three low pitched tones (“Ready”)

If the dog presses the ‘pawButton’ A SECOND yellow LED (Shift Register Qb) lights up and the Speaker gives one medium pitched tone.

This loop occurs for each of the 5 yellow LEDs and on the 6th (The Red LED) the speaker gives two short high pitched tones and activates “servo1” (DigitalPin 9) (which will release a treat) (“Good Dog”)

Then the system stops until I hit the ‘resetButton’ and I can at that time change the ‘delayPot’ to alter the time inbetween 'Ready"

It has three inputs:

resetSwitch – DigitalPin 7

delayPot – A0

pawButton – DigitalPin 6

and Four Outputs (One being the Shift register)

pawLight – DigitalPin 13

Spk1 – DigitalPin 10

Servo1 – DigitalPin 9

Shift Register –

Data - DigitalPin 2

Clock – DigitalPin 3

Latch – DigitalPin 4

5 yellow LEDs - (Shift Register - Qa – Qe)

1 red LED - (Shift Register - Qf

I have connected all the components to the Arduino using two breadboards using the aforementioned connections. I believe I have the proper resistors (7@330 ohm & 2 @10K ohm) and I believe it will work with the proper code.

A pic is here:

https://twitter.com/txeclipse/status/28 … 24/photo/1

How do I write it?

Start a sketch from scratch and drop in snippets? I’ve tried that and after about two snippets, it goes wonky.

Some other way? I’ve done a tiny tiny bit of programming so I do understand it is all a bunch of ‘If/Then/Else’ loops.

Any assistance would be most appreciated.

The way Ive done things in the past is to break it down into smaller pieces, write the code for each one, and as you are satisfied with the operation, add additional code (for each additional piece of the project). Best bet is, since you have an idea of how it will function, start writing pseudo code, and that makes it a bit easier to transition over in to working code.

Is [this example sort of what you are trying to do?](http://www.instructables.com/id/The-Touch-module-a-robotic-dog-toy/#step1)

Yes, That is precisely what I am doing. Thank you for providing the link. It is an excellent staring point for me. Thank you again.

May I ask why you’ve added the “complexity” of a shift register ? Unless I’ve miscounted the Arduino has enough I/O to do what you want w/o adding in the extra circuitry. Simpler is generally better. You can do what the SR does but do it in software (unless your LEDs are particularly bright).

Separate from the above, let me try to get a better handle on your requirements.

0: you turn the trainer on and after some time, it’s “ready”. A “paw LED” and a tone announce this fact. Presumably if the dog paws the lever prior to being ready, nothing happens ? Is there also a time limit the dog has to paw the lever after it becomes “ready” ? Or is any time after “ready” OK ?

1: the dog paws the lever and a tone announces that it’s been done. Another LED (#1) comes on, the 1’st in a stage of 6. You didn’t say it but I believe the “paw LED” also goes out on the lever press. After some settable time delay the “paw LED” comes back on, a tone announces “ready” and the machine awaits a paw press of the lever.

2: the dog paws the lever and a tone announces that it’s been done. Another LED comes on (#2). The “paw LED” goes out on the lever press. After the time delay the “paw LED” comes back on, a tone announces “ready” and the machine awaits another paw press of the lever.

3: the dog paws the lever and a tone announces that it’s been done. Another LED comes on (#3). The “paw LED” goes out on the lever press. After the time delay the “paw LED” comes back on, a tone announces “ready” and the machine awaits another paw press of the lever.

4: the dog paws the lever and a tone announces that it’s been done. Another LED comes on (#4). The “paw LED” goes out on the lever press. After the time delay the “paw LED” comes back on, a tone announces “ready” and the machine awaits another paw press of the lever.

5: the dog paws the lever and a tone announces that it’s been done. Another LED comes on (#5). The “paw LED” goes out on the lever press. After the time delay the “paw LED” comes back on, a tone announces “ready” and the machine awaits another paw press of the lever.

6: the dog paws the lever and a tone announces that’s been done. The final (red) LED comes on (#6). The “paw LED” goes out on the lever press. A treat is dispensed and the machine now sits awaiting a reset. Presumably a reset can also be done at anytime in the aforementioned sequence. A reset (manual via human only ??) returns the machine to step 0.

To give you a hint on the coding … it sounds like once the machine is “ready”, it awaits a paw press. When that happens you sound a tone, turn off the paw light and increment a counter. Depending on the value in the counter, a number of LEDs are turned on. The machine delays a settable delay and then becomes “ready” again, lighting the “paw LED” and announcing it via a tone. Do this over and over upon each paw press until a the counter reaches 6 LEDs on. When counter = 6, the servo is commanded to dispense the treat. Lot’s of the same stuff happening over and over as if in a loop.

Mee_n_Mac, Thank you for your response.

For the first question, Why the Shift register, no real reason. For future enhancements and for me to learn it. Maybe too soon. Future enhancements may require different number/type of output (A tennis ball tosser, a laser pointer [for a cat], etc…) Again, maybe too much for me right now. And there maybe alternative solutions that are much easier that I am naive to. Please give me your thoughts.

On the process: 0-6 above.

0:

  • The time for the system to be ready, and the time between ‘paw press opportunities’ is decided by a potienometer adjustable by human. [Can one be a slight multiple of the other? (such as 5 seconds from first reset to first opportunity and 2.5 seconds between each successive opportunity.)]

  • Yes, if dog paws lever prior to light, no result.

  • ‘Is there a Time limit after it becomes ready’ - no, i don’t think so. I think anytime is good. but not thought about it.

1:

  • Yes, PawLight goes out when pressed to indicate ‘not active’

  • On each of the correct paw presses, in addition to the successive LEDs lighting up. Is it possible the ‘incremental’ reward tone increase in pitch?

2-5: Yes, exactly.

6: ‘reset’ yes - reset at anytime during sequence and only by human. (future enhancements for wireless reset, mobile reset, etc…? Possible?)

Yep, It is a bunch of loops, counters and sub-loops.

A couple of starter questions for me.

Can I / Should I …

declare all variables at the very beginning of the sketch? or must be within each loop?

My next step is to take your excellent description and boil it down to the code I believe it to be.

Again, Thank you.

I will be back at this later today. Take Care.

txeclipse:
[Can one be a slight multiple of the other? (such as 5 seconds from first reset to first opportunity and 2.5 seconds between each successive opportunity.)]

Yes, you're going to read in a voltage and multiply that by a scale factor to turn it into a time (msecs). No reason that scale factor can't be different for different tasks.

txeclipse:
Is it possible the ‘incremental’ reward tone increase in pitch?

Yes. Look at the *tone()* function. You should put a DC blocking cap inbetween the pin and the speaker and you should use a high enough impedance speaker that the pin's current drive capability (40 mA) is not exceeded. If that results in too low a volume, other circuitry can be used to buffer the pin from the speaker.

http://arduino.cc/en/Reference/HomePage

http://arduino.cc/en/Reference/Tone

txeclipse:
A couple of starter questions for me.

Can I / Should I …

declare all variables at the very beginning of the sketch? or must be within each loop?

Variables and constants must be declared somewhere. When declared within a separate function or within a for loop (or equivalent) those variables don't exist outside of that function or loop. As an example when you do this :
for(int i = 0; i < NN; i++){
...
}

… the variable i only exists and can be used within the braces. That allows you to use the same if statement elsewhere without having to change the i to j or k or …

OTOH if you wanted to use that i elsewhere, you’re out of luck. In your case I’d declare most stuff up top and make them global variables, usable anywhere in the program. The exceptions would be for indicies like I just illustrated. Variable storing times or states or things to be used multiple times in the program, make them global. Your program should be short enough that a profusion of too many global variables is not going to be a problem.

In order to make this, my first project easier, I am going to pull the shift register and decrease the number of LEDs.

I’ll let you know how it goes.

In practical terms, whats the difference between a speaker and a buzzer?

You should put a DC blocking cap in between the pin and the speaker and you should use a high enough impedance speaker that the pin’s current drive capability (40 mA) is not exceeded. If that results in too low a volume, other circuitry can be used to buffer the pin from the speaker.

I don’t even know where to begin to tell you how much I don’t understand this statement. :?

I have a 4 ohm 1 W speaker salvaged.

I know the 40mA and the 1 W are related… but that’s all I know… :frowning: :frowning:

Thanks

txeclipse:
In practical terms, whats the difference between a speaker and a buzzer?

A speaker takes an AC signal in and produces a sound. If that signal input is a single frequency, then you get a pure tone out. Generally a speaker will accept a range of frequencies and reproduce sounds over that range. Woofers work at "low frequencies" and tweeters at "high" frequencies and midrange speakers the tones inbetween.

A buzzer takes a DC voltage, a power supply voltage really, and makes a single sound. It has the AC signal (an oscillator) built into it. Different buzzers may make different tones, generally not pure tones, and you can’t change the frequencies it outputs.

If you want to make several different tones w/o having to buy buzzer for each, you want a speaker.

txeclipse:
I have a 4 ohm 1 W speaker salvaged.

I know the 40mA and the 1 W are related… but that’s all I know…

You need to learn some basics about electricity; voltage, current, resistance and power. This wiki is a good overall primer. The Hydraulic analogy part is a good section to read to get a basic grip on the relationship btw voltage, current and resistance.

http://en.wikipedia.org/wiki/Ohm%27s_law

Power is the rate at which something is produced or used. In electrical terms the power used by something, like a speaker, is the voltage across it squared divided by the resistance of the device. Alternate, equally good, expressions to calculate power exist. Your 1 watt speaker can accept a voltage up to a certain level before it gets too hot and fries up. Since speakers are “fed” with AC signals, the voltage across them is usually measured in a quantity known as volts RMS. You can Google that to learn more. To learn the Vrms limit for that speaker you can solve this:

1W = (Vrms * Vrms)/4ohms → Vrms (max) = 2.

You can calculate the current, also an RMS quantity, for that voltage from Ohms law.

V = IR or I = V/R → 2Vrms/4ohms = 0.5 Arms

I’m not worried about the Arduino hurting your speaker but the reverse. A speaker with a low ohms spec may try to draw too much current out of the Arduino pin and fry it up. How much current might that speaker draw ? Start with the worst case as determined by Ohms law, V = IR where V is the voltage (volts), I is the current (amps) and R the resistance (ohms). If the output pin goes to 5V and R = 4ohms, the peak current will try to be 5/4 = 1.25 amps. That’s a lot more than the 0.04 amp limit (40 mA) that can safely be output. So it’s not a good idea to use that speaker directly. You want something like this :

http://www.radioshack.com/product/index … Id=2062402 Also see this:

http://oomlout.com/a/products/ardx/circ-06

Most piezo speakers are higher impedance, 90+ ohms, but can only reproduce a limited range of frequencies. You could use the speaker you have but you need a “driver”. One simple “driver” would be to use the transistor circuit shown for the motor in this. Replace the motor and diode with your speaker and a blocking cap.

http://oomlout.com/a/products/ardx/circ-03/

Lastly on the DC blocking cap … you may have notice the piezo speaker example above did not use one. You can sometimes get away w/o one but it’s not a recommended idea. Speakers like AC signals, the don’t like a constant DC voltage across them. The 0 to 5 V signal the Arduino outputs is like an AC signal of +2.5V to -2.5V with a DC signal of +2.5V added in. A blocking cap removes the DC part and lets the AC go through to the speaker. You put it in series (in between) the Arduino pin and the speaker as shown here:

http://www.kpsec.freeuk.com/images/speakcap.gif

If you want to use the “driver” above I can be more specific re: that case.

txeclipse:
In order to make this, my first project easier, I am going to pull the shift register and decrease the number of LEDs. I’ll let you know how it goes.

Be aware that the analog pins can also be used a digital I/O pins. Just declare them as you would normally but use the A0 to Ax names. An Arduino has 14 digital I/O pins. Let's see how you might use them:

0 - in use for communication, don’t use it elsewhere

1 - in use for communication, don’t use it elsewhere

2 - speaker output

3 - servo output

4 - reset the machine switch input

5 - paw lever switch input

6 - ready LED output

7 - LED #1 output

8 - LED #2 output

9 - LED #3 output

10 - LED #4 output

11 - LED #5 output

12 - LED #6 output

13 - already used by onboard LED, avoid using when possible unless it’s to use the onboard LED

A0 - pot

You may re-arrange the usages above but it seems to me you can do what you originally intended if you still want to.

Finally to get you started, here some “pseudo code” (not real code, just something code-like) to give you a hint as to what I’d do. Declare and initialize your constants and variables and setup your pins as inputs and outputs. The main loop() might look something like this :

void loop(){
  Read the pot, set up time constants
  Read the reset switch
  If pressed do reset stuff (zero paw counter, LED settings, etc)
  Read the paw switch
  If paw switch pressed
    - increment paw count
    - If paw count = 1
      : turn on LED #1
      : turn off ready LED
      : set delay time #1
    - If paw count = 2
      : turn on LED #2
      : turn off ready LED
      : set delay time #2
    - If paw count = 3
      : turn on LED #3
      : turn off ready LED
      : set delay time #3
    - If paw count = 4
      : turn on LED #4
      : turn off ready LED
      : set delay time #4
    - If paw count = 5
      : turn on LED #5
      : turn off ready LED
      : set delay time #5
    - If paw count = 6
      : turn on LED #6
      : turn off ready LED
      : activate servo for reward
    - wait time delay (note: only happens if paw switch pressed)
}

There are way to simplify the coding of the above but 1’st get it working then make it pretty. :mrgreen:

EDIT : I forgot the “sound the tone” parts but you should know where to plug those in now.

Mee_n_Mac:
A buzzer takes a DC voltage, a power supply voltage really, and makes a single sound. It has the AC signal (an oscillator) built into it. Different buzzers may make different tones, generally not pure tones, and you can’t change the frequencies it outputs.

I have to disagree with you there. I have a high Decibal Piezo Buzzer (from an Industrial Alarm) and Ive managed to change tones, frequencies and generate pure tones using a basic Arduino Uno.

Ok, I am back at this after a weekend with family.

I am going to reduce the complexity because I am having too many failures at the big picture.

Thanks for your help.

I’ll be back.

Take Care