http://forum.arduino.cc/index.php?topic=239283.msg1717067#msg1717067
PaulRB:
http://forum.arduino.cc/index.php?topic=239283.msg1717067#msg1717067
That was kind of sneaky ![]()
The unsigned long turnUpdate is something I first started with, but then gave up because I could not figure out how to make it work. I was confused by the digital/analog difference in our codes. Thank you so much! You have been a patient and great teacher! Now my question is...is mine wrong, or just not the best way to make it work?
If it does more of what you want then it is better. If two sketches have the same functionality, then tie-breakers include compactness, efficiency, speed, ease of reading/understanding, ease of extensibility etc...
Well done in getting there yourself. It's so long since I learned, its often hard for me to understand why others get confused so easily or can't simply see/know how to code something. To an experienced programmer, programming is just the same as explaining how to do something in detailed, clear, consise, unambiguous English (or whatever your native language is). I suppose it changes the way you think a little bit.
Paul, I just want to say THANK YOU again for all your help.
On a side note to anyone who decides to duplicate this project, despite what the data sheet on the $3 pro mini's says, it cannot handle 12v and requires a voltage regulator or separate 5v power source. I learned this lesson the hard way. Sure glad they're cheap, and that I purchased 5 at once.
HolidayV:
despite what the data sheet on the $3 pro mini's says, it cannot handle 12v
How much current were you drawing from the Arduino? It should have been OK as long as you don't draw more than a couple of hundred mA.
PaulRB:
HolidayV:
despite what the data sheet on the $3 pro mini's says, it cannot handle 12vHow much current were you drawing from the Arduino? It should have been OK as long as you don't draw more than a couple of hundred mA.
The whole circuit is drawing about 500mah. I hooked it up to a 12v, 9800mah li-ion battery with about 50% charge on it outputting 11.4 volts and the lights came on for 2 seconds and then the ATmega chip put out a pretty little puff of blue smoke.
I rebuilt the circuit last night putting an LM7805 between the battery and vcc of the arduino and it works great now.
Hmmm... how are you driving the led strips? What voltage do they require?
(By the way, you do know the difference between mAh and mA, don't you?)
PaulRB:
Hmmm... how are you driving the led strips? What voltage do they require?(By the way, you do know the difference between mAh and mA, don't you?)
I'm driving the LED strips through MOSFETs (not sure of the exact model number right now but it is similar to the design of an RGB LIGHT strip driver, except I have 4 channels instead of 3). They require 12v. Yes, I know the difference. I'm at work and my phone auto filled mah when it should have been ma for the current draw on the circuit.
Just bad luck then I guess. I have been running cheap Micro Pro Arduinos from eBay/China on 12V without any problems, probably using more current @ 5V than you are, although still very little (10~40mA).
It sure is a good thing the pro mini's are cheap. I burned another one out last night. Accidentally wired the brake switch to 12v instead of 5v and pumped 12v into pin 8. If i had taken the time to learn eagle and print the circuit board instead of doing it on perfboard it wouldnt have happened. NEW PROBLEM: I had ordered a wireless remote for the switches but the eBay auction didn't specify that they were momentary so now I have to rewrite the code so that the turn signals turn on on the first button press and off on the second button press.
I have just spent 3 1/2 hours trying to get the turn signals to stay on until the button is pressed again and I can't get it to work. Help PLEASE!!! ![]()
Come on, come on, you know the drill by now, post your sketch, and updated schematic too if needed.
I think you are going to need new "state" variable which will take 3 values representing left, right and normal. This is in addition to the two state variables for left and right buttons you have now. When left button changes from not pressed to pressed and the state is normal or right, state becomes left. Similar for right button. If left button changes from not pressed to pressed and state is already left, state becomes normal. Similar for right button.
I give up. All I keep doing is breaking the code. There are too many actions going on for me to figure out what to do.
Can't help if you don't post your latest sketch.
Had put this on the backburner while I was doing a couple other projects for my kids, and then my PC got his with a really bad virus. Now that I can OPEN the IDE, here's the last WORKING code that isn't broken from by efforts to figure out how to get the switches to work right:
//////Bicycle Light System v1.05////////////
/////Author: Christopher Valentine 2014
/////Special Thanks to: PaulRB of the Arduino support forum!!!
////This is a full blown bicyle light system with
////headlights, turn signals (front and back),
////brakelights, and multi-function flashing tail lights
////Although this set up is designed for a bike,
////with the use of a wireless remote control it could
////Also be used on a remote control car or other things
byte BRAKE_SWITCH = 8; //Set Pin 8 as Brake Switch
byte Right_Turn_Switch= 4; //Set Pin 4 as Switch
byte LED_RIGHT_REAR = 11; //Right Rear Set Pin 11 as LED
byte LED_RIGHT_FRONT = 7; //Right Front Set Pin 7 as LED
byte LEFT_TURN_SWITCH = 5 ; //Set Pin 5 as Switch
byte LED_LEFT_REAR = 10; //Left Rear Set Pin 10 as LED
byte LED_LEFT_FRONT = 6; //Left Front Set Pin 6 as LED
byte FADE_TOGGLE_SWITCH = 3; //Toggle Fades set Pin 3
boolean buttonState_Left_Turn; //Integer variable named buttonState_Left_Turn Left Turn Switch
boolean buttonState_Right_Turn; //Integer variable named buttonState_Right_Turn Right Turn Switch
boolean buttonState_Brake_Switch; //Integer variable named buttonState_Brake_Switch Brake Switch
// Delay time: sets the time in milliseconds between loop iterations.
// Make this value large for slower transitions.
unsigned long delayTime = 10;
unsigned long lastPatternUpdate = 0;
int currentPattern = 7;
int patternbuttonState = HIGH;
// The initial values of each color.
int rightRearLED = 175;
int leftRearLED = 175;
// Indicates whether a color is incRighttRearementing (1) or decrementing (0).
int incRighttRear = 1;
int incLefttRear = 1;
void setup()
{
pinMode(FADE_TOGGLE_SWITCH, INPUT_PULLUP);
pinMode(LEFT_TURN_SWITCH, INPUT);
pinMode(LED_LEFT_REAR, OUTPUT);
pinMode(LED_LEFT_FRONT, OUTPUT);
pinMode(Right_Turn_Switch, INPUT);
pinMode(LED_RIGHT_REAR, OUTPUT);
pinMode(LED_RIGHT_FRONT, OUTPUT);
pinMode(BRAKE_SWITCH, INPUT);
}
void turnLeft()//turnLeft function
{
analogWrite(LED_RIGHT_FRONT, 255);
analogWrite(LED_RIGHT_REAR, 255);
analogWrite(LED_LEFT_REAR, 125);
buttonState_Left_Turn = HIGH; //the micro the switch is now HIGH
while (buttonState_Left_Turn == HIGH) //While the switch is NOT pressed do the following
{
buttonState_Left_Turn = digitalRead(LEFT_TURN_SWITCH); //Continually look at the switch to see if its pressed
analogWrite(LED_LEFT_REAR, 255); //Set the Rear Left LED to maximum brightness
analogWrite(LED_LEFT_FRONT, 255); //Set the Front Left LED to maximum brightness
delay(100);
analogWrite(LED_LEFT_REAR, 0); //turn the LED off for a blinking effect
analogWrite(LED_LEFT_FRONT, 0); //turn the LED off for a blinking effect
delay(100);
}
//Once the switch is pressed again, break out of the loop above and then break out of the function completely and go back to our main loop
buttonState_Left_Turn = LOW; //First we tell the micro the switch is now LOW
analogWrite(LED_LEFT_REAR, 0); //We turn the LED off before leaving our custom function
analogWrite(LED_LEFT_FRONT, 0); //We turn the LED off before leaving our custom function
}
void turnRight()//turnRight function
{
analogWrite(LED_LEFT_FRONT, 255);
analogWrite(LED_RIGHT_REAR, 125);
analogWrite(LED_LEFT_REAR, 255);
buttonState_Right_Turn = HIGH; //the micro the switch is now HIGH
while (buttonState_Right_Turn == HIGH) //While the switch is NOT pressed do the following
{
buttonState_Right_Turn = digitalRead(Right_Turn_Switch); //Continually look at the switch to see if its pressed
analogWrite(LED_RIGHT_REAR, 255); //Set the Rear Right LED to maximum brightness
analogWrite(LED_RIGHT_FRONT, 255); //Set the Front Right LED to maximum brightness
delay(100);
analogWrite(LED_RIGHT_REAR, 0); //turn the LED off for a blinking effect
analogWrite(LED_RIGHT_FRONT, 0); //turn the LED off for a blinking effect
delay(100);
}
//Once the switch is pressed again, break out of the loop above and then break out of the function completely and go back to our main loop
buttonState_Right_Turn = LOW; //First we tell the micro the switch is now LOW
analogWrite(LED_RIGHT_REAR, 0); //We turn the LED off before leaving our custom function
analogWrite(LED_RIGHT_FRONT, 0); //We turn the LED off before leaving our custom function
}
// Smoothly changes the color values
void transition()
{
if (rightRearLED >= 175)
incRighttRear = 0;
else if (rightRearLED <= 20)
incRighttRear = 1;
if (leftRearLED >= 175)
incLefttRear = 0;
else if (leftRearLED <= 20)
incLefttRear = 1;
if (incRighttRear)
rightRearLED++;
else
rightRearLED--;
if(incLefttRear)
leftRearLED++;
else
leftRearLED--;
}
// Sets the output voltage on the LED pins.
void setColor()
{
analogWrite(LED_RIGHT_REAR, rightRearLED);
analogWrite(LED_LEFT_REAR, leftRearLED);
}
void loop()
{
buttonState_Brake_Switch = digitalRead(BRAKE_SWITCH); //Continually look at the switch to see if its pressed
buttonState_Left_Turn = digitalRead(LEFT_TURN_SWITCH); //Continually look at the switch to see if its pressed
buttonState_Right_Turn = digitalRead(Right_Turn_Switch); //Continually look at the switch to see if its pressed
if (buttonState_Brake_Switch == HIGH && buttonState_Left_Turn == HIGH) //If the switch goes HIGH, act on it
{
turnLeft();
}
else if (buttonState_Brake_Switch == HIGH && buttonState_Right_Turn == HIGH) //If the switch goes HIGH, act on it
{
turnRight();
}
else if (buttonState_Brake_Switch == HIGH) //If the switch goes HIGH, act on it
{
analogWrite(LED_RIGHT_REAR, 255); //Set the Rear Right LED to maximum brightness
analogWrite(LED_LEFT_REAR, 255); //Set the Rear Left LED to maximum brightness
analogWrite(LED_LEFT_FRONT, 255);
analogWrite(LED_RIGHT_FRONT, 255);
}
else if (buttonState_Right_Turn == HIGH) //If the switch goes HIGH, act on it
{
turnRight();
}
else if (buttonState_Left_Turn == HIGH) //If the switch goes HIGH, act on it
{
turnLeft();
}
else { //Normal mode, no buttons pressed
analogWrite(LED_LEFT_FRONT, 255);
analogWrite(LED_RIGHT_FRONT, 255);
if (millis() - lastPatternUpdate >= delayTime) {
lastPatternUpdate = millis();
transition();
setColor();
}
if (digitalRead(FADE_TOGGLE_SWITCH) != patternbuttonState) { //the button state has changed
if (patternbuttonState == LOW) { //the button state must have just gone high
patternbuttonState = HIGH; //record that button has just been pressed but do nothing for now, wait for release
}
else { //it was high, so the button must have just been released
//record the fact that be button has now gone low ie. not pressed
patternbuttonState = LOW;
switch (currentPattern) {
case 1: //Pattern 1 has been running
//set up pattern 2
currentPattern = 2;
rightRearLED = 175;
leftRearLED = 175;
incRighttRear = 1;
incLefttRear = 1;
delayTime = 5;
break;
case 2://Pattern 2 has been running
//set up pattern 3
currentPattern = 3;
rightRearLED = 175;
leftRearLED = 175;
incRighttRear = 1;
incLefttRear = 1;
delayTime = 1;
break;
case 3://Pattern 3 has been running
//set up pattern 4
currentPattern = 4;
rightRearLED = 175;
leftRearLED = 20;
incRighttRear = 1;
incLefttRear = 1;
delayTime = 10;
break;
case 4://Pattern 4 has been running
//set up pattern 5
currentPattern = 5;
rightRearLED = 175;
leftRearLED = 20;
incRighttRear = 1;
incLefttRear = 1;
delayTime = 5;
break;
case 5://Pattern 5 has been running
//set up pattern 6
currentPattern = 6;
rightRearLED = 175;
leftRearLED = 20;
incRighttRear = 1;
incLefttRear = 1;
delayTime = 1;
break;
case 6://Pattern 6 has been running
//set up pattern 7
currentPattern = 7;
rightRearLED = 175;
leftRearLED = 175;
incRighttRear = 0;
incLefttRear = 0;
delayTime = 0;
break;
case 7:
//set up pattern 1
currentPattern = 1;
rightRearLED = 175;
leftRearLED = 175;
incRighttRear = 1;
incLefttRear = 1;
delayTime = 10;
break;
}
}
}
}
}
On a side note, Paul, I was wondering if you have any suggestions on this topic as well?
http://forum.arduino.cc/index.php?topic=245649.msg1760633#new
HolidayV:
NEW PROBLEM: I had ordered a wireless remote for the switches but the eBay auction didn't specify that they were momentary so now I have to rewrite the code so that the turn signals turn on on the first button press and off on the second button press.
Right, so if I understand correctly, the problem you have now is that you have some kind of remote switch for the turn signals, which sends a momentary signal when you first begin to signal left or right, correct? A second left signal means that the previous left signal is now cancelled (and similar for right)?
I think you need a new int variable, call it something like "turn_status" . Set it to 0 initially. We will use the value 0 to mean no turn signal, 1 to mean left signal and 2 for right signal. Use this new variable to control your left/right flashing code instead of the values read directly from the switches.
Then you need a new piece of code to change the new status variable:
If the left switch is high and it was previously low then
delay for around 20~30 ms to avoid switch bouncing
If the turn status is 1
update the turn status to 0
else
update the turn status to 1
If the right switch is high and it was previously low then
delay for around 20~30 ms to avoid switch bouncing
If the turn status is 2
update the turn status to 0
else
update the turn status to 2
Finally, update the previous statuses of the left switch and right switch to their current values.
Hopefully that will give you a few clues.
If you can get that working, a suggestion would be to build in an automatic cancel after, say, 10 secs.
I will see what I can do. Thank you.