Show Posts
Pages: [1] 2
1  Using Arduino / Audio / Tone Library and Digital Potentiometer - Weird results on: March 14, 2011, 03:22:55 pm
I'm attempting to use the tone library and a digital potentiometer to programmatically change the volume of my tones.
I have not even hooked up the digital potentiometer yet. When I call the subroutine to enable volume change, I shouldn't get any volume change at all because I haven't hooked up the chip yet. I expect to get a tone in each ear and a corresponding delay for a total repeating duration of about 4 seconds. This works correctly in the left ear. However, in the right ear, I get a rising, then falling pitch for the same duration as the left ear. This may have something to do with the limitations of the Atmega 328. I'd be grateful if someone could explain why what I'm doing does not give expected results, and whether there is a workaround that will work with the digital potentiometer I've chosen.
Code:
/*********************************************************
Sketch: Tone_Test_With_Digital_Potentiometer
Author: Chris Sparnicht - http://low.li
Date: 2011.01.31
License: Creative Commons 2.5 Attrib. & Share Alike
Description: A binaural tone test for arduino.
Note: Make sure you have dual wheel potentiometer to reduce
the volume of the audio with your headset. If you don't,
you might damage your ear drums, your arduino or your headset.

*********************************************************/
/*********************************************************
Include the arduino tone library (you'll have to download
this from arduino.cc's playground, since it's not standard.
*********************************************************/
#include <Tone.h>
//Variables for tone generator
float binauralBeat[] = { 14.4, 11.1, 6.0, 2.2 };
Tone rightEar;
Tone leftEar;
float centralTone = 200; //We're starting at this tone and spreading the binaural beat from there.
int volumeRight = 3;
int volumeLeft = 11;

/*********************************************************
Setup defines pins and tones.
*********************************************************/
void setup()  {
  Serial.begin(9600);
  rightEar.begin(9);
  leftEar.begin(10);
  pinMode(volumeRight, OUTPUT);
  pinMode(volumeLeft, OUTPUT);
}

/*********************************************************
Main function - play the tones continuously in a loop
*********************************************************/
void loop()
{
  for (int i = 0; i < 4; i++){
  rightEar.play(centralTone - (binauralBeat[i]/2));
  leftEar.play(centralTone + (binauralBeat[i]/2));
  // Serial.print("Right Ear: ");
  Serial.print(centralTone - (binauralBeat[i]/2));
  Serial.print(" | ");
  // Serial.print(" | Left Ear: ");
  Serial.println(centralTone + (binauralBeat[i]/2));
//   delay(3000);
  crescendoDecrescendo();
  rightEar.stop();
  leftEar.stop();
  delay(1000);
  }
}

void crescendoDecrescendo(){
  for (int j = 0; j < 256; j++){
    analogWrite(volumeRight, j);
    analogWrite(volumeLeft, j);
    delay(7);
  }
  for (int j = 255; j > 0; j--){
    analogWrite(volumeRight, j);
    analogWrite(volumeLeft, j);
    delay(7);
  }
}
2  Using Arduino / Project Guidance / Re: Need an Arduino Tone Digital Volume Control Method on: February 16, 2011, 03:46:57 pm
I am toying with the idea of making a 10" x 10" board of footprints for $95 here, cutting them up hacksaw and adding square posts for breadboard use as I go.
https://www.internationalcircuits.com/layer_grid.php?cat_id=13
Looks like someone already has had a similar idea and is selling it on ebay.
http://cgi.ebay.com/SMD-CONVERTER-ADAPTER-PCB-SOT-MSOP-SIP-DIP-14-/120628243230?pt=LH_DefaultDomain_0&hash=item1c1600eb1e#ht_2710wt_905
3  Using Arduino / Project Guidance / Re: Need an Arduino Tone Digital Volume Control Method on: February 16, 2011, 03:20:39 pm

Alas, Dual Inline Package (DIP) availability is going away for many parts. Unless someone comes up with a line of really inexpensive breakout boards for the myriad of part types, the cost of prototyping will either rise with having to make  custom board for any layout, or just become cumbersome with the one-size to accomodate many parts kind of breakout boards.
I see what you mean about availability. Almost everything is now surface mount, and there all a lot more expensive.

