Pages: [1]   Go Down
Author Topic: Simple Sketch help  (Read 640 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Thanks,
Josh

Anyways,
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    
     delay(30);                            
    for Time-1=Loop);
}
else
{
// 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    
     delay(30);
     for (Time+1=Loop);
}

« Last Edit: September 20, 2009, 08:41:15 pm by jm82792 » Logged

From:0x000000
Offline Offline
Full Member
***
Karma: 0
Posts: 213
sudo make me a (bread+ham+lettuce+cheese+bread)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Is what you want to create a siren sound?(sorry if i am off smiley-lol)
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...
« Last Edit: September 20, 2009, 09:05:01 pm by ReCreate » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 60
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

What exactly are you trying to do?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

From:0x000000
Offline Offline
Full Member
***
Karma: 0
Posts: 213
sudo make me a (bread+ham+lettuce+cheese+bread)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh...Well if you want a siren, then i have done that.
Code:
#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()
{
  tone1.begin(spkpin);
}

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 smiley-lol sorry my bad, fixed
« Last Edit: September 20, 2009, 10:20:43 pm by ReCreate » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Logged

From:0x000000
Offline Offline
Full Member
***
Karma: 0
Posts: 213
sudo make me a (bread+ham+lettuce+cheese+bread)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Haha Your welcome smiley
Glad to help
~ReCreate
Logged

Pages: [1]   Go Up
Jump to: