Show Posts
Pages: [1] 2 3 ... 5
1  Using Arduino / Project Guidance / Re: Digital Touch Pad for Guitar on: June 18, 2013, 10:10:44 am
second part of the code

Code:
Wire.send('f');
Wire.send(0xff);
Wire.send('h');
Wire.send(hue_val);
Wire.send(0xff);
Wire.send(0xff);
Wire.endTransmission();
Led=0;
Holdtch=1;
}
else {
if (Tch==1 and holdkp2==0){
midiCC(0xB0,92,0);
//Serial.println("touch off");
Tch=0;
}
if (Led==0){
Wire.beginTransmission(0x09);
Wire.send('f');
Wire.send(del_time);
Wire.send('p');
Wire.send(effect);
Wire.send(0x00);
Wire.send(0x00);
Wire.endTransmission();
//Serial.println("screen not touched LED effect");
    Led=1;
}
Holdtch=0;
}
//hold functions
if (buttonState != lastButtonState) {
if (pedal==0){//hold function for KP2
if  (buttonState == HIGH and lightState != HIGH and holdkp2==0){
if (Tch==0){
midiCC(0xB0, 92, 127);
//Serial.println("KP2 hold on");
Tch=1;
}
holdkp2=1;
}
else if  (buttonState == HIGH and lightState != HIGH and holdkp2==1){
if (Holdtch==0){
midiCC(0xB0, 92, 0);
//Serial.println("KP2 hold off");
}
holdkp2=0;
}
}
if (pedal==1){//hold function for KP3
if  (buttonState == HIGH and lightState != HIGH and holdkp3==0){
midiCC(0xB0, 95, 127);
//Serial.println("KP3 hold on");
holdkp3=1;
}
else if  (buttonState == HIGH and lightState != HIGH and holdkp3==1){
midiCC(0xB0, 95, 0);
//Serial.println("KP3 hold off");
holdkp3=0;
}
}
if (pedal==2){//hold function for Whammy
if  ((buttonState == HIGH) and Hld==0){
midiCC(0xB0, 0, 127);
//Serial.println("Whammy hold on");
Hld=1;
}
else if  ((buttonState == HIGH) and Hld==1){
midiCC(0xB0, 0, 0);
//Serial.println("Whammy hold off");
Hld=0;
}
}
}
lastButtonState=buttonState;
//permanent storage of variables

}

// return TRUE if touched, and coords in touchX and touchY
boolean touched(){
boolean touch = false;
pinMode(leftOutput, OUTPUT);
digitalWrite(leftOutput, LOW);     // set Left = ground
pinMode(rightOutput, OUTPUT);
digitalWrite(rightOutput, HIGH);   // set Right = Vcc
pinMode(topOutput, INPUT);         // Sets Top and Bottom to inputs
pinMode(bottomOutput, INPUT);      // to give them high impedence
delay(10);
touchY = analogRead(topInput) ;   // wait a bit, then read from Top
pinMode(bottomOutput, OUTPUT);
digitalWrite(bottomOutput, LOW); // Set Bottom to 0v
pinMode(topOutput, OUTPUT);
digitalWrite(topOutput, HIGH);  // set Top to +Vcc
pinMode(rightOutput, INPUT);
pinMode(leftOutput, INPUT);
delay(10);
touchX = analogRead(rightInput) ; // wait a bit, then read from Right

if(touchX < 1000 and touchY < 1000)
touch = true;
return touch;
}

