Hello fellas,
I'm sure this has been asked a million times and I apologize in advance.
Might be a silly question, although I was curious if this is a proper way to drive 30 LEDs with an external supply.
I currently have 30 LEDs total with 2 LEDs per output/contact to the transistor, making use of 15 pins on my Mega.
The reason I ask is when I simply use the external supply and have the arduino NOT hooked up to my PC, the arduino resets every now and then when all LEDs are illuminated...
I can also see on the VIN (5v out from the motor driver is powering up the arduino) that it dips slightly below 4v when the reset is happening.
Do I need a bigger resistor on the base pin of the transistor?
I am working on a school carnival game for the kids' school.
Here is the project so far!
(Sorry if the wiring/stuff is hokey, I plan on putting a back plate on the backside.)
Here is a rough schematic (I have 30 LEDs wired up like this // 2 LEDs on one arduino pin to transistor) --
Here is the code --
//led high striker game
//references to code used and help//documents
//psi math reference -- 'https://www.youtube.com/watch?v=UrqPxwsPWGk'
//led loops -- 'http://ece.uprm.edu/techcarnival/highstriker.html#video'
//L298N driver -- 'https://circuitsgeek.com/guides-and-how-to/arduino-dc-motor-control-using-l298n-motor-driver/'
//original idea (site appears to have issues now?) -- 'http://www.jmillerid.com/wordpress/2009/09/mustache-themed-hi-striker/'
int standby = 0;
int ledz = 0; // led loop init integer
float difficulty = 0.0; //difficulty variable
float difficultyValue = 0.0; // if difficulty is raised
unsigned long startMillis; //millisecond counter start value
unsigned long currentMillis; // millisecond current value
const unsigned long period = 500; //millisecond period length
const float pressureZero = 103.55; //reading of pressure transducer at 0psi
const float pressureMax = 921.60; //reading of pressure transducer at 30psi
const float pressuretransducermaxPSI = 30.00; //psi value of transducer being used
float pressureValue = 0.0; //variable to store the value coming from the pressure transducer
float psiMaxVal = 0.005; //stores the psi max value after being compared // anything > 0 for boot up
void setup() {
Serial.begin(9600); //begin serial for output window
pinMode(LED_BUILTIN, OUTPUT); //turn on the on-board led
for(ledz=39; ledz<=53; ledz++){ //for arduino di contact 39 to 53 // being used for led display
pinMode(ledz, OUTPUT); //loop and set the outputs of the contacts
}
for(ledz=2; ledz<=7; ledz++){ //contact 2 is bell, 4 is ready status, 5/6/7 is difficulty knob, 3 not used
pinMode(ledz, OUTPUT);
}
}
void loop() {
currentMillis = millis(); //set cur millis to internal millis clock
//psi sensor math
pressureValue = analogRead(A3); //reads value from sensor input pin and assigns to variable
pressureValue = ((pressureValue-pressureZero)*pressuretransducermaxPSI)/(pressureMax-pressureZero); //conversion equation to convert analog reading to psi
difficulty = analogRead(A0) * 3.0 / 1024.0; //difficulty zero pot
//debug psi input
Serial.print(pressureValue); //prints value to serial
Serial.println(" psi"); //prints egu to serial
//Serial.println(analogRead(A3));
//debug difficulty
//Serial.println(pressureValue + difficultyValue);
//debug diff zero pot
//difficulty zero pot input
//zero pot on +3v output 0/1/2 for ranges
//Serial.println(analogRead(A0) * 3.0 / 1024.0);
//Serial.println(" ZERO POT");
Serial.println(standby);
if(pressureValue >= psiMaxVal + 0.2){ //if psi curr val is greater than psi max (+ a little offset) then set MAX
psiMaxVal = pressureValue; //set max variable
digitalWrite(4, LOW); //turn status led off // not ready for next hit
startMillis = currentMillis; //set the start of the milli counter for 0.5s reset
//debug psi max
Serial.print(psiMaxVal);
Serial.println(" psi MAXX");
}
if ((currentMillis - startMillis >= period) && (psiMaxVal != 0)){ //Reset psi max after 0.5s and if psi max is not zero
psiMaxVal = 0; //clear out psi max variable
digitalWrite(4, HIGH); //turn status led on // ready for next round
//debug ready status
//Serial.println("READY");
}
if ((difficulty >= 0) && (difficulty < 1)) {
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
difficultyValue = 0;
}
else if ((difficulty > 1) && (difficulty < 2)) {
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
difficultyValue = 1;
}
else if (difficulty > 2) {
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
difficultyValue = 2;
}
//review psi max
if (psiMaxVal > 5 + difficultyValue){
ledzUP(53);
ledzDOWN(53);
}
else if (psiMaxVal > 4.5 + difficultyValue){
ledzUP(51);
ledzDOWN(51);
}
else if (psiMaxVal > 4 + difficultyValue){
ledzUP(50);
ledzDOWN(50);
}
else if (psiMaxVal > 3.5 + difficultyValue){
ledzUP(48);
ledzDOWN(48);
}
else if (psiMaxVal > 3 + difficultyValue){
ledzUP(46);
ledzDOWN(46);
}
else if (psiMaxVal > 2.5 + difficultyValue){
ledzUP(45);
ledzDOWN(45);
}
else if (psiMaxVal > 2 + difficultyValue){
ledzUP(43);
ledzDOWN(43);
}
else if (psiMaxVal > 1.5 + difficultyValue){
ledzUP(41);
ledzDOWN(41);
}
else if (psiMaxVal > 1 + difficultyValue){
ledzUP(40);
ledzDOWN(40);
}
else if (psiMaxVal > 0.5 + difficultyValue){
ledzUP(39);
ledzDOWN(39);
}
//dance ledz when zero pot is turned down all the way
if ((difficulty >= 0) && (difficulty < 0.1)) {
ledzUP(53);
delay(2000); //delay at the top so blink leds can "show better"
ledzDOWN(53);
delay(250); //delay at the bottom
standby = 1; //standby flag to not ring bell
}
else{
standby = 0; //turn standby off
}
}
void ledzUP(int x){
if(x < 39){
return;
}
else{
ledzUP(x-1);
digitalWrite(x, HIGH);
delay(100);
if((x >= 53) && (standby == 0)) {
//ring bell
digitalWrite(2, HIGH);
delay(1000);
digitalWrite(2, LOW);
}
}
}
void ledzDOWN(int x){
if(x < 39){
return;
}
else{
delay(100);
digitalWrite(x, LOW);
ledzDOWN(x-1);
}
}
A few points --
I hope the code comments make sense, but I am using a psi xducer to measure a "hit" and drive some LEDs up and then ring a bell.
Original idea and code/references are in the code at the top.
I have a zero pot to adjust the "difficulty" of the game.
I'll try to get a schematic drawn up for further assistance.
I understand I'm not really using the motor driver properly/as intended. Was hoping to just try one out and see what it does.
My code may not be the best, but it's working as I would like!
Any advice helps!