Arduino Forum

Using Arduino => Installation & Troubleshooting => Topic started by: Neofito on Aug 12, 2016, 12:02 pm

Title: My arduino freezes while using this code i'm doing. (mpr121 and rgb LED)
Post by: Neofito on Aug 12, 2016, 12:02 pm
I'm noob with arduino, and programing in general.


The mpr121 is a multitactil sensor, and I want to set an especific color depending of which electrode of the sensor is being touched.

The question is, for some reason, the arduino some times freezes ONLY when is the red channel alone, like in the first and last if statement of this code. By freezing i mean that i touch the electrode 0 or 4, but when i release it, it keeps with the red colour ON.

I'm wondering if is a question of ram, because of the continous division of the value. I have to do the division because i can only give 30V to the LED, but that might overload the red channel. So maybe an arduino mega should do the job fine?


Code: [Select]

#include "Wire.h"
#include "Adafruit_MPR121.h"

Adafruit_MPR121 cap = Adafruit_MPR121();

void setup() {
  cap.begin(0x5A);
  for (int i=9 ; i<12 ; i++)
      pinMode(i, OUTPUT);
}


void Color(int R, int G, int B)  // corrección del canal rojo a máximo PWM 127 (50%), para trabajar a 32V, en vez de los 22V sacados empíricamente).
  {
  
    R = R / 2;           // 50 % del valor
    analogWrite(9, R) ; // Red
    analogWrite(10, G) ; // Green
    analogWrite(11, B) ; // Blue
  }

void loop() {
  
  if (cap.touched()&(1<<0))  // pizz
  {
    Color(255,0,0);
    }
  else if(cap.touched()&(1<<1))  // IV String
  {
    Color(0,255,0);
  }
  else if(cap.touched()&(1<<2))  // III String
  {
    Color(0,0,255);
  }
  else if(cap.touched()&(1<<3))  // II String
  {
    Color(255,255,255);  
  }
  else if(cap.touched()&(1<<4)) // I String
  {
    Color(127,0,0);  
  }
  else
  {
    Color(0,0,0);
  }
}





Thank you for the fish, and sorry for my english  :D


PS: i just realized i post it on troubleshooting instead of coding, i had the two tabs opened on my computer and i confused between them.
Title: Re: My arduino freezes while using this code i'm doing. (mpr121 and rgb LED)
Post by: sterretje on Aug 12, 2016, 12:08 pm
Please use code tags and not quote tags.

How are your LEDs wired?
Title: Re: My arduino freezes while using this code i'm doing. (mpr121 and rgb LED)
Post by: Neofito on Aug 12, 2016, 01:24 pm
Please use code tags and not quote tags.

How are your LEDs wired?
Corrected!


Pins 9, 10 and 11 are connected to an ULN2803 driver thats supply 30V, and after that to the 30W RGB LED. The wiring works fine, is just that some times, like 5-10% of the time i press electrodes 0 or 4, the arduino freezes on red and it doesnt recognized anything else, untill i press reset button.



The problem is that red channel works in 23v-300mA, but blue and green are around 32V. Since i have to use the same supply for all 3 channels, I have to divide the R channel information (gived in RGB standard). I do not know if I'm explaining well.
Title: Re: My arduino freezes while using this code i'm doing. (mpr121 and rgb LED)
Post by: Neofito on Aug 12, 2016, 02:12 pm
Please use code tags and not quote tags.

How are your LEDs wired?
i just made this schematic. My first one, so i guess is kind of messy

(https://i.imgur.com/sF4huQG.png)
Title: Re: My arduino freezes while using this code i'm doing. (mpr121 and rgb LED)
Post by: Neofito on Aug 16, 2016, 09:03 pm
I still can't find the solution =/