void midiCC(char command, char value1, char value2){
Serial.print(command, BYTE);
Serial.print(value1, BYTE);
Serial.print(value2, BYTE);
2  Using Arduino / Project Guidance / Digital Touch Pad for Guitar tutorial on: June 18, 2013, 10:10:13 am
hello All,

A few years back I created a touch pad for my guitar which output midi signal to manipulate an audio processing unit, (kaoss pad KP2). if you would like to see mine in action follow the following link to my youtube channel: http://youtu.be/1ZJT-jf3r_k.

Without this forum I could have never finished the project, so as a token of gratitude and also help for any other person trying to do the same thing; here is my code and a schematic(ish) diagram: http://postimg.org/image/ons2fdtqb/, I hope it helps:

Code:
// Touch panel output wiring. These define the pins
// used to apply voltages across the touch screen
#define topOutput 5     // TOP to Digital output dig5
#define leftOutput 6    // LEFT to digital output dig6
#define bottomOutput 7  // BOTTOM to digital output dig7
#define rightOutput 8   // RIGHT to digital output dig8
// Touch panel input wiring. These define the analog
// input pins used to read the voltage from the screen
#define topInput 0      // TOP also to analog input an0
#define rightInput 1    // RIGHT also to analog input an3
//blinkM
#include "Wire.h"
#include "BlinkM_funcs.h"
#include "EEPROM.h"
#define blinkm_addr 0x09
int startup=1;
//int which determines whether its whammy pedal code or kaoss pad code
int pedaleeprom;
int pedal = EEPROM.read(pedaleeprom); //KP2 = 0 (flashes white)  KP3 = 1(flashes yellow) whammy = 2(flashes red)
const int pedalselectPin = 9;
int pedalState = 0;
int lastpedalState = 0;
//KP2 ints
int holdkp2 = 0;
int Holdtch = 0;
//KP3 ints
int holdkp3 = 0;
//hold button ints
const int buttonPin = 3;
int Hld = 0;
int buttonState = 0;
int lastButtonState = 0;
//encoder ints
int val;
int encoder0PinA = 2;
int encoder0PinB = 4;
int encoder0Poseeprom;
int encoder0Pos = EEPROM.read(encoder0Poseeprom);
int encoder0PinALast = LOW;
int n = LOW;
int currentenc = 200;
int cenc=0;
int encoderrunning =0;
//touch pad ints
int Tch = 0;
int touchX = 0;
int touchY = 0;
int ledPin = 13;
int midiX = 0;
int midiY = 0;
//blinkM ints
int LedX = 0;
int LedY = 0;
int Led = 0;
int blinkX = 300;
int blinkY = 300;
int effecteeprom;
int effect=EEPROM.read(effecteeprom);
int del_time=8;
const int lightselectPin = 10;
int lightState = 0;
int lastlightState = 0;
byte hue_val;

void setup(){
pinMode(encoder0PinA, INPUT);
pinMode(encoder0PinB, INPUT);
pinMode(buttonPin, INPUT);
pinMode(pedalselectPin, INPUT);
pinMode(lightselectPin, INPUT);
attachInterrupt(0, doEncoder, CHANGE);  // encoder pin on interrupt 0 - pin 2
Serial.begin(31250);//31250  9600
Serial.flush();
//blinkM
BlinkM_beginWithPower();
BlinkM_stopScript(blinkm_addr);  // turn off startup script              
}

void doEncoder(){//encoder loop
if (EEPROM.read(encoder0Poseeprom)==255){
encoder0Pos=0}
else {
encoder0Pos=EEPROM.read(encoder0Poseeprom)
}
if (EEPROM.read(pedaleeprom)==255){
pedal=0}
else {
pedal=EEPROM.read(pedaleeprom)
}
n = digitalRead(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH)) {
if (digitalRead(encoder0PinB) == LOW) {
encoder0Pos++;
}
else {
encoder0Pos--;
}
}
encoder0PinALast = n;
if (pedal == 0){//KP2
if (encoder0Pos<0){(encoder0Pos=99);
}
else if (encoder0Pos>99){(encoder0Pos=0);
}
}
else if (pedal == 1){//KP3
if (encoder0Pos<0){
(encoder0Pos=127);
}
else if (encoder0Pos>127){
(encoder0Pos=0);
}
}
else if (pedal == 2){//whammy
if (encoder0Pos<17){
(encoder0Pos=34);
}
else if (encoder0Pos>34){
(encoder0Pos=17);
}
}
if (currentenc != encoder0Pos){
encoderrunning=1;
if (pedal !=2){
holdkp2=0;
holdkp3=0;
}
midiCC(0xC0, 0, encoder0Pos);
//Serial.print("encoder possition=");
//Serial.println(encoder0Pos);
EEPROM.write(encoder0Poseeprom, encoder0Pos);
currentenc=encoder0Pos;
encoderrunning=0;
}
}

