Pages: [1]   Go Down
Author Topic: even counting script help  (Read 494 times)
0 Members and 1 Guest are viewing this topic.
adelaide
Offline Offline
Newbie
*
Karma: 0
Posts: 8
SAVE THE LASERS!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

heya all
 im a few months new to the arduino world  im into modifying show lasers and want to create a more "intelligent" show light that counts beats and alters pattern to music structure not just beat trigger .

my aim is to write a script that's an event counter  "not an event time timer"  so the basics im after a script that would sense a digital  input that goes low to high to low  it counts 1 then second low to high to low counts a count of 2 ... then to have a variable pre defined parameter of count limit  before an output change happens say a pwm % change so  eg , every 4 event counts it makes a pwm change 100% to 75% ect.  also it keeps counting while the  pwm is running a steady output till the next 4 events happen then it changes the output % again but also say be able to  change the event total counts to 8,16,32 events  . i understand the physical side and have worked out basic pwm control from messing with the sweep sketch  and already have the pwm out operating with a laser correctly  and the audio sensing running as a digital high low . I'm just not to sure on how to create an event counter or  how to use: "and , or , if" statements and their correct use .

any light to be shone on helping me understanding this would be greatly appreciated  thank you for reading 
Logged

*----[LASERS]----*

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 You should check the interrupt - http://arduino.cc/en/Reference/AttachInterrupt -

a simple example with the built in LED as light

Code:
int pin = 13;
volatile int counter = 0;

void setup()
{
  pinMode(pin, OUTPUT);
  attachInterrupt(0, irq, FALLING);  // HIGH -> LOW
}

void loop()
{
  if (counter == 4)
  {
    digitalWrite(pin, HIGH);
  }
  if (counter >10)
  {
    digitalWrite(pin, LOW);
    counter = 0;
  }
}

void blink()
{
  counter++;
}
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

adelaide
Offline Offline
Newbie
*
Karma: 0
Posts: 8
SAVE THE LASERS!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

well thankyou that does answer some questions  by breaking the code up and reading each parts purpose ill have to load this sketch up and have a go at messing with it to get the desired affects and work from there .. smiley greatly appreciated
Logged

*----[LASERS]----*

adelaide
Offline Offline
Newbie
*
Karma: 0
Posts: 8
SAVE THE LASERS!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

heya just a quick one  on line 5 of this code u you have put

"attachInterrupt(0, irq, FALLING);  // HIGH -> LOW
}"

 what is the purpose of the irq as it choks up as an error when loaded in arduino . in my understanding of this where u have placed irq that is to be the function title to be run when the interupt triggers also i have changed the FALLING statment to RISING to be able to use a positive 5v pulse as the signal     in  the great words of a short ranga "??? please explain ???" lol
cheers once again

Logged

*----[LASERS]----*

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25764
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
"attachInterrupt(0, irq, FALLING); 
Shouldn't that be "blink", not "irq"?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

adelaide
Offline Offline
Newbie
*
Karma: 0
Posts: 8
SAVE THE LASERS!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

lol im not sure as to this side of scripting as im still verry fresh and learning the hows of it all hence why i have started this thread to c if any one can help shine some light on my project of a "smarter " show light
Logged

*----[LASERS]----*

adelaide
Offline Offline
Newbie
*
Karma: 0
Posts: 8
SAVE THE LASERS!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i have just changed the "irq" to "blink" and it passed the compiler
Logged

*----[LASERS]----*

adelaide
Offline Offline
Newbie
*
Karma: 0
Posts: 8
SAVE THE LASERS!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

heya  this is what i have done so far with the script its working marvelously with the analog laser im using thank you for your assistance  just a few quick questions. the replacement of irq  to blink  . what are these to commands functions as i cant find them in the reference page at all yes blink is working in the script but i am  trying to work in a second interrupt pin for reset to "count 0" at any time for full laser blanking   just wondering how i could do this as i have tried to double up  on the input codes and change the listing for the next pin over to create a second count but it didnt work "guessing  there can only be only 1 total count tally " 


this is harder than i though it would be to learn from scratch ..lol




int pin = 9;// led out
volatile int counter = 0; //pin 2 interupt imput