8402, DIP package, with TL072 opamp for output buffering, sounds like a good plan for driving earbuds or other high impedance (as compared to 8 ohm) drivers.
For the price and availability, linear will have to do.

Again thanks.
4  Using Arduino / Project Guidance / Re: Need an Arduino Tone Digital Volume Control Method on: February 16, 2011, 01:05:06 pm
Sweet! There are quite a number to choose from even when narrowing it down to the ad8402.
Is there a particular one of those listed that you would suggest?
Just to be sure - this is still a linear rather than logarithmic solution, right?

You got me thinking about digital potentiometers, so I looked here to find out who sells in lots of one.
http://octopart.com/parts/search?q=digital+potentiometer
I might be able to buy a few other parts at the same time.

If nothing else, it will be interesting and hopefully entertaining to figure out which pin connects where.

Again thanks.
5  Using Arduino / Project Guidance / Re: Need an Arduino Tone Digital Volume Control Method on: February 16, 2011, 09:53:05 am
Are you getting any volume like that? 5V thru a 4.7K resistor, that only leaves 1mA to go thru a speaker.  If you have an 8 ohm speaker, that's only .008 milliWatts.  Not sure that'd even be audible.
Are you actually driving a speaker?  Or are you going into an amplifier?
If you were driving an 8 ohm speaker directly, you'd need a 120 ohm resistor in series to keep from blowing the output pin. 40mA & 8 ohm speaker is 12.8mW.

What you could do is filter a PWM output, use that to drive a Voltage Controlled Amplifier, run the tone output thru that to a speaker amplifier.
Let me see if I can look one up.
Hi CrossRoads,

Thank you for your in-depth replies. I greatly appreciate it. If it isn't abundantly clear yet, I'm a noob, so I apologize for not further narrowing the scope of my parameters.

I've adapted Mitch Altman's ATtiny brain machine to Arduino UNO. It's essentially a guided meditation device. I'm using two pins to generate distinct tones, one to each ear using stereo headphones. The separated tones might be 441 Hz in the right ear and 399 Hz in the left ear. The end result is a binaural beat of 2Hz as interpreted by the brain through the efforts of our wonderful corpus callosum.

Without any resistors at all, the sound output is enough to probably damage the earphones with continued use. (I used a very old rugged cheap pair of over-the-head earphones for testing. I suspect a pair of $10 silicone-insert types used for many mp3 players would have been blown on the first try.) The 4.7KOhm resistors step down the volume to the point where the highest amplification is merely annoyingly loud rather than ear-damagingly loud using an audio headset. The 10K dual audio potentiometer does a good job of further reducing sound to a user-preferred level to an audio headset for anyone with a reasonable hearing capacity.

I didn't know what a DAC was, so I looked it up and found a lot of interesting Digital-to-Analog Converters that may be more expensive than the original Arduino UNO itself. I'm probably wrong, but I got the impression that I'd be moving the actual tone generation to a different integrated circuit, out of the realm of the arduino tone library.

Specific tones generated by the arduino are imperative to the project. Given the sound levels on the output pins for the tones, I want to create a 'sound envelope' using (perhaps?) voltage-controlled volume that allows me to ramp the sound up gently to it's normal existing output level at the beginning of a session and ramp the sound back down at the end of a session.

The example you've very kindly posted looks like it's designed to amplify more than dampen what's already there. I'm not exactly sure it's the kind of thing I'm looking for, though it might be adapted.

I have $15 dollars in the form of a gift certificate from Radioshack down the street. If it helps, I already have a popular quad opamp I bought from Radioshack. Taking from what you suggested, I wonder if I could use two pwm pins as voltage controllers that could limit the volume of the tones generated on two non-pwm pins before it goes out through the user-controlled potentiometer.

I found this example and wondered if it might be something I could adapt to use with an opamp and some extra components. Do you think it would work, or is this for something else entirely? If I were to use a circuit like this, I imagine the pwm pin would have to be attached either to 8 or 4 in the below diagram. Again, I'd be grateful for feedback and suggestions.