//main loop
void loop() {
if (EEPROM.read(effecteeprom)==255){
effect=10}
else {
effect=EEPROM.read(effecteeprom)
}
//background select
lightState = digitalRead(lightselectPin);
buttonState = digitalRead(buttonPin);
if ((lightState != lastlightState) or (buttonState == HIGH)){
if  ((lightState == HIGH) and (buttonState != HIGH) and (effect<18)){
effect++;
}
else if  ((lightState== HIGH) and (buttonState != HIGH) and (effect>17)){
effect=0;
}
if  ((lightState == HIGH) and (buttonState != HIGH)){
hue_val =(150);
Wire.beginTransmission(0x09); //goes off
Wire.send('f');
Wire.send(0xff);
Wire.send('h');
Wire.send(hue_val);
Wire.send(0xff);
Wire.send(0x00);
Wire.endTransmission();
delay(50);
Wire.beginTransmission(0x09);//flashes blue
Wire.send('f');
Wire.send(0xff);
Wire.send('h');
Wire.send(hue_val);
Wire.send(0xff);
Wire.send(0xff);
Wire.endTransmission();
delay(50);
Wire.beginTransmission(0x09); //goes off
Wire.send('f');
Wire.send(0xff);
Wire.send('h');
Wire.send(hue_val);
Wire.send(0xff);
Wire.send(0x00);
Wire.endTransmission();
delay(900);
Led=0;
//Serial.println("LED Effect=");
//Serial.println(effect);
}
if((lightState== HIGH) and (buttonState == HIGH)){
effect=9;
hue_val =(150);
Wire.beginTransmission(0x09); //goes off
Wire.send('f');
Wire.send(0xff);
Wire.send('h');
Wire.send(hue_val);
Wire.send(0xff);
Wire.send(0x00);
Wire.endTransmission();
delay(50);
Wire.beginTransmission(0x09);  //flashes blue
Wire.send('f');
Wire.send(0xff);
Wire.send('h');
Wire.send(hue_val);
Wire.send(0xff);
Wire.send(0xff);
Wire.endTransmission();
Led=0;
//Serial.println("LED Effect=");
//Serial.println(effect);
}
EEPROM.write(effecteeprom, effect);
}
lastlightState = lightState;
//pedal select
pedalState = digitalRead(pedalselectPin);
if (pedalState != lastpedalState) {
if  (pedalState == HIGH and pedal < 2){
pedal++;
}
else if (pedalState == HIGH and pedal==2){
   pedal=0;
}
if ( pedal==0){//KP2
hue_val =(128);
}
else if  (pedal==1){//KP3
hue_val =(40);
}
else if  (pedal==2){
hue_val =(255);
}
if (pedalState == HIGH){
Wire.beginTransmission(0x09); //goes off
Wire.send('f');
Wire.send(0xff);
Wire.send('h');
Wire.send(hue_val);
Wire.send(0xff);
Wire.send(0x00);
Wire.endTransmission();
delay(50);
Wire.beginTransmission(0x09);//flashes blue
Wire.send('f');
Wire.send(0xff);
Wire.send('h');
Wire.send(hue_val);
Wire.send(0xff);
Wire.send(0xff);
Wire.endTransmission();
delay(50);
Wire.beginTransmission(0x09); //goes off
Wire.send('f');
Wire.send(0xff);
Wire.send('h');
Wire.send(hue_val);
Wire.send(0xff);
Wire.send(0x00);
Wire.endTransmission();
delay(900);
Led=0;
//Serial.print("device=");
//Serial.println(pedal);
delay(250);
}
EEPROM.write(pedaleeprom, pedal);
}
lastpedalState=pedalState;
//touch function of pad
if (touched() and (encoderrunning==0)){
if (Tch==0){
midiCC(0xB0,92,127);
//Serial.println("touch on");
Tch=1;
}
midiX = map(touchX,250,800,1,126);  // maps the value range of the axis to midi 0->127
midiY = map(touchY,200,800,0,126);  // maps the value range of the axis to midi 0->127
if (touchX > 800){
midiX = 127;
}
else if (touchX < 250){
midiX = 0;
}
if (touchY > 800){
midiY = 127;
}
else if (touchY < 200){
midiY = 0;
}
if (pedal != 2){//KP void
midiCC(0xB0, 13, midiX);    
//Serial.print("X=  "); // sends midi control change for touch pad X axis
//Serial.print(midiX);
midiCC(0xB0, 12, midiY);
//Serial.print(" Y=  ");   // sends midi control change for touch pad y axis
//Serial.println(midiY);
if (cenc != encoder0Pos){
midiCC(0xB0,92,127);
//Serial.println("touch on");
cenc=encoder0Pos;
}
}
else if (pedal == 2){//whammy void
midiCC(0xB0, 11, midiY);
//Serial.print(" Y=  ");
//Serial.println(midiY);
}
//blinkm
LedY = map(touchY,180,820,0,255);    // read the hue pot
LedX = map(touchX,230,840,0,255);    // read the hue pot
hue_val =((LedX+LedY)/2);    // read the hue pot
Wire.beginTransmission(0x09);

}
3  Using Arduino / Programming Questions / Re: RGB lamp error on: June 18, 2013, 09:47:01 am
haha I guess i'm just not ready to embrace it. I fixed the problem by forcing the code to check if the input was low or high before assigning a delay.

i have also since added more functions, anyway its not pretty but it does the job:

Code:
#include "EEPROM.h"
//RED ints
int REDpot = 0;    // select the input pin for the potentiometer
int REDpin = 9;   // select the pin for the RED LED (9)
int REDval = 0;       // variable to store the value coming from the sensor;
int REDcount = 200;
//GREEN ints
int GREENpot = 1;    // select the input pin for the potentiometer
int GREENpin = 10;   // select the pin for the GREEN LED(10)
int GREENval = 0;       // variable to store the value coming from the sensor
int GREENcount = 200;
//BLUE ints
int BLUEpot = 5;    // select the input pin for the potentiometer
int BLUEpin = 11;   // select the pin for the BLUE LED(11)
int BLUEval = 0;       // variable to store the value coming from the sensor
int BLUEcount = 200;
//Encoder Pins these pins can not be changed 2/3 are special pins
int encoderPin1 = 2;
int encoderPin2 = 3;
int count = 200;
volatile int lastEncoded = 0;
volatile long brightness = 0;
long lastbrightness = 0;
int lastMSB = 0;
int lastLSB = 0;
//Encoder ints for speed
int count2 = 200;
volatile int lastEncodedspeed = 0;
volatile long wait = 10;
int waiteeprom;
long lastwait = 0;
int lastMSBspeed = 0;
int lastLSBspeed = 0;
//pattern
int mode=0;
const int modebutton=4;
int modestate=0;
int modestateprev;
// Color arrays
int black[3]  = { 0, 0, 0 };
int white[3]  = { 100, 100, 100 };
int red[3]    = { 100, 0, 0 };
int green[3]  = { 0, 100, 0 };
int blue[3]   = { 0, 0, 100 };
int yellow[3] = { 40, 95, 0 };
int dimWhite[3] = { 30, 30, 30 };
// etc.

// Set initial color
int redVal = black[0];
int grnVal = black[1];
int bluVal = black[2];

int hold = 0;       // Optional hold when a color is complete, before the next crossFade
int DEBUG = 1;      // DEBUG counter; if set to 1, will write values back via serial
int loopCount = 60; // How often should DEBUG report?
int repeat = 0;     // How many times should we loop before stopping? (0 for no stop)
int j = 0;          // Loop counter for repeat

// Initialize color variables
int prevR = redVal;
int prevG = grnVal;
int prevB = bluVal;


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

  pinMode(GREENpin, OUTPUT);
  pinMode(BLUEpin, OUTPUT);
  pinMode(encoderPin1, INPUT);
  pinMode(encoderPin2, INPUT);
  pinMode(modebutton, INPUT);
  digitalWrite(encoderPin1, HIGH); //turn pullup resistor on
  digitalWrite(encoderPin2, HIGH); //turn pullup resistor on

  //call updateEncoder() when any high/low changed seen
  //on interrupt 0 (pin 2), or interrupt 1 (pin 3)
  attachInterrupt(0, updateEncoder, CHANGE);
  attachInterrupt(1, updateEncoder, CHANGE);
  Serial.begin(9600);//9600
  Serial.flush();
}
//Encoder Update
void updateEncoder(){
  //brightness
if (mode ==0){
  int MSB = digitalRead(encoderPin1); //MSB = most significant bit
  int LSB = digitalRead(encoderPin2); //LSB = least significant bit
  int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
  int sum  = (lastEncoded << 2) | encoded; //adding it to the previous encoded value
  if (brightness < 99){
    if(sum == 0b1101 && brightness < 100) brightness ++;
  }
    if (brightness > 1){
  if(sum == 0b1110 && brightness > 0) brightness --;
  }
  lastEncoded = encoded; //store this value for next time
}
else if (mode == 1){
  if (EEPROM.read(waiteeprom)!=wait){
    wait=EEPROM.read(waiteeprom);
  }
  //speed
  int MSBspeed = digitalRead(encoderPin1); //MSB = most significant bit
  int LSBspeed = digitalRead(encoderPin2); //LSB = least significant bit
  int encodedspeed = (MSBspeed << 1) |LSBspeed; //converting the 2 pin value to single number
  int sum  = (lastEncodedspeed << 2) | encodedspeed; //adding it to the previous encoded value
  if(sum == 0b1101 &&  wait < 50) {
    wait ++;
    EEPROM.write(waiteeprom, wait);
    Serial.println(wait);
  }
  if(sum == 0b1110 && wait > 1) {
    wait --;
    EEPROM.write(waiteeprom, wait);
    Serial.println(wait);
  }

  lastEncodedspeed = encodedspeed; //store this value for next time
 
}
}