void setup()
{
  pinMode(pin, OUTPUT);
  attachInterrupt(0, blink, RISING);  // HIGH -> LOW
}

void loop()
{
  if (counter == 1)
   for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
    // sets the value (range from 0 to 255):
    analogWrite(pin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect   
    delay(30);                           
  }
 
  if (counter == 2)
  {
  digitalWrite(pin,LOW);
  }
 
  if (counter == 3)
  {
  analogWrite(pin,15);
  }
 
  if (counter == 4)
  {
  digitalWrite(pin,LOW);
  }
 
   if (counter == 5)
  {
  digitalWrite(pin,HIGH); 
  }
   
   if (counter == 6 )
   {
   digitalWrite (pin ,LOW);
   }
   
    if (counter == 7)
  {
  analogWrite(pin,25);
  }
 
  if (counter == smiley-cool
  {
  digitalWrite(pin,LOW);
  }
 
  if (counter == 9)
  {
  analogWrite(pin,30);
  }
 
  if (counter == 10)
  {
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
    // sets the value (range from 0 to 255):
    analogWrite(pin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect   
    delay(30);                           
  }
  }
 
   if (counter == 11)
  {
  digitalWrite(pin,LOW); 
  }
   
   if (counter == 12)
   {
   digitalWrite (pin ,HIGH);
   }
     if (counter == 13)
  {
  analogWrite(pin,40);
  }
 
  if (counter == 14)
  {
  digitalWrite(pin,LOW);
  }
 
   if (counter == 15)
  {
  digitalWrite(pin,HIGH); 
  }
   
   if (counter == 16)
   {
   digitalWrite (pin ,LOW);
   }
   
    if (counter == 17)
  {
  analogWrite(pin,50);
  }
 
  if (counter == 18)
  {
  analogWrite(pin,128);
  }
 
  if (counter == 19)
  {
  digitalWrite(pin,LOW);
  }
 
  if (counter == 20)
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
    // sets the value (range from 0 to 255):
    analogWrite(pin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect   
    delay(30);                           
  }
 
   if (counter == 21)
  {
  digitalWrite(pin,HIGH);
  }
 
  if (counter == 22)
  {
  analogWrite(pin,70);
  }
 
  if (counter == 23)
  {
  digitalWrite(pin,HIGH);
  }
 
  if (counter == 24)
  {
  analogWrite(pin,80);
  }
 
   if (counter == 25)
  {
  digitalWrite(pin,LOW); 
  }
   
   if (counter == 26 )
   {
   digitalWrite (pin ,HIGH);
   }
   
    if (counter == 27)
  {
  analogWrite(pin,85);
  }
 
  if (counter == 28)
  {
  digitalWrite(pin,LOW);
  }
 
  if (counter == 29)
  {
  analogWrite(pin,95);
  }
 
  if (counter == 30)
 for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
    // sets the value (range from 0 to 255):
    analogWrite(pin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect   
    delay(30);                           
  }
 
   if (counter == 31)
  {
  digitalWrite(pin,LOW); 
  }
   
   if (counter == 32)
   {
   digitalWrite (pin ,HIGH);
   }
     if (counter == 33)
  {
  analogWrite(pin,105);
  }
 
  if (counter == 34)
  {
  analogWrite(pin,200);
  }
 
   if (counter == 35)
  {
  digitalWrite(pin,LOW); 
  }
   
   if (counter == 36)
   {
   digitalWrite (pin ,HIGH);
   }
   
    if (counter == 37)
  {
  analogWrite(pin,115);
  }
 
  if (counter == 38)
  {
  digitalWrite(pin,LOW);
  }
 
  if (counter == 39)
  {
  analogWrite(pin,125);
  }
 
  if (counter == 40)
  {
  analogWrite(pin,10);
  }
   
  if (counter == 41)
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
    // sets the value (range from 0 to 255):
    analogWrite(pin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect   
    delay(30);                           
  }
   
   if (counter  >= 42)
   {
     digitalWrite (pin,LOW);
     counter =0;
   }

}   
 

void blink()
{
  counter++;
}
Logged

*----[LASERS]----*

Pages: [1]   Go Up
Jump to: