My code hangs.. Maybe I2c? [SOLVED]

Hello,

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…
Please help me!
(and be easy as I’m learning and i know my code looks like s**t!)

Here the whole thing:

Onebutton library: Arduino OneButton Library
Adafruit color sensor library: GitHub - adafruit/Adafruit_TCS34725: Driver for Adafruit's TCS34725 RGB Color Sensor Breakout

#include <MemoryFree.h>
#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>
//Outputs
#define relay1 6
#define relay2 7
#define redout 11 
#define greenout 10
#define blueout 9 
//Inputs
OneButton centroin(2, true);
OneButton ledin(3, true);
#define colorein 4
//Delay
#define dimspeed 50
//VAR
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() {

  if (tcs.begin()) {} 
  else {
    Reset_AVR(); // halt!    
  }
  
//set led pin
  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;
  
//set relay pin
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
//set button pin  
  pinMode(colorein, INPUT_PULLUP);
  
// link the myClickFunction function to be called on a click event.     
    centroin.attachClick(clickcentro);
    
    ledin.attachClick(clickled);
    ledin.attachDuringLongPress(pressled);
    ledin.attachLongPressStop(stoppress);
// set onebutton times
    centroin.setClickTicks(400);    
    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() {
//listen to imput buttons 
  centroin.tick();
  ledin.tick();
  ledin.isLongPressed();

  

//get color sensor readings                            
  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);
        
// 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;

    
      redstate =(int)r;// gammatable[(int)r];
      greenstate =(int)g;// gammatable[(int)g];
      bluestate =(int)b;// 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);
//rgb led output 
      analogWrite(redout, rgb[0]);
      analogWrite(greenout, rgb[1]);
      analogWrite(blueout, rgb[2]);
     
  }
// turn off LED  
  tcs.setInterrupt(true);  
delay(10);        
        
}

//relay1 on and off
void clickcentro(){
   if (relay1state == 0){
    digitalWrite (relay1, HIGH);
    relay1state = 1;
  }
   else {
    digitalWrite (relay1, LOW);
    relay1state = 0;
  }
}

//relay2 on and off + rgb led dim on and dim off
void clickled(){
  
   switch (relay2state){
   case 0:
//DIMon  	
    digitalWrite (relay2, HIGH);
 
    relay2state = 1;
    
 
    for (double x = 0.00; x <= hsv[3]; x += 0.01){
     color.hsvToRgb(hsv[0],hsv[1],x,rgb);
  
     analogWrite(redout, rgb[0]);
     analogWrite(greenout, rgb[1]);
     analogWrite(blueout, rgb[2]);
 
     delay(dimspeed);     
    }
  break;
  
  case 1:
//DIMoff
 
    double v;
    
    for (v = hsv[3]; v >= 0.00; v -= 0.01){
     color.hsvToRgb(hsv[0],hsv[1],v,rgb);
     
     analogWrite(redout, rgb[0]);
     analogWrite(greenout, rgb[1]);
     analogWrite(blueout, rgb[2]);
 
     delay(dimspeed);
    }
 
    digitalWrite (relay2, LOW);
    relay2state = 0;
 
  break;
  }
}

// set rgb led brightness
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;
 
         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;
 
         analogWrite(redout, rgb[0]);
         analogWrite(greenout, rgb[1]);
         analogWrite(blueout, rgb[2]);
         
         delay(dimspeed);
       }     
     }
   }
}

// change brightness direction
void stoppress(){
  
  if (direzdim == 0){
    direzdim = 1;
  }
  else {
    direzdim = 0;
  }
}
//end

The code runs indefinitely, but when relay2 is HIGH after a while it hangs.

The hardware now should be set properly, with resistor to limit the current in line with ssr, and the transistor pins are disconnected. This leaves to the uno board three buttons, 2 ssr, adafruit color sensor via I2c and USB for power.

Also, if I load the hardware with a simpler sketch, powering up relay1, relay2, and fading the rgb led strip everything works fine.

New update... If I remove from code the parts relative to adafruit color sensor Looks like it does not hang anymore... Could it be I2c? I really need some help...

Hi tomy983

Can you post a diagram of how your hardware is connected?

Thanks

Ray

Not very good at drawing, if you cannot understand I will redraw it…
Also there are resistors between arduino pins and tip120 and ssr

Solved

http://forum.arduino.cc/index.php?topic=246488.0