//pattern
  int calculateStep(int prevValue, int endValue) {
    int step = endValue - prevValue; // What's the overall gap?
    if (step) {                      // If its non-zero,
      step = 1020/step;              //   divide by 1020
    }
    return step;
  }
 
  int calculateVal(int step, int val, int i) {
  if (mode==1){
      if ((step) && i % step == 0) { // If step is non-zero and its time to change a value,
        if (step > 0) {              //   increment the value if step is positive...
          val += 1;           
        }
        else if (step < 0) {         //   ...or decrement it if step is negative
          val -= 1;
        }
      }
      // Defensive driving: make sure val stays in the range 0-255
      if (val > 255) {
        val = 255;
      }
      else if (val < 0) {
        val = 0;
      }
      return val;
    }
  }
 
void crossFade(int color[3]) {// Convert to 0-255
  if (mode==1){
    int R = (color[0] * 255) / 100;
    int G = (color[1] * 255) / 100;
    int B = (color[2] * 255) / 100;
 
    int stepR = calculateStep(prevR, R);
    int stepG = calculateStep(prevG, G);
    int stepB = calculateStep(prevB, B);
 
    for (int i = 0; i <= 1020; i++) {
      redVal = calculateVal(stepR, redVal, i);
      grnVal = calculateVal(stepG, grnVal, i);
      bluVal = calculateVal(stepB, bluVal, i);
 
      analogWrite(REDpin, redVal);   // Write current values to LED pins
      analogWrite(GREENpin, grnVal);     
      analogWrite(BLUEpin, bluVal);
      if (digitalRead(modebutton) == LOW){
        modestateprev=2;
        delay(wait); // Pause for 'wait' milliseconds before resuming the loop

      }
      else if (digitalRead(modebutton) == HIGH){
      }
    }
    // Update current values for next loop
    prevR = redVal;
    prevG = grnVal;
    prevB = bluVal;
    delay(hold); // Pause for optional 'wait' milliseconds before resuming the loop
  }
}