6  Using Arduino / Project Guidance / Need an Arduino Tone Digital Volume Control Method on: February 15, 2011, 10:46:18 pm
Say I have a tone generated on pin 4. I set it to go through one 4.7K ohm resistor and then a 10K potentiometer before it gets to the speaker then completes to ground. So I have some analog volume control. However, regardless of my preferred overall volume as limited by my potentiometer and resistor, I want to ramp the volume up from zero at the beginning of a sequence of tones and then ramp it back down at the end of the series of tones within in a specified duration digitally. What component(s) can I use between pin 4 and my resistor/potentiometer setup that I will allow the arduino to ramp volume up and down digitally?
7  Using Arduino / Project Guidance / Re: Brain Machine for Arduino on: February 03, 2011, 12:00:21 pm
I went back to Mitch Altman's original code and used that as my base.
This is the result.
There is a short youtube video of the working arduino brain machine there.
8  Using Arduino / Project Guidance / Re: Brain Machine using existing headsets on: January 30, 2011, 06:28:32 pm
I've been looking around for "common cathode vs. common anode" information on LED's, and found that there is a much simpler way of approaching the use of my existing eyephones. This post on the adafruit forum was pretty insightful.

To that end, I set up a test sketch thus:
Code:
/*********************************************************
Sketch: Eyephone Test
Author: Chris Sparnicht - http://low.li
Date: 2011.01.31
License: Creative Commons 2.5 Attrib. & Share Alike
Description: A test for stereo led eye phones.
Notes:
  Common Anode Blink Test for existing LED Glasses
  Blinks two sets of LED's alternately in loop, using a common anode.
  When pins 5 and 6 are set to LOW, they emit light (on).
  When each pin is set to HIGH the no longer emit light (off).
*********************************************************/
 
 // Some variables:
int intensity[]={255, 127, 63, 31, 15};
int rintensity[]={15, 31, 63, 127, 255};
 
/*********************************************************
Setup defines LED outputs.
*********************************************************/
void setup() {               
  // initialize the digital pin as an output.
  // The LED's anodes :
  pinMode(5, OUTPUT);     // LED's on right side
  pinMode(6, OUTPUT);     // LED's on left side
}

/*********************************************************
Main function - blink the LED's continuously in a loop
*********************************************************/
void loop() {
  for (int i = 0; i < 5; i++) {
  // Two blinks on one side to confirm which pin is right or left.
  // Do this five times.
  digitalWrite(5, HIGH);   // Common Anode: Turn LED off.
  digitalWrite(6, LOW);    // Common Anode: Turn LED on.
  delay(250);              // wait for a second
  digitalWrite(5, LOW);   // Common Anode: Turn LED off.
  digitalWrite(6, LOW);    // Common Anode: Turn LED on.
  delay(250);              // wait for a second
  digitalWrite(5, HIGH);   // Common Anode: Turn LED off.
  digitalWrite(6, LOW);    // Common Anode: Turn LED on.
  delay(250);              // wait for a second
  digitalWrite(5, LOW);   // Common Anode: Turn LED off.
  digitalWrite(6, LOW);    // Common Anode: Turn LED on.
  delay(250);              // wait for a second
  // Single blink on second in order to verify both sides function.
  digitalWrite(5, LOW);    // Common Anode: Turn LED on.
  digitalWrite(6, HIGH);    // Common Anode: Turn LED off.
  delay(1000);              // wait for a second
  }
  // Now do some PWM to show it works in reverse for PWM too.
  for (int i = 0; i < 5; i++) {
      for (int i = 0; i < 5; i++) { // over the course of a second
        analogWrite(5, intensity[i]); // Right side: dim to bright
        analogWrite(6, 0); //
        delay(200);
      }
      for (int i = 0; i < 5; i++) { // over the course of a second
        analogWrite(5, 0); //
        analogWrite(6, rintensity[i]); // Left Side: bright to dim
        delay(200);
      }
  }
}
I set up a double blink for the LED's on the right side just to confirm that the pin I'd specified was on the correct side. The fact that the left side LED's emit light during the double blink on the right side confirms that LOW means "On" and HIGH means "Off" with a common anode situation. Likewise, the analogWrite values are reverse as well. It looks like no transistors are necessary for this part of the programming. I may just need to inverse pin values in the sketch. Cool!

This is how the test is laid out on a bred board:


I can't tell whether I'm learning electronics incidentally to programming or vice versa. Either way, this is excellent fun.

The next thing to tackle is a better method for the earphones.
9  Using Arduino / Project Guidance / Re: Brain Machine using existing headsets on: January 28, 2011, 10:57:29 pm
Eyephones: Instead of a "common ground" for the LEDs like you'd think, this has a "common source" - so, just use a couple of small NPN transistors (2n2222 would probably work fine), emitter to ground, collector to the pin for the LED set, and put a base resistor on the base (330 ohm would probably be a good start) and control it with an Arduino digital output.

Headphones: This actually sounds like there is a short somewhere - likely while the plug fits the jack, the plug is in some way custom so that both channels are being fed incorrectly (that is, the contacts interior to the jack are making contact incorrectly with the contacts on the plug). Not sure what you can do about this, except to ohm out the plug and jack, to verify the hypothesis, then if that is correct, look for another jack or another pair of headphones (or a matching plug for the jack, then replace the headphone plug).

Also - note that an Arduino shouldn't be used to try to directly drive speakers - that's a good way to blow Arduino pins. Instead, you need to run the Arduino's output into a small amplifier (like an LM386), then run its output to the headphone jack (you'll need one LM386 for each speaker).

 smiley
cr0sh,

Thanks for your reply.

Does the following circuit diagram come close to what you're suggesting for the eyephones?


As to the amplifier, I have a quad opamp. Could I use two subsets of pins from an opamp as easily as using two LM386's?

10  Using Arduino / Project Guidance / Arduino Brain Machine on: January 28, 2011, 05:41:55 pm
I have an old brain machine - a Synetic "Esprit" 'Computerized Relaxation and Mental Fitness System' that still works. It has 12 Presets and a very simple interface 4-button/6-LED project box interface. It uses a sunshades with four small ganged LED's per eye and a pair of stereo headphones (any set works fine). I want to recreate the brain machine shown here, but using my existing 'eyephones' and headphones.

I bought two stereo headphone jacks that fit the eyephone and the head phone plugs.

Eyephones:
This one was confusing enough that I decided to check out the stereo plug on the eyephones to see what works to light up the LED's. It turns out ground has to be in two places at once to light up both arrays when I test the plug directly, but that doesn't make much sense to me. This looks a little like charlieplexing, so should I use a third pin instead of ground? I know both eyes on the eyephones blink sometimes in tandem and sometimes alternately when used with the "Esprit", so that functionality should be surmountable with the arduino. If someone give me a hint or two about how I need to interface this with my pins so the output from pin 12 goes to the right eye only and pin 13 to the left eye only, both at the same time or alternately according to my preferences, I would be grateful.



Headphones:
These work, but not exactly as expected. For this experiment, I'm the ToneTest from the Arduino Tone library exactly as is. I've put one of the stereo jacks on the breadboard. The center pin is ground. The left jack pin goes to arduino pin 12 and the right jack pin goes to arduino pin 9. If I play 'a', I can hear 440 a coming mostly from the right ear. Play 's' to stop. Then play 'A', and I hear it mostly in the left ear. Play 'S' to stop. Then play 'a' and play 'D', and you can hear two tones louder and in both ears. In both instances, I'm hearing bleed-through, where the sound from one speaker can also be heard to some extent on the other speaker. I was hoping I'd be able to get just pin 9 out of the right ear and just pin 12 out of the left ear, without combining. When I remove the wire from the ground pin on the arduino, the sound is equally loud in both ears. I would have thought the sound would have stopped, since the circuit technically has no ground.

How can I make it so the sounds coming out of each pin are truly isolated from the sound coming from the other pin?

These are the things that

11  Forum 2005-2010 (read only) / Troubleshooting / EMF Meter - Questions about implementation on: January 17, 2011, 11:54:59 pm
I've tweaked the emf meter sketch found here:
http://www.hyundaiaftermarket.org/forum/index.php?/blog/3/entry-26-arduino-ef-meter
adapting the blogger's circuit diagram:

using the below sketch on the Arduino UNO:
Code:
/*
  Hertz based electric field meter for Arduino.  
  Free and Open Source Apache License release.
  Apache license is free and open source and simply requires notification
  to the author before derrivitive work.  adamoutler at hotmail dotcom.
  Based on a sketch found here:
  http://www.hyundaiaftermarket.org/forum/index.php?/blog/3/entry-26-arduino-emf-meter/
  and circuit diagram here:
  http://i236.photobucket.com/albums/ff111/DrivingTibNaked/leds.gif
*/

//A note on HZ, if you notice meter oscillations on a known steady
//  signal increase or decrease hertz by 1.  The oscillations are
//  caused when the signals are in near-perfect resonance.
float FrequencyToMonitor=59; //hertz


//setup pins
#define ANTENNA 10 //antenna pin
//10 LEDs on 16 different pins
// int LEDPin[]={ 2, 3, 4, 5, 6, 7, 8, 9, 11, 12 } ;
int LEDPin[]={ 12, 11, 9, 8, 7, 6, 5, 4, 3, 2 } ;
int LEDCount=10;    //number of LEDs

//setup initial global values
int LEDCounter=0;               //initial value of LEDs to display
int HalfPeriodMicroseconds=0;// initial value of halfwavelength

/*
   SETUP defines pins and performs POT(Power On Test) on LEDs
*/
void setup()  {
   double HalfPeriod=((1/FrequencyToMonitor)*.5);   //Half-period in seconds from frequency
   HalfPeriodMicroseconds=int(1000000*HalfPeriod);  //Convert period seconds to microseconds integer

   pinMode(ANTENNA, INPUT);                   //set antenna pin
   for (int Pin=0; Pin < LEDCount; Pin++)  { //initialize each LED
    pinMode(LEDPin[Pin], OUTPUT);          
    digitalWrite(LEDPin[Pin], HIGH);          //Turn the LED on
    delay(100);
    digitalWrite(LEDPin[Pin], LOW);          //Turn the LED back off
   }
   swingLEDs();         //perform LED check to verify LED functionality
}


/*
   LOOP checks for value of antenna to go high and then low
   if change is seen, then 2 LEDs are lit, otherwise total number
   of LEDS are decreased.  This allows for a magentic effect when
   near the field.
   http://www.hyundaiaftermarket.org/forum/index.php?/blog/3/entry-26-arduino-emf-meter/
*/
void loop(){
   int ANTENNAValue=0;    //sets and resets ANTENNAValue
   int OldANTENNAValue=0; // as well as OldANTENNAValue


   for ( int Pass=0; Pass < 4; Pass++ )  {//take 4 readings
    ANTENNAValue=digitalRead(ANTENNA);    //   from the antenna
    if ( ( ANTENNAValue == HIGH ) && ( OldANTENNAValue == LOW ) ) {
        incrementLED(); //If a change is detected increase twice
        incrementLED(); //   4 passes means 3 changes total
    } else if ( ( ANTENNAValue ==  LOW ) && ( OldANTENNAValue == HIGH ) ) {
        incrementLED(); //   This makes the meter rise by
        incrementLED(); //   a total factor of  2 and
    } else {
        decrementLED(); //   if there is no change it falls by 1
    }
        OldANTENNAValue=ANTENNAValue; //Log old value for next pass
        delayMicroseconds(HalfPeriodMicroseconds);  // half-wave pause before next reading
   }
   delayMicroseconds(HalfPeriodMicroseconds); //visual delay between reading passes
   delayMicroseconds(HalfPeriodMicroseconds); //twice to avoid extremely long numbers  
}


/*
FUNCTION swingLEDs tests the LEDs by lighting
them up sequentially.  Each LED is brought high
then low twice.
*/
void swingLEDs(){
        for (int Pin=0; Pin < LEDCount; Pin++)  {
        digitalWrite(LEDPin[Pin], HIGH);  // light LEDs from left to right
        delay(50);
   }
   for (int Pin=0; Pin < LEDCount; Pin++)  {
        digitalWrite(LEDPin[Pin], LOW);   // clear LEDs from left to right
        delay(50);
   }
   for (int Pin=LEDCount; Pin > -1; Pin--)  {
        digitalWrite(LEDPin[Pin], HIGH);  // light LEDs from right to left
        delay(50);
   }
   for (int Pin=LEDCount; Pin > -1; Pin--)  {
        digitalWrite(LEDPin[Pin], LOW);   // clear LEDs from right to left
        delay(50);
   }
}

