Go Down

Topic: Simple Sketch help (Read 2689 times) previous topic - next topic

Josh martin

Sep 21, 2009, 03:37 am Last Edit: Sep 21, 2009, 03:41 am by jm82792 Reason: 1
It's been day 2 and I love using arduino,
best of all it's not as hard as I pictured.

I'm working on a simple stripped down version of an aquarium controller for smaller 20 gallon to maybe a gallon or so reef tanks,
so far it's going great, since I can rip apart tutorials, understand how to code better then implement that into my project.

One problem I see is that it won't globally effect the interger Loop,
like every pas of the "Of" doesn't permanently take 1 away from it.

Any help and suggestions,
or links would be greatly appreciated.

here is a simple sketch for an alarm tone.
Not sure why the compiler doesn't like it,
I have no previous coding knowledge but so far I'm learning quickly.

Basically it uses for to remove 1 from "time",
after a ton of cycles it will go low enough to go to tone 2.
Then after it hits the if it cycles over and over.

int speakerPin = 9;
int Time = 5000;
int Loop = 1;

void setup()  {
   pinMode(speakerPin, OUTPUT);

 if (Loop > 5000)
void loop()  
  // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 2550; fadeValue +=5)  
    // sets the value (range from 0 to 255):
    analogWrite(speakerPin, fadeValue);        
    // wait for 30 milliseconds to see the dimming effect    
   for Time-1=Loop);
// fade out from max to min in increments of 5 points:
  for(int fadeValue = 2550 ; fadeValue >= 0; fadeValue -=5)
    // sets the value (range from 0 to 255):
    analogWrite(speakerPin, fadeValue);        
    // wait for 30 milliseconds to see the dimming effect    
    for (Time+1=Loop);


Sep 21, 2009, 04:00 am Last Edit: Sep 21, 2009, 04:05 am by ReCreate Reason: 1
Is what you want to create a siren sound?(sorry if i am off XD)
Anyways, i see many things in your code wrong...i am trying to get it fixed now...
Edit: Ok...I can't get it to work, Explain what you want it to do and i will rewrite it...

Josh martin

Basically a two tone siren,
from what I can understand a tone is having the piezo turn on and off at a rapid rate, generating a tone.
Combine/switch between two tones,
while using PWM and you get a graduation between tones,
I was thinking I'd get a siren.


Yea, you have a number of syntax problems there..

What exactly are you trying to do?

Josh martin

Well now that I am thinking I had some pretty major fallacies.
I guess it's what comes with my minimal experince.

Many operators can't be used in the context I though,
so this would never work making it really cryptic/"what were you thinking!!!" now that I've done more reading

Basically I want to go from tone "cycle1"(delay, on delay on.....)
to "tonecycle2" somehow.

How do I make it so that after the code loops enough that it goes to the second piece of code, loops that for X number of times then go back to the first one and repeats over and over?


Sep 21, 2009, 05:16 am Last Edit: Sep 21, 2009, 05:20 am by ReCreate Reason: 1
Oh...Well if you want a siren, then i have done that.
Code: [Select]
#include <Tone.h>
Tone tone1;
int spkpin = 9;//the speaker (digital PWM)pin is 9
int note = 440;//the starting hertz
int minhz = note;//minimum hertz
int maxhz = note*2;//maximum hertz
int way  = 0;//0 and the tone goes lower, 1 and it goes higher
int changespeed = 1;//the speed of changing the note in miliseconds
void setup()

void loop()

 tone1.play(note);//plays the current note

 if (way==0)//if the way the tone is changing to is down
 note = note-5;//subtract 5 hertz from the note
 delay(changespeed);//delay it
 else//or if it is going up
 note = note+5;//add 5 hertz
 delay(changespeed);//delay it

 if (note<=minhz)//if the note is at the minimun hertz value
   way = 1;//set way to 1, thus making the tone go higher

  if (note>=maxhz)//if the note is at the maximum herts value
   way = 0;//set way to 0, thus making the tone go lower


You need the tone library to be installed though, you can get it from the arduino website in the libraries area.
Edit: Put wrong code XD sorry my bad, fixed

Josh martin

Thanks a bunch!
Well now that I know what libraries are,
I will spend less time on things that can be done much more easily :)


Haha Your welcome :)
Glad to help

Go Up