void loop() {
   //mode state
    modestate=digitalRead(modebutton);
  if (modestate == HIGH && mode==0 && modestateprev != 1){
    mode=1;
    modestateprev = 1;
    Serial.print("mode 1");
   
  }
  else if (modestate == HIGH && mode==1 && modestateprev != 1){
    mode=0;
    modestateprev = 1;
    Serial.print("mode 0");
  }
  if (digitalRead(modebutton) == LOW){
        modestateprev=2;
  } 
  //pattern
  crossFade(red);
  crossFade(green);
  crossFade(blue);
  crossFade(yellow);

  if (repeat) { // Do we loop a finite number of times?
    j += 1;
    if (j >= repeat) { // Are we there yet?
      exit(j);         // If so, stop.
    }
  }
  //RED loop
  REDval = round((analogRead(REDpot))/4.05)-brightness*2.6;    // read the value from the sensor
  if (REDval<0){
    REDval=0;
  }
  analogWrite(REDpin, REDval);  // turn the ledPin on
  if (REDcount != REDval){
    REDcount = REDval;
  }
  //GREEN loop
  GREENval = round((analogRead(GREENpot))/10.23)-brightness*1.03;    // read the value from the sensor
    if (GREENval<0){
    GREENval=0;
  }
  analogWrite(GREENpin, GREENval);  // turn the ledPin on
  if (GREENcount != GREENval){
  GREENcount = GREENval;
  }
  //BLUE loop
  BLUEval = round((analogRead(BLUEpot))/8.1)-brightness*1.3;    // read the value from the sensor
    if (BLUEval<0){
    BLUEval=0;
  }
  analogWrite(BLUEpin, BLUEval);  // turn the ledPin on
  if (BLUEcount != BLUEval){
  BLUEcount = BLUEval;
  }
  //Encoder
  if (count != brightness){
  Serial.print("encoder possition=");
  Serial.println(brightness);
  count=brightness;
  }
 
}

thanks for all your help guys.
4  Using Arduino / Programming Questions / Re: RGB lamp error on: June 17, 2013, 06:07:37 pm
thanks, have you got any suggestions? I have no idea what to replace them with, also holding the button down didn't fix the issue.
5  Using Arduino / Programming Questions / RGB lamp error on: June 17, 2013, 05:11:11 pm
Hey all

I'm doing a fairly simple RGB style lamp with several modes,
anyway I've managed to do most of the hard stuff and its working the way I want it to, but there is an issue with when I want to change modes.
so in the void loop there is code to change the mode from 1 to 0 depending on its previous state and whether I have pressed the momentary switch or not.
well the mode changes from mode 0 to mode 1 no problem but I cant change it back, I'm guessing the RGB pattern cycle programmed is interfering with reading the input of the switch. An interesting thing to note is when I remove this from the main loop:
Code:
if (mode==1){
    crossFade(red);
    crossFade(green);
    crossFade(blue);
    crossFade(yellow);
  }
The button works perfectly but naturally the RGB pattern cycle code is broken. Any help would be appreciated, many thanks

Chris.

Code:
//RED ints
int REDpot = 0;    // select the input pin for the potentiometer
int REDpin = 9;   // select the pin for the RED LED (9)
int REDval = 0;       // variable to store the value coming from the sensor;
int REDcount = 200;
//GREEN ints
int GREENpot = 1;    // select the input pin for the potentiometer
int GREENpin = 10;   // select the pin for the GREEN LED(10)
int GREENval = 0;       // variable to store the value coming from the sensor
int GREENcount = 200;
//BLUE ints
int BLUEpot = 5;    // select the input pin for the potentiometer
int BLUEpin = 11;   // select the pin for the BLUE LED(11)
int BLUEval = 0;       // variable to store the value coming from the sensor
int BLUEcount = 200;
//Encoder Pins these pins can not be changed 2/3 are special pins
int encoderPin1 = 2;
int encoderPin2 = 3;
int count = 200;
volatile int lastEncoded = 0;
volatile long brightness = 0;
long lastbrightness = 0;
int lastMSB = 0;
int lastLSB = 0;
//pattern ints
  int mode=0;
  const int modebutton=4;
  int modestate=0;
  // Color arrays
  int black[3]  = { 0, 0, 0 };
  int white[3]  = { 100, 100, 100 };
  int red[3]    = { 100, 0, 0 };
  int green[3]  = { 0, 100, 0 };
  int blue[3]   = { 0, 0, 100 };
  int yellow[3] = { 40, 95, 0 };
  int dimWhite[3] = { 30, 30, 30 };
  // etc.
 
  // Set initial color
  int redVal = black[0];
  int grnVal = black[1];
  int bluVal = black[2];
 
  int wait = 10;      // 10ms internal crossFade delay; increase for slower fades
  int hold = 0;       // Optional hold when a color is complete, before the next crossFade
  int DEBUG = 1;      // DEBUG counter; if set to 1, will write values back via serial
  int loopCount = 60; // How often should DEBUG report?
  int repeat = 0;     // How many times should we loop before stopping? (0 for no stop)
  int j = 0;          // Loop counter for repeat
 
  // Initialize color variables
  int prevR = redVal;
  int prevG = grnVal;
  int prevB = bluVal;

  int calculateStep(int prevValue, int endValue) {
    int step = endValue - prevValue; // What's the overall gap?
      if (step) {                      // If its non-zero,
        step = 1020/step;              //   divide by 1020
      }
      return step;
  }
  int calculateVal(int step, int val, int i) {
    if (mode==1){
      if ((step) && i % step == 0) { // If step is non-zero and its time to change a value,
        if (step > 0) {              //   increment the value if step is positive...
          val += 1;           
        }
        else if (step < 0) {         //   ...or decrement it if step is negative
          val -= 1;
        }
      }
      // Defensive driving: make sure val stays in the range 0-255
      if (val > 255) {
        val = 255;
      }
      else if (val < 0) {
        val = 0;
      }
      return val;
    }
  }
//end of pattern ints


void setup() {
  pinMode(REDpin, OUTPUT);
  pinMode(GREENpin, OUTPUT);
  pinMode(BLUEpin, OUTPUT);
  pinMode(encoderPin1, INPUT);
  pinMode(encoderPin2, INPUT);
  pinMode(modebutton, INPUT);
  digitalWrite(encoderPin1, HIGH);
  digitalWrite(encoderPin2, HIGH);
  digitalWrite(modebutton, HIGH);
  attachInterrupt(0, updateEncoder, CHANGE);
  attachInterrupt(1, updateEncoder, CHANGE);
  Serial.begin(9600);//9600
  Serial.flush();
}
//Encoder Update
void updateEncoder(){
  int MSB = digitalRead(encoderPin1); //MSB = most significant bit
  int LSB = digitalRead(encoderPin2); //LSB = least significant bit

  int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
  int sum  = (lastEncoded << 2) | encoded; //adding it to the previous encoded value
  if (brightness < 99){
    if(sum == 0b1101 && brightness < 100) brightness ++;
  }
    if (brightness > 1){
  if(sum == 0b1110 && brightness > 0) brightness --;
  }

  lastEncoded = encoded; //store this value for next time
}

void loop() {
   //mode state
  modestate=digitalRead(modebutton);
  if (modestate == HIGH && mode==0){
    mode=1;
    Serial.print("mode 1");
    delay (500);
  }
  else if (modestate == HIGH && mode==1){
    mode=0;
    Serial.print("mode 0");
    delay (500);
  }
  if (mode==1){
    crossFade(red);
    crossFade(green);
    crossFade(blue);
    crossFade(yellow);
  }
  else if (mode==0){
    //RED pot loop
    REDval = round((analogRead(REDpot))/4.05)-brightness*2.6;    // read the value from the sensor
    if (REDval<0){
      REDval=0;
    }
    analogWrite(REDpin, REDval);  // turn the ledPin on
    if (REDcount != REDval){
      Serial.print("Red pot value=");
      Serial.println(REDval);
      REDcount = REDval;
    }
    //GREEN pot loop
    GREENval = round((analogRead(GREENpot))/10.23)-brightness*1.03;    // read the value from the sensor
      if (GREENval<0){
      GREENval=0;
    }
    analogWrite(GREENpin, GREENval);  // turn the ledPin on
    if (GREENcount != GREENval){
    Serial.print("Green pot value=");
    Serial.println(GREENval);
    GREENcount = GREENval;
    }
    //BLUE pot loop
    BLUEval = round((analogRead(BLUEpot))/8.1)-brightness*1.3;    // read the value from the sensor
      if (BLUEval<0){
      BLUEval=0;
    }
    analogWrite(BLUEpin, BLUEval);  // turn the ledPin on
    if (BLUEcount != BLUEval){
    Serial.print("pot value=");
    Serial.println(BLUEval);
    BLUEcount = BLUEval;
    }
    //Encoder
    if (count != brightness){
    Serial.print("encoder possition=");
    Serial.println(brightness);
    count=brightness;
    }
  } 
}

void crossFade(int color[3]) {
  if (mode==1){
    if (repeat) { // Do we loop a finite number of times?
      j += 1;
      if (j >= repeat) { // Are we there yet?
        exit(j);         // If so, stop.
      }
    }
    // Convert to 0-255
    int R = (color[0] * 255) / 100;
    int G = (color[1] * 255) / 100;
    int B = (color[2] * 255) / 100;
 
    int stepR = calculateStep(prevR, R);
    int stepG = calculateStep(prevG, G);
    int stepB = calculateStep(prevB, B);
 
    for (int i = 0; i <= 1020; i++) {
      redVal = calculateVal(stepR, redVal, i);
      grnVal = calculateVal(stepG, grnVal, i);
      bluVal = calculateVal(stepB, bluVal, i);
 
      analogWrite(REDpin, redVal);   // Write current values to LED pins
      analogWrite(GREENpin, grnVal);     
      analogWrite(BLUEpin, bluVal);
     
 
      delay(wait); // Pause for 'wait' milliseconds before resuming the loop
  }
    // Update current values for next loop
    prevR = redVal;
    prevG = grnVal;
    prevB = bluVal;
    delay(hold); // Pause for optional 'wait' milliseconds before resuming the loop
  }
}
6  Community / Website and Forum / Re: Karma on: January 31, 2011, 09:51:07 am
its a bit too prone to miss-clicking, its good but maybe you should seperate the + and - a bit more.
7  Using Arduino / Programming Questions / Re: using EEPROM on: January 29, 2011, 04:50:57 pm
I would recommend reading a button or something to control when the writes happen so you don't just sit there & pound the living daylights out of that 1 location by accident, they do have limited durability.  As pointed out, you start writing to address 0 whenever the sketch starts - maybe change the sketch to read first, with "a = EEPROM.read(address);", then continue incrementing "a" from there.
Virgin EEPROM  addresses will have 0xFF (255 decimal) when read back.
Keep in mind that EEPROM.write has built in 3.3mS time to complete, which you provide for with 500mS delay. If you capture the time written as part of a loop of doing other stuff, and check that 3.3ms have gone by before starting the next write, you can be doing other stuff while waiting (this is the heart of Blink Without Delay).



that was all I needed to do, the problem is solved thank you very much, and yes I will be using buttons to activate the code in other projects
As just a little side note, thank you to the other two who also posted help and in refference to the second reply, pressding the reset button does not erase the EEPROM variables, they stay exactly the same.

for the benifit of others, here is the working code, I hope it helps in the future:

Code:
#include <EEPROM.h>
int address;
int value;
int a=EEPROM.read(address);

void setup(){
  Serial.begin(9600);
}

void loop(){
   //Lets first write a value to the eeprom
  if ((a==255)){
  a=0;
  }
  EEPROM.write(address,a);
  a++;
  delay(500);
  if(a>100){a=0;}

  //Lets read the value back and print it to the serial
  value = EEPROM.read(address);
    //Output the data to serial
  Serial.print(value,DEC);  // will print 123
  Serial.print("\n");
  //Serial.print(value,BIN);  // will print 1111011 which is 123 in binary
}


Peace out