/*
FUNCTION incrementLED increases the number
of LEDs lit by 1.  
*/
void incrementLED(){
    if ( LEDCounter <  LEDCount  - 1  ){ LEDCounter++;} //if not max increase
    digitalWrite(LEDPin[LEDCounter], HIGH); //light new LED
}

/*
FUNCTION decrementLED decreases the number
of LEDs lit by 1.
*/
void decrementLED(){
   digitalWrite(LEDPin[LEDCounter], LOW); //turn off current LED
   if ( LEDCounter >= 0 ){ LEDCounter-- ;} // if not zero, decrease
}

Everything works as expected. Woohoo!

I noticed an odd thing when I first tried this
Code:
int LEDPin[]={ 11, 10, 9, 8, 7, 6, 5, 4, 3, 2 } ;
for my pins. Pin 10 would light up at the same time as pin 2. I cannot say why, so that was the main reason why pin 10 became my choice for the antenna. Also, I chose pin 10 for the antenna because Pins, 13 and 0 didn't seen to work well for this. I didn't try pin 1. Pins 12 and 11 Use blue LED's. Pins 9 and 8 are green. 7 and 6 are yellow. 5 and 4 are orange. 3 and 2 are red.

Dumb Noob Questions:
1. Why would pin 10 light up at the same time as pin 2 when used as shown in the one-line code LEDPin[] array as above, and why in general do some of the digital pins not work as expected? For instance, I couldn't get pin 13 to act as an antenna, but it's fine as an LED.

2. I tried two different size antennae (3" and 6") but could distinguish little difference in sensitivity. What is a good antenna length for sensing something at about 60Hz? Why aren't we using an analog-in for the antenna?

3. I notice that I had to isolate the UNO from the AC power supply when connecting the UNO via USB to the computer in order to get a valid reading. But when I put my hands near the antenna (within six inches), I get a high reading. It doesn't make sense that the human body should be cycling at 60 Hz. What's really happening?

4. It looks like the fluctuation matches my heart beat, but I'm guessing this is really the oscillation the original blogger noted about in the sketch. Correct?

5. I've used 220 Ohm resistors instead of the 440. Might this affect sensitivity? I'm guessing not. But if so, how?


Cheers.

12  Forum 2005-2010 (read only) / Troubleshooting / GSWA - Example 7B - p71 - Reading Fan Speed. on: January 10, 2011, 09:15:08 pm
From Getting Started with Arduino, page 71, there is an image showing a method for turning on and off a motor. There is no arduino sketch included and no control switch, so I created a sketch using a 12V 40mm 3-pin computer fan, mosfet, zener diode, resistor, momentary switch and a 9V power source. I'm successful and turning the fan on and off, but am not sure what I'm seeing with regards to output from the yellow wire. When the fan is off, the reading from the analog 0 pin reads 1023. When the fan is on, I get a range of numbers between 0 and about 100, averaging somewhere around 9. These don't look like fan speed in RPM to me. (See example serial monitor listing, patch diagram and sketch below.) When I remove the patch wire between A0 and the yellow fan wire completely, I get analog numbers on the serial monitor averaging around 350, regardless of whether the fan is spinning or not. So I'm not sure what I'm seeing.

I suspect I need an actual sample method for A0, but am not sure where to begin, nor how to incorporate it with other commands that include delays within the loop. I would be grateful for some suggestions about how to measure the input from A0 so I can get a valid RPM on my fan.

Other thoughts:
I tried using pin ~10 in place of A0, but got the same numbers as A0 without patch cord.
Since the original exercise alluded to PWM for changing fan speed, I thought I'd try
    analogWrite(CONTROL, 128); // turn Fan ON
to get half speed, but ended up with a very high pitched whine emitting from the fan. It did spin at a visibly slower rate. I'm unsure as to whether continued use in this way could damage the motor or the board. I'd like to know one way or the other about this too.

Cheers

Code:
fan speed: 1023 <-- fan off
fan speed: 1023
fan speed: 1023
fan speed: 0    <-- fan on
fan speed: 20
fan speed: 19
fan speed: 10
fan speed: 77
fan speed: 0
fan speed: 8
fan speed: 9
fan speed: 32
fan speed: 8
fan speed: 0
fan speed: 0
fan speed: 0
fan speed: 13
fan speed: 0
fan speed: 0
fan speed: 0
fan speed: 0
fan speed: 0
fan speed: 0
fan speed: 11
fan speed: 16
fan speed: 19
fan speed: 0
fan speed: 8
fan speed: 15
fan speed: 0
fan speed: 10
fan speed: 0
fan speed: 14
fan speed: 0
fan speed: 0
fan speed: 10
fan speed: 0
fan speed: 0
fan speed: 0
fan speed: 8
fan speed: 0
fan speed: 8
fan speed: 0
fan speed: 7
fan speed: 12
fan speed: 9
fan speed: 8
fan speed: 15
fan speed: 0
fan speed: 9
fan speed: 15
fan speed: 0
fan speed: 24
fan speed: 115
fan speed: 0
fan speed: 8
fan speed: 0
fan speed: 0
fan speed: 7
fan speed: 24
fan speed: 0
fan speed: 9
fan speed: 9
fan speed: 0
fan speed: 7
fan speed: 8
fan speed: 14
fan speed: 0
fan speed: 7
fan speed: 7
fan speed: 15
fan speed: 0
fan speed: 0
fan speed: 0
fan speed: 0
fan speed: 7
fan speed: 0
fan speed: 0
fan speed: 0
fan speed: 12
fan speed: 0
fan speed: 0
fan speed: 0
fan speed: 0
fan speed: 0
fan speed: 8
fan speed: 0
fan speed: 7
fan speed: 0
fan speed: 7
fan speed: 8
fan speed: 16
fan speed: 7
fan speed: 52
fan speed: 0
fan speed: 10
fan speed: 0
fan speed: 10
fan speed: 15
fan speed: 0
fan speed: 0
fan speed: 10
fan speed: 13
fan speed: 11
fan speed: 19
fan speed: 0
fan speed: 96
fan speed: 0
fan speed: 8
fan speed: 0
fan speed: 20
fan speed: 79
fan speed: 25
fan speed: 9
fan speed: 20
fan speed: 8
fan speed: 14
fan speed: 0
fan speed: 8
fan speed: 8
fan speed: 1023 <-- fan off
fan speed: 1023
fan speed: 1023
fan speed: 1023
fan speed: 1023
fan speed: 665  <-- remove wire from A0
fan speed: 426  <-- ???
fan speed: 351  <-- ???
fan speed: 344
fan speed: 338
fan speed: 342
fan speed: 337
fan speed: 340
fan speed: 336
fan speed: 340
fan speed: 336

board patch diagram:


sketch:
Code:
// Example 07B: Detect Analog Sensor and send to Serial Port

#define SENSOR 0 // The pin for the Fan Sensor
#define CONTROL 6 // Control Pin
#define BUTTON 7 // Start and Stop Button

int motorspeed = 0; // Variable used to store the value coming from the sensor.
int val = 0; // State of Pin 6
int old_val = 0; // This variable stores the previous value of "val".
int state = 0;  // 0 = CONTROL off while 1 = CONTROL on
int delayTime = 5000; // Five seconds in milliseconds.
int thisTime = 0; // A counter to compare with delayTime.

void setup() {
  // pinMode(SENSOR, OUTPUT); // tried PWM pin 10, didn't work. Same as no A0 patch.
  pinMode(CONTROL, OUTPUT); // tell Arduino LED is an output
  pinMode(BUTTON, INPUT); // and BUTTON is an input.
}

