Take the 2-minute tour ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

OneButton library was working well up to now...

I purchased an Adafruit color sensor and what I am trying to achieve is:

three buttons and one color sensor for imput

two ssr and three tip120 transistor as output.

One of the buttons controls on click one of the relay. Looks like is working.

One button control the led build into the color sensor and reads its outputs only when led strip is on.

The last button if clicked fade on or off the led strip via tip120 and disconnects from mains the 12v power supply for the strip via the second ssr; while if is pressed modifies the values of illumination trough values of hsv color scheme.

At present the Arduino gets two grounds, one from led strip supply and one from usb(laptop) for programming and debugging, later will have its own supply always separated from led strip supply, but always sharing grounds.

Using serialprint I understand that my code blocks after a press to turn on the led strip...

I can turn on and off light with relay1 no problem with centroin, but as soon as I hit ledin Relay2 turns on, led strip dim up, serialPrint gives out 500 as check... And then no buttons work anymore...!

Really can't figure out why...

(and be easy as I'm learning and I know my code looks bad!)

Here the whole thing:

Onebutton: http://www.mathertel.de/Arduino/OneButtonLibrary.aspx

#include <avr/io.h>
#include <avr/wdt.h>

#define Reset_AVR() wdt_enable(WDTO_30MS)

#include <RGBConverter.h>
#include <Wire.h>
#include "Adafruit_TCS34725.h"
#include "OneButton.h"
// Pick analog outputs, for the UNO these three work well
// use ~560  ohm resistor between Red & Blue, ~1K for green (its brighter)
#define relay1 7
#define relay2 8
#define redout 11 
#define greenout 10
#define blueout 9 
OneButton centroin(2, true);
OneButton ledin(3, true);
#define colorein 4
#define dimspeed 50

int relay1state = 0;
int relay2state = 0;
byte redstate = 0;
byte greenstate = 0;
byte bluestate = 0;


RGBConverter color;
double hsv[4];
byte rgb[3];

boolean direzdim = 0;
double dimdownstop = 0.10;

#define commonAnode false

// our RGB -> eye-recognized gamma color
byte gammatable[256];


Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);

void setup() {
  hsv[3] = 0.00;
  Serial.begin(9600);
  Serial.println("Color View Test!");
  Serial.print(hsv[3]);
  if (tcs.begin()) {
    Serial.println("Found sensor");
  } else {
    Serial.println("No TCS34725 found ... check your connections");
//    while (1);
    Reset_AVR(); // halt!
  }

// use these three pins to drive an LED
  pinMode(redout, OUTPUT);
  pinMode(greenout, OUTPUT);
  pinMode(blueout, OUTPUT);

  rgb[0] = 100;
  rgb[1] = 100;
  rgb[2] = 100;
  hsv[0] = 1.00;
  hsv[1] = 0.50;
  hsv[2] = 0.50;
  hsv[3] = 0.50;

//setta pin rele'come uscite
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);

  pinMode(colorein, INPUT_PULLUP);
// link the myClickFunction function to be called on a click event.   

    centroin.attachClick(clickcentro);
    centroin.setClickTicks(400);
    ledin.attachClick(clickled);
    ledin.attachDuringLongPress(pressled);
    ledin.attachLongPressStop(stoppress);
    ledin.setClickTicks(400);
    ledin.setPressTicks(700);


// thanks PhilB for this gamma table!
// it helps convert RGB colors to what humans see
  for (int i=0; i<256; i++) {
    float x = i;
    x /= 255;
    x = pow(x, 2.5);
    x *= 255;

    if (commonAnode) {
      gammatable[i] = 255 - x;
    } else {
      gammatable[i] = x;      
    }
    //Serial.println(gammatable[i]);
  }
}



void loop() {
//  int coloreinstate = digitalRead(colorein);
//  unsigned long now = millis();
  centroin.tick();
  ledin.tick();
  ledin.isLongPressed();



  if ((digitalRead(colorein) == LOW) && (relay2state == 1)){
      uint16_t clear, red, green, blue;
// turn on LED    
      tcs.setInterrupt(false);      
// takes 50ms to read    
      delay(60);                     

      tcs.getRawData(&red, &green, &blue, &clear);


      Serial.print("C:\t"); Serial.print(clear);
      Serial.print("\tR:\t"); Serial.print(red);
      Serial.print("\tG:\t"); Serial.print(green);
      Serial.print("\tB:\t"); Serial.print(blue);

// Figure out some basic hex code for visualization
      uint32_t sum = clear;
      float r, g, b;
      r = red; r /= sum;
      g = green; g /= sum;
      b = blue; b /= sum;
      r *= 256; g *= 256; b *= 256;
      Serial.print("\t");
      Serial.print((int)r, HEX); Serial.print((int)g, HEX); Serial.print((int)b, HEX);
      Serial.println();

      redstate = gammatable[(int)r];
      greenstate = gammatable[(int)g];
      bluestate = gammatable[(int)b];

//convert rgb to hsv
      byte r1 = redstate;
      byte g1 = greenstate;
      byte b1 = bluestate;
      color.rgbToHsv(r1,g1,b1,hsv);

      color.hsvToRgb(hsv[0],hsv[1],hsv[3],rgb);
      analogWrite(redout, rgb[0]);
      analogWrite(greenout, rgb[1]);
      analogWrite(blueout, rgb[2]);

  }
// turn off LED  
  tcs.setInterrupt(true);  
delay(10);        

}

void clickcentro(){
  Serial.print("clickcentro");
  Serial.println();
  if (relay1state == 0){
    digitalWrite (relay1, HIGH);
    relay1state = 1;
  }
  else {
    digitalWrite (relay1, LOW);
    relay1state = 0;
  }
}

void clickled(){
  Serial.print("clickled");
  Serial.println();
  switch (relay2state){
  case 0:
    digitalWrite (relay2, HIGH);
    Serial.print("onled");
    Serial.println();

    relay2state = 1;

    Serial.print(hsv[3]);
    Serial.println();

    for (double x = 0.00; x <= hsv[3]; x += 0.01){
     color.hsvToRgb(hsv[0],hsv[1],x,rgb);
     Serial.print(x);
     Serial.println();
     analogWrite(redout, rgb[0]);
     analogWrite(greenout, rgb[1]);
     analogWrite(blueout, rgb[2]);
     Serial.print("500");
     delay(dimspeed);     
    }
  break;

  case 1:
//DIMDOWN
//  digitalWrite (relay2, LOW);
    Serial.print("100");
    double v;

    for (v = hsv[3]; v >= 0.00; v -= 0.01){
      Serial.print("200");
     color.hsvToRgb(hsv[0],hsv[1],v,rgb);
     analogWrite(redout, rgb[0]);
     analogWrite(greenout, rgb[1]);
     analogWrite(blueout, rgb[2]);
     Serial.print(v);
     Serial.println();

     delay(dimspeed);
    }
    Serial.print("300");
    digitalWrite (relay2, LOW);
    relay2state = 0;
    Serial.print("offled");
    Serial.println();
  break;
  }
}
//DIM
void pressled(){Serial.print("pressled");
   if (direzdim == 0 && hsv[3] < 0.98){

     for (double v = hsv[3]; v < 0.99 ; v += 0.01){
       if (digitalRead (3)==LOW && relay2state == 1){
         color.hsvToRgb(hsv[0],hsv[1],v,rgb);
         hsv[3] = v;
         Serial.print("upled");
         Serial.println();
         Serial.print(v);
         Serial.println();
         analogWrite(redout, rgb[0]);
         analogWrite(greenout, rgb[1]);
         analogWrite(blueout, rgb[2]);

         delay(dimspeed);
       }          
     }
   }

   if (direzdim == 1 && hsv[3] > (dimdownstop + 0.01)){

     for (double v = hsv[3]; v > dimdownstop; v -= 0.01){
       if (digitalRead (3)==LOW && relay2state == 1){
         color.hsvToRgb(hsv[0],hsv[1],v,rgb);
         hsv[3] = v;
         Serial.print("downled");
         Serial.println();
         Serial.print(v);
         Serial.println();
         analogWrite(redout, rgb[0]);
         analogWrite(greenout, rgb[1]);
         analogWrite(blueout, rgb[2]);

         delay(dimspeed);
       }     
     }
   }
}

void stoppress(){

  if (direzdim == 0){
    direzdim = 1;
  }
  else {
    direzdim = 0;
  }
}
share|improve this question

migrated from stackoverflow.com Jun 23 at 19:59

This question came from our site for professional and enthusiast programmers.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.