Chris.
8  Using Arduino / Programming Questions / using EEPROM on: January 29, 2011, 03:56:51 pm
hey all.

ive been reading the EEPROM library and have looked everywhere for examples on how EEPROM works.

here is the code I am using right now:
Code:
#include <EEPROM.h>

int address = 0;
int value;
int a=0;

void setup(){
  Serial.begin(9600);
}

void loop(){
   //Lets first write a value to the eeprom
  EEPROM.write(address,a);
  a++;
  delay(500);
  if(a>100){a=0;}

  //Lets read the value back and print it to the serial
  value = EEPROM.read(address);
    //Output the data to serial
  Serial.print(value,DEC);  // will print 123
  Serial.print("\n");
  //Serial.print(value,BIN);  // will print 1111011 which is 123 in binary
}

Now if I understand EEPROM right,
if i was to reset the arduino at the "a" count of say...53, when it starts up again, it should pick up where it left off.
instead it starts at 0 again.
this is my first ever experience with EEPROM, so I would like to understand the basic of the code before I implement it in my other projects.
cheers in advance for all your help guys.

 smiley

Chris
9  Forum 2005-2010 (read only) / Development / Re: Help- using an arduino to make a midi controller on: January 05, 2011, 10:37:54 pm
suryavoice, dude ill give you a hand with your encoder, and in exchange could i ask for a favor? you see i dont have a KP3 so I can't test the code i wrote for it. but in theory it should work. if you could PM me your encoder code, ill see what I can do, and if its ok ill PM you a message asking you to test a bit of code, (its just the hold function i need testing).

anyway buddy, hope you had a good holiday!

let me know

 ;D ;D ;D ;D ;D
10  Forum 2005-2010 (read only) / Development / Re: Help- using an arduino to make a midi controller on: January 04, 2011, 05:06:55 pm
it was fun, I upgraded the code so that it should work on KP3...sadly i dont have a KP3 to test it with.

I also made it so the LED blinks when settings are being changed, for example when the HUE cycle is being changed or when you choose between the whammy and the KP2.
11  Forum 2005-2010 (read only) / Development / Re: Help- using an arduino to make a midi controller on: December 20, 2010, 05:20:39 am
also, the hold function. I was just reading the owners manual for the KP3, and that has an actual hold command...which is midiCC(0xB0, 95, 127).

but on the KP2 that is MUTE, so im wondering what command you guys used for the hold function. did you use the 92 on/off command?

also my code isnt running as smoothly on the drum machine as I want it to. I have a feeling its because of the constant sending of coordinates, as when I move my finger on the kaoss pad itself, the drum effect changes but remains in rhythm. if i do it on my pad, it doesnt. i think it might just be with sending the touch ON code too many times. but im not sure.

let me know guys.

cheers.
12  Forum 2005-2010 (read only) / Development / Re: Help- using an arduino to make a midi controller on: December 20, 2010, 02:06:20 am
just curious, how have you guys attached the touch pad to the guitar? I.E, have you put a frame around the touch pad? and if so, how did you make it?

I'm not too happy with my frame, so I want to remake it, im just curious for ideas. cheers.
13  Forum 2005-2010 (read only) / Development / Re: Help- using an arduino to make a midi controller on: December 15, 2010, 03:01:09 pm
I wish I could... but I don't have a camera. sorry dude.
14  Forum 2005-2010 (read only) / Development / Re: Help- using an arduino to make a midi controller on: December 13, 2010, 11:17:02 pm
well I did not expect this thread to attract so much attention.

I'm pretty much done now. I've done all the programming, I just have to wedge it all in my guitar.

The touch pad works both on the kaoss pad and the whammy pedal.

encoder works and so does the hold button.

I've made the touch pads light responsive to movement. but along with that, Ive added an extra button which means I can switch the standby light effect. there are about 18 pre-sets in the BlinkM already and I was more then happy to use them.

I am really tempted to put a kit together and put it on sale on ebay. but as I was doing this as something just for me, I wasn't really looking on making a profit. depends how interested people are on this idea.

one of the dudes above was asking about encoders and hold buttons.

Well I got almost all my stuff off ebay, and you can get an encoder that also has a switch on it. thats the one I opted for. as for the buttons to change the lighting effect or the mode of the touch pad(whammy or kaoss pad) I haven't decided yet.

So I shall see.

Guys I'm more then willing to help others with this project, as long as you don't expect me to do it for you.

thankfully no one has expected that so far.

15  Forum 2005-2010 (read only) / Development / Re: Help- using an arduino to make a midi controller on: November 18, 2010, 11:36:20 am
just buy some cloudy acrylic.
Pages: [1] 2 3 ... 5