void loop(){
    val = digitalRead(BUTTON); //read input value and store it.
  
  // Check if there was a transition
  if ((val == HIGH) && (old_val == LOW)) {
    state = 1 - state; // turn LED ON.
    // delay(10); // debounce delay.
  }
  
  old_val = val; // val is now old. Let's store it.
  
  if (state == 1) {
    digitalWrite(CONTROL, HIGH); // turn Fan ON.
    // analogWrite(CONTROL, 128); // turn Fan ON.
    // Tried this but got a high-pitched whine from the fan. Probably not good.
  } else {
    digitalWrite(CONTROL, LOW); // turn Fan ON.
  }

  thisTime = thisTime + 100; // Increment current delayTime.
  motorspeed = analogRead(SENSOR); // Read the value from the sensor.
  if (thisTime > delayTime){
  // Write to serial port every time thisTime is greater than delayTime.
  Serial.begin(9600); // Open serial port to send data to computer at 9600 bps.
  Serial.print("fan speed: "); // Send this text.
  Serial.println(motorspeed); // Send the value and a line feed.
   thisTime = 0; // Reset our counter.
  }
  delay(100); // 1/10th second delay.
}
13  Forum 2005-2010 (read only) / Troubleshooting / Re: GSWA - Example 07 - P69 - variable not working on: January 07, 2011, 06:05:09 pm
I changed it to a float. It works now, though maybe not as expected.
I've certainly got a lot to think about. Thanks!


Code:
val = 52 | ravgval = 39 | hval = 53 | intensity = 13.00 | brightness = 2 | thisTime = 5028
val = 52 | ravgval = 39 | hval = 53 | intensity = 13.00 | brightness = 2 | thisTime = 5044
val = 65 | ravgval = 41 | hval = 65 | intensity = 16.00 | brightness = 4 | thisTime = 5130
val = 224 | ravgval = 128 | hval = 224 | intensity = 56.00 | brightness = 49 | thisTime = 5126
val = 233 | ravgval = 180 | hval = 233 | intensity = 58.00 | brightness = 53 | thisTime = 5428
val = 481 | ravgval = 188 | hval = 481 | intensity = 120.00 | brightness = 227 | thisTime = 5168
val = 476 | ravgval = 283 | hval = 481 | intensity = 119.00 | brightness = 225 | thisTime = 5732
14  Forum 2005-2010 (read only) / Troubleshooting / Re: GSWA - Example 07 - P69 - variable not working on: January 07, 2011, 05:52:01 pm
I realized just as you were replying.
Thanks - it clears up a lot.
Is there an easy work around for int, or should I try float?

PEMDAS - Parentheses are your friends, eh?
15  Forum 2005-2010 (read only) / Troubleshooting / Re: GSWA - Example 07 - P69 - variable not working on: January 07, 2011, 05:17:21 pm
PaulS - Thank you for your reply.

I might not be understanding what you mean about intensity.
Intensity calculates on the fly to be somewhere between 50 and 30.
Intensity must always be 255 or less since it's val/4.
Intensity divided by 254 gives me a percentage.
I'm actually multiplying that percentage by the highest-recently-found-val 'hval', not 254.
If I take my data into excel:

[glow]brightness[/glow] = (intensity/254)*hval
val = 203 | ravgval = 181 | hval = 203 | intensity = 50 | brightness = [glow]0[/glow] | thisTime = 5154
(50/254)*203 = [glow]40[/glow] <-- Expected result

val = 207 | ravgval = 193 | hval = 208 | intensity = 51 | brightness = [glow]0[/glow] | thisTime = 5366
(51/254)*208 = [glow]42[/glow] <-- Expected result

val = 202 | ravgval = 194 | hval = 208 | intensity = 50 | brightness = [glow]0[/glow] | thisTime = 5236
(50/254)*208 = [glow]41[/glow] <-- Expected result

val = 204 | ravgval = 194 | hval = 208 | intensity = 51 | brightness = [glow]0[/glow] | thisTime = 5296
(51/254)*208 = [glow]51[/glow] <-- Expected result
 
val = 131 | ravgval = 175 | hval = 208 | intensity = 32 | brightness = [glow]0[/glow] | thisTime = 5008
(32/254)*208 = [glow]26[/glow] <-- Expected result

I suspect you may be right about ints, but when I do the math as shown above, I get these results completely different from what arduino is showing me.

Int seems to be handling my rolling mock average as expected:
Code:
ravgval = ((14*ravgval)+val)/15; // Mock Rolling Average.
Why not brightness?
Pages: [1] 2