Show Posts
Pages: [1]
1  Using Arduino / Networking, Protocols, and Devices / Re: RFID: RC522 on: January 21, 2014, 10:14:12 am
I'm just waiting for mine to arrive, could you post a link to the code you found?

thanks!
2  Using Arduino / Programming Questions / Re: program wont return to loop from interrupt. on: June 30, 2013, 12:36:27 pm
if anyone is interested, basically the circuit currently fades through various colours on a $50 ebay "dmx 64 par can" when set below 130bpm. when set above 130bpm I'm going to program more rapid patterns with strobing etc
The colours are set to 12 specific colour combinations which look cool, as using random combinations looked pretty shit. ie

Code:
 {255,0,0}, //strong red
  {0,255,0}, // strong green
  {0,0,255},  // strong blue
  {255,255,0}, // fluro green
  {255,0,22}, // pink
  {255,50,0}, // sunset orange
  {146,51,253}, //dusk
  {1,165,132},  // sky blue
  {1,51,255},  // pool blue
  {146,0,36},  //pinky purple
  {204,0,255}, //purple
  {252,26,3},  //dawn
  {1,25,1}  // mint

Code:
#include <TrueRandom.h> //uses analog 0
#include <DmxSimple.h>

int fadeab = 0;
long reda = 0;
long redb = 0;
long greena = 0;
long greenb = 0;
long bluea = 0;
long blueb = 0;
int bright = 50;
float fader = 0;
float fadeg = 0;
float fadeb = 0;
int i = 0;
const int buttonPin = 2;
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 100;    // the debounce time; increase if the output flickers
volatile int bpm = 0;
volatile unsigned long t1 = 0;
volatile unsigned long t2 = 0;
unsigned long beattime = 0;
byte colours[13][3]={
  {255,0,0}, //strong red
  {0,255,0}, // strong green
  {0,0,255},  // strong blue
  {255,255,0}, // fluro green
  {255,0,22}, // pink
  {255,50,0}, // sunset orange
  {146,51,253}, //dusk
  {1,165,132},  // sky blue
  {1,51,255},  // pool blue
  {146,0,36},  //pinky purple
  {204,0,255}, //purple
  {252,26,3},  //dawn
  {1,25,1}  // mint
  
};  
int a = 0;
int b = 0; // labels for array in use


void setup() {
  attachInterrupt(0, bpmset, HIGH);
  pinMode(buttonPin, INPUT);
  DmxSimple.maxChannel(8);
  DmxSimple.write(2, 0);   //strobe
  DmxSimple.write(3, 10); //aux func
  DmxSimple.write(4, 100); //aux func
  DmxSimple.write(1, 200); //main
  Serial.begin(9600);

}

void bpmset(){
  if ((millis() - lastDebounceTime) > debounceDelay) { //creates a 100 millisecond requirement from last press to avoid debounce, where millis is a contant counter
    if(t1 == 0 || millis() - t1 > 2000){ // each 2 seconds the button sets t1, for less than 2.5 seconds t2 is set
      t1 = millis();
    }
    else{
      t2 = millis();
      bpm = 60000UL/(t2 - t1);
      if(bpm < 0 || bpm > 300) bpm = 0;
    }

  }
  lastDebounceTime = millis();

}

void loop(){
  if((t2-t1) > 0 && (t2-t1) < 5000) beattime = (t2-t1);
  if( bpm <= 130 ){            // slow fade pattern for low bpm values
    Serial.println(bpm);
    DmxSimple.write(1, 255); //main
    int x = 1;
    for (int i = 0; i > -1; i = i + x){ // i is a percentile which fades up to 100
       if (i == 0) {
        a = TrueRandom.random(12);
        reda  = colours[a][0];
        greena  = colours[a][1];
        bluea  = colours[a][2];
        Serial.print("a = ");
        Serial.println(a);
        }        
      fader = i*reda/100 + (float)(100 -i)/100*redb; //
      fadeg =  i*greena/100 + (float)(100 -i)/100*greenb; //
      fadeb = i*bluea/100 + (float)(100 -i)/100*blueb; //
      DmxSimple.write(5,(int) fadeb); //blue
      DmxSimple.write(6,(int) fadeg); //green
      DmxSimple.write(7, (int) fader); //red
      delay(50);
      if (i == 100){
        x = -1;
        b = TrueRandom.random(12);
        redb  = colours[b][0];
        greenb  = colours[b][1];
        blueb  = colours[b][2];
        Serial.print("b = ");
        Serial.println(b);
      }
    }
  }

  if(bpm > 130){
    Serial.print(" bmp = ");
    Serial.println(bpm);
    /*
    DmxSimple.write(5, 255); //blue
    DmxSimple.write(6, 150); //green
    DmxSimple.write(7, 0); //red
    DmxSimple.write(1, 255); // code to test sync of the bpm values
    delay(beattime);
    DmxSimple.write(1, 5);
    delay(beattime);
    */
    
    /*
    //finding good values with linear pots
    int mapr =  map( analogRead(5), 15, 1020, 0, 255 );
    int r = constrain(mapr,0,255);
    int mapg = map( analogRead(3), 15, 1020, 0, 255 );
    int g = constrain( mapg,0,255);
    int mapb = map( analogRead(4), 15, 1020, 0, 255 );
    int b = constrain( mapb,0,255);
    DmxSimple.write(5,b); //blue
    DmxSimple.write(6,g); //green
    DmxSimple.write(7,r); //red
    Serial.print("r = ");
    Serial.print(r);
    Serial.print(" g = ");
    Serial.print(g);
    Serial.print(" b = ");
    Serial.print(b);
    delay(600);
    */
    
    /*
    // manual colour array
    int mapr =  map( analogRead(5), 15, 1010, 0, 12 );
    int b = constrain(mapr,0,12);
    DmxSimple.write(5,colours[b][2]); //blue
    DmxSimple.write(6,colours[b][1]); //green
    DmxSimple.write(7,colours[b][0]); //red
    Serial.println(" array row = ");
    Serial.print(b);
    delay(600);
    */
    
    for(int i=0;i < 13; i++){
    DmxSimple.write(5,colours[i][2]); //blue
    DmxSimple.write(6,colours[i][1]); //green
    DmxSimple.write(7,colours[i][0]); //red
    Serial.print(" array row = ");
    Serial.println(i);
    delay(600);
    }
  }
}
3  Using Arduino / Programming Questions / Re: program wont return to loop from interrupt. on: June 30, 2013, 08:58:57 am
got around it yay!
 
Code:
if((t2-t1) > 0 && (t2-t1) < 5000) beattime = (t2-t1);
4  Using Arduino / Programming Questions / Re: program wont return to loop from interrupt. on: June 30, 2013, 08:16:31 am
I haven't been working on this project for ages, but I finally got back to it. The replies are much appreciated, I've modified the code as suggested, but I'm still having problems...
The code will work fine, and I can go between the two main sections of the main loop, above and below 130bpm. But if I set the bpm above 130, the code will freeze if I press my interrupt button again. Im going to try work around this, but here is my current code if anyone has any tips for getting around this.
I thought the problem may have been because my delay in the part of the code for above 130bpm references t1 and t2 which are set in the interrupt...

cheers,
 Harry

Code:
#include <TrueRandom.h>
#include <DmxSimple.h>

int fadeab = 0;
long reda = 0;
long redb = 0;
long greena = 0;
long greenb = 0;
long bluea = 0;
long blueb = 0;
int bright = 50;
float fader = 0;
float fadeg = 0;
float fadeb = 0;
int i = 0;
const int buttonPin = 2;
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 100;    // the debounce time; increase if the output flickers
volatile int bpm = 0;
volatile unsigned long t1 = 0;
volatile unsigned long t2 = 0;
unsigned long beattime = 0;


void setup() {
  attachInterrupt(0, bpmset, HIGH);
  pinMode(buttonPin, INPUT);
  DmxSimple.maxChannel(8);
  DmxSimple.write(2, 0);   //strobe
  DmxSimple.write(3, 10); //aux func
  DmxSimple.write(4, 100); //aux func
  DmxSimple.write(1, 200); //main
  Serial.begin(9600);

}

void bpmset(){
  if ((millis() - lastDebounceTime) > debounceDelay) { //creates a 100 millisecond requirement from last press to avoid debounce, where millis is a contant counter
    if(t1 == 0 || millis() - t1 > 2500){ // each 2.5 seconds the button sets t1, for less than 2.5 seconds t2 is set
      t1 = millis();
    }
    else{
      t2 = millis();
      bpm = 60000UL/(t2 - t1);
      if(bpm < 0 || bpm > 300) bpm = 0;
    }

  }
  lastDebounceTime = millis();

}

void loop(){
  beattime = (t2-t1);
  if( bpm <= 130 ){            // slow fade pattern for low bpm values
    Serial.println(bpm);
    DmxSimple.write(1, 200);

    int x = 1;

    for (int i = 0; i > -1; i = i + x){ // i is a percentile which fades up to 100
      DmxSimple.write(1, 255); //main

      if (i == 100) {
        x = -1;
        reda  = TrueRandom.random(1,254);
        greena  = TrueRandom.random(1,254);
        bluea  = TrueRandom.random(1,254);
      }
      fader = i*redb/100 + (float)(100 -i)/100*reda; //
      fadeg =  i*greenb/100 + (float)(100 -i)/100*greena; //
      fadeb = i*blueb/100 + (float)(100 -i)/100*bluea; //
      DmxSimple.write(5,(int) fadeb); //blue
      DmxSimple.write(6,(int) fadeg); //green
      DmxSimple.write(7, (int) fader); //red
      delay(20);
      if (i == 0){
        redb  = TrueRandom.random(1,254);
        greenb  = TrueRandom.random(1,254);
        blueb  = TrueRandom.random(1,254);
      }
    }
  }

  if(bpm > 130){
    Serial.println(bpm);
    DmxSimple.write(5, 255); //blue
    DmxSimple.write(6, 150); //green
    DmxSimple.write(7, 0); //red
    DmxSimple.write(1, 255); // code to test sync of the bpm values
    delay(beattime);
    DmxSimple.write(1, 5);
    delay(beattime);
  }
}
5  Using Arduino / Programming Questions / program wont return to loop from interrupt. on: January 28, 2013, 09:43:30 am
Ive got some code to figure out audio beats per min from a button press interrupt. I can get the bpm fine into void loop() but my problem is that within loop() I have two while loops depending on bpm values,
Code:
while( bpm <= 130 ){
...
}
while(bpm > 130){
...
}
if I change change from above 130 back down below 130 bpm using the interrupt, the code will stop running and seems to stay within the interrupt... wtf? any clues?
 heres my code:
Code:
#include <TrueRandom.h>
#include <DmxSimple.h>

int fadeab = 0;
long reda = 0;
long redb = 0;
long greena = 0;
long greenb = 0;
long bluea = 0;
long blueb = 0;
int bright = 50;
float fader = 0;
float fadeg = 0;
float fadeb = 0;
int i = 0;
const int buttonPin = 2;
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers
volatile int bpm = 0;
volatile float t1 = 0;
volatile float t2 = 0;


void setup() {
  
  // DmxSimple.usePin(2);
  //pinMode(3, INPUT);
  attachInterrupt(0, bpmset, HIGH);
  pinMode(buttonPin, INPUT);
  DmxSimple.maxChannel(8);
  DmxSimple.write(2, 0);   //strobe
  DmxSimple.write(3, 10); //aux func
  DmxSimple.write(4, 100); //aux func
  DmxSimple.write(1, 200); //main
  Serial.begin(9600);
  
}

void bpmset(){
  if ((millis() - lastDebounceTime) > debounceDelay) {
    if(t1 == 0 || millis() - t1 > 5000){
      t1 = millis();
      Serial.print("t1 = ");
      Serial.println(t1);
    }
    else{
      t2 = millis();
      Serial.print("bpm = ");
      bpm = 1/(t2 - t1)*60000;
      if(bpm < 0 || bpm > 300) bpm = 0;
      Serial.println(bpm);
    }
    
  }
  lastDebounceTime = millis();

}

void loop(){
  
  while( bpm <= 130 ){
    Serial.println(bpm);
    DmxSimple.write(1, 200);
    
   int x = 1;
  
  for (int i = 0; i > -1; i = i + x){
    
      //sensorValue = analogRead(A1);            
      //outputValue = map(sensorValue, 0, 1023, 0, 255);    // map it to the range of the analog out:
      DmxSimple.write(1, 255); //main
    
    if (i == 100) {
      x = -1;
    //  randomSeed(analogRead(0));
      reda  = TrueRandom.random(1,254);
    //  randomSeed(analogRead(0));
      greena  = TrueRandom.random(1,254);
    //  randomSeed(analogRead(0));
      bluea  = TrueRandom.random(1,254);
    }
    fader = i*redb/100 + (float)(100 -i)/100*reda; //
    fadeg =  i*greenb/100 + (float)(100 -i)/100*greena; //
    fadeb = i*blueb/100 + (float)(100 -i)/100*bluea; //
    DmxSimple.write(5,(int) fadeb); //blue
    DmxSimple.write(6,(int) fadeg); //green
    DmxSimple.write(7, (int) fader); //red
  // Serial.print((float)(100 -i)/100*reda); //  pow ( i,  -1);
   // Serial.print("    ");
  // Serial.println( i);
    delay(20);
    if (i == 0){
    //  randomSeed(analogRead(0));
      redb  = TrueRandom.random(1,254);
    // Serial.println(redb);
   //  randomSeed(analogRead(0));
     greenb  = TrueRandom.random(1,254);
 //    Serial.println(greenb);
  //   randomSeed(analogRead(0));
     blueb  = TrueRandom.random(1,254);
  //   Serial.println(blueb);
    }
  }
  }
  
  while(bpm > 130){
    Serial.println(bpm);
       redb  = TrueRandom.random(1,0);
     greenb  = TrueRandom.random(1,254);
     blueb  = TrueRandom.random(1,25);
     DmxSimple.write(1, 200);
     delay(t2-t1);
     DmxSimple.write(1, 15);
     delay(t2-t1);
  }
}
Pages: [1]