Switching on the diode for some time, as well as execution of the next program in Arduino - arduino

The circuit made in tinkercad
I use the relay, because I have only 4 wires to led diode and this two switches.
int led = 12; // red led
int s1 = 9; //switch 1
int s2 = 10; //switch 1
int k1 = 3; // first blue led
int k2 = 2; // second blue led
int y1 = 11; // relay
unsigned long startTime1 = 0;
unsigned long startTime2 = 0;
const int led1Duration = 6000; // first blue led time
const int led2Duration = 12000; // second blue led time
void setup()
{
pinMode(led, OUTPUT);
pinMode(k1, OUTPUT);
pinMode(k2, OUTPUT);
pinMode(y1, OUTPUT);
pinMode(s1, INPUT);
pinMode(s2, INPUT);
}
void loop()
{
if (digitalRead(s1) == HIGH and digitalRead(s2) == LOW)
{
digitalWrite(k1, HIGH);
digitalWrite(y1, HIGH);
startTime1 = millis();
}
else
{
digitalWrite(led, LOW);
}
if (digitalRead(s2) == HIGH and digitalRead(s1) == LOW)
{
digitalWrite(k2, HIGH);
digitalWrite(y1, HIGH);
startTime2 = millis();
}
else
{
digitalWrite(led, LOW);
}
if (digitalRead(k1) == HIGH && (millis() - startTime1 >= led1Duration))
{
digitalWrite(k1, LOW);
digitalWrite(y1, LOW);
}
if (digitalRead(k2) == HIGH && (millis() - startTime2 >= led2Duration))
{
digitalWrite(k2, LOW);
digitalWrite(y1, LOW);
}
if (digitalRead(k1) == HIGH and digitalRead(k2) == LOW)
{
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
delay(200);
}
if (digitalRead(k2) == HIGH and digitalRead(k1) == LOW)
{
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
}
if (digitalRead(k2) == HIGH and digitalRead(k1) == HIGH)
{
digitalWrite(led, HIGH);
}
}
Is there easier way to do that, cause when I assembled it the switches work very slow that blue led turn on after some time. Next I use the esp2866 but the relay for 3V doesn't turn on. How can I make it to work?

#Peter is true : avoid blocking delay code.
here is an example : I have a little simplified your code and added else if to make it more readable.
update1 :
int led = 12; // red led
int s1 = 9; //switch 1
int s2 = 10; //switch 1
int k1 = 3; // first blue led
int k2 = 2; // second blue led
int y1 = 11; // relay
unsigned long startTime1 = 0;
unsigned long startTime2 = 0;
unsigned long t200 = 200;
unsigned long t500 = 500;
const int led1Duration = 6000; // first blue led time
const int led2Duration = 12000; // second blue led time
void setup()
{
pinMode(led, OUTPUT);
pinMode(k1, OUTPUT);
pinMode(k2, OUTPUT);
pinMode(y1, OUTPUT);
pinMode(s1, INPUT);
pinMode(s2, INPUT);
}
void loop()
{
if (digitalRead(s1) && !digitalRead(s2)) {
digitalWrite(k1, HIGH);
digitalWrite(y1, HIGH);
startTime1 = millis();
blinkLed200();
} else if (!digitalRead(s1) && digitalRead(s2)) {
digitalWrite(k2, HIGH);
digitalWrite(y1, HIGH);
startTime2 = millis();
blinkLed500();
}
if (digitalRead(k1) && (millis() - startTime1 >= led1Duration)) {
digitalWrite(k1, LOW);
digitalWrite(y1, LOW);
digitalWrite(led, LOW);
}
if (digitalRead(k2) && (millis() - startTime2 >= led2Duration)) {
digitalWrite(k2, LOW);
digitalWrite(y1, LOW);
digitalWrite(led, LOW);
}
}
void blinkLed200() {
if (millis() - t200 > 200) { //tick every 200mS
t200 = millis();
digitalWrite(led, !digitalRead(led));
}
}
void blinkLed500() {
if (millis() - t500 > 500) { //tick every 500mS
t500 = millis();
digitalWrite(led, !digitalRead(led));
}
}
The 3V relays have a very low resistance and I am not sure that the digital outputs are powerful enough. Personally I always switch relays via a transistor.

Related

I don't know what's wrong with my code, random stuff happens

Hi I'm very much a beginner in the world of Arduino and I just got my Arduino Uno, I wanted to try something out, so I found some instructions on a traffic light. Pretty simple, but I wanted to ad a pedestrian crossing button. However, when I did that, without touching the button, after three cycles of green yellow red, it would act if I pushed the button. I have decided (from removing components) that the code is the problem. Does anybody know how to fix this?
int red = 10;
int yellow = 9;
int green = 8;
int button = 12;
int pedgreen = 11;
int pedred = 13;
int pedcross;
int count;
void setup(){
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
pinMode(pedgreen, OUTPUT);
pinMode(pedred, OUTPUT);
pinMode(button, INPUT);
}
void loop() {
pedcross = 0;
digitalWrite(pedred, HIGH);
pedcross = 0;
changeLights();
pedcross = 0;
}
void changeLights(){
pedc = 0;
digitalWrite(pedred, HIGH);
digitalWrite(yellow, LOW);
digitalWrite(red, LOW);
digitalWrite(green, HIGH);
count = 40;
while (count > 0){
if (digitalRead(button) == HIGH) {
delay(15);
if (digitalRead(button) == HIGH) {
pedcross = 1;
}
}
count = count - 1;
delay(100);
}
digitalWrite(yellow, HIGH);
digitalWrite(red, LOW);
digitalWrite(green, LOW);
count = 20;
while (count > 0){
if (digitalRead(button) == HIGH) {
delay(15);
if (digitalRead(button) == HIGH) {
pedcross = 1;
}
}
count = count - 1;
delay(100);
}
digitalWrite(yellow, LOW);
digitalWrite(red, HIGH);
digitalWrite(green, LOW);
if (pedcross == 1){
pedcross = 0;
pedcycle();
pedcross = 0;
}
else {
delay(5000);
}
}
void pedcycle(){
pedcross = 0;
digitalWrite(pedgreen, HIGH);
digitalWrite(pedred, LOW);
delay(3000);
digitalWrite(pedgreen, LOW);
digitalWrite(pedred, LOW);
delay(500);
digitalWrite(pedred, HIGH);
delay(500);
digitalWrite(pedred, LOW);
delay(500);
digitalWrite(pedred, HIGH);
delay(500);
digitalWrite(pedred, LOW);
delay(500);
digitalWrite(pedred, HIGH);
delay(500);
digitalWrite(pedred, LOW);
delay(500);
digitalWrite(pedred, HIGH);
delay(1500);
pedcross = 0;
}
You have to pull your input either up or down using a resistor, or you can use the built-in pull-up resistor of the Arduino by initializing the input pin like this:
pinMode(button, INPUT_PULLUP);

Arduino Nano and analog joystick

I have a game that consist of 4 direction of movement (up down left and right)
using Arduino Nano and analog joystick, seems like code is right as check before other posts.
This is the Arduino code:
byte x_axis = A3;
byte y_axis = A1;
byte btn1 = 8;
byte btn2 = 9;
byte btn3 = 10;
byte btn4 = 11;
byte btn5 = 12;
byte led = 13;
void setup(){
pinMode(x_axis, INPUT);
pinMode(y_axis, INPUT);
pinMode(btn1, INPUT);
pinMode(btn2, INPUT);
pinMode(btn3, INPUT);
pinMode(btn4, INPUT);
pinMode(btn5, INPUT);
digitalWrite(btn1, HIGH);
digitalWrite(btn2, HIGH);
digitalWrite(btn3, HIGH);
digitalWrite(btn4, HIGH);
digitalWrite(btn5, HIGH);
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
Serial.begin(9600);
}
void loop(){
Int read_x = analogRead(x_axis);
int read_y = analogRead(y_axis);
if(read_x > 600){
Serial.println("R");
digitalWrite(led, HIGH);
}
if(read_x < 400){
Serial.println("L");
digitalWrite(led, HIGH);
}
if(read_y > 600){
Serial.println("D");
digitalWrite(led, HIGH);
}
if(read_y < 400){
Serial.println("U");
digitalWrite(led, HIGH);
}
if(digitalRead(btn1) == LOW){
Serial.println("1");
digitalWrite(led, HIGH);
}
if(digitalRead(btn2) == LOW){
Serial.println("2");
digitalWrite(led, HIGH);
}
if(digitalRead(btn3) == LOW){
Serial.println("3");
digitalWrite(led, HIGH);
}
if(digitalRead(btn4) == LOW){
Serial.println("4");
digitalWrite(led, HIGH);
}
if(digitalRead(btn5) == LOW){
Serial.println("5");
digitalWrite(led, HIGH);
}
delay(10);
digitalWrite(led, LOW);
}
But when I use serial monitor to check it, it non stop show me U and L even without touching the joystick.
How can I fix this problem?
I'm feeling generous, so you have some jitter issues in the code and you really could use some cleanup. This code compiles. Ok now when a joystick is 0,0 in x/y physically it jitters in code. One thing you could do is remap out the jitter to give a wider center. print the raw analog values to the serial monitor and then map them out to your 0 point with a little padding. reference: https://www.arduino.cc/reference/en/language/functions/math/map/
byte x_axis = A3;
byte y_axis = A1;
byte btn[] = {8, 9, 10, 11, 12}; // 2,3,4,5,6
byte stat[] = {1, 2, 3, 4, 5};
byte led = 13;
int dval = 50;
void setup() {
pinMode(x_axis, INPUT);
pinMode(y_axis, INPUT);
for (int i = 0; i < 5; i++) {
pinMode(btn[i], INPUT);
digitalWrite(btn[i], HIGH);
}
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
Serial.begin(9600);
}
void loop() {
int read_x = analogRead(x_axis);
delay(dval);
int read_y = analogRead(y_axis);
delay(dval);
if (read_x > 600) {
Serial.println("R");
digitalWrite(led, HIGH);
}
if (read_x < 400) {
Serial.println("L");
digitalWrite(led, HIGH);
}
if (read_y > 600) {
Serial.println("D");
digitalWrite(led, HIGH);
}
if (read_y < 400) {
Serial.println("U");
digitalWrite(led, HIGH);
}
for (int i = 0; i < 5; i++) {
if (digitalRead(btn[i]) == LOW) {
Serial.println(stat[i]);
digitalWrite(led, HIGH);
}
}
delay(10);
digitalWrite(led, LOW);
}
Emad joon:
1- Check that the ground of your joy stick and the Vdd is connected to your arduino.
2- connect the x and y of the joy stick to the arduino analog inputs.
use this code as starter:
#define X_AXIS A1
#define Y_AXIS A3
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("X = ");
Serial.println(analogRead(X_AXIS);
Serial.print("Y = ");
Serial.println(analogRead(Y_AXIS);
delay(150);
}
See what values you get when your Joy Stick is at initial position. You can build up on this code for buttons.
Also, do not forget to use debouncing for your button readings:
if(digitalRead(Button1)==0){
delay(40);
if(digitalRead(Button1==0){
buttonPressed=true;
}
}

How to add the Interrupt(); function to arduino code

For a small school project I am building a simulation of a traffic light system, based on three directions. What makes the system special is it can detect vehicles waiting in front of the traffic light. When a vehicle is detected, the given direction will get a green light. To do so I make use of a Hall Effect sensor.
Right now I am stuck on the following problem; the Arduino stops detecting the state of the sensor while giving a certain direction a green light. I already have read about the Interrupt() function to do certain tasks simultaneously but didn't manage to implement it in my code.
Hope you guys know a way to do so!
int sensorPin3 = 2;
int sensorPin2 = 3;
int sensorPin1 = 4;
int g1 = 11;
int y1 = 12;
int r1 = 13;
int g2 = 8;
int y2 = 9;
int r2 = 10;
int g3 = 5;
int y3 = 6;
int r3 = 7;
int counter = 0;
boolean sensorState1 = false;
boolean sensorState2 = false;
boolean sensorState3 = false;
void setup()
{
// setup serial - diagnostics - port
Serial.begin(9600);
pinMode(sensorPin1, INPUT);
pinMode(sensorPin2, INPUT);
pinMode(sensorPin3, INPUT);
digitalWrite(sensorPin1, HIGH);
digitalWrite(sensorPin2, HIGH);
digitalWrite(sensorPin3, HIGH);
pinMode(g1, OUTPUT);
pinMode(y1, OUTPUT);
pinMode(r1, OUTPUT);
pinMode(g2, OUTPUT);
pinMode(y2, OUTPUT);
pinMode(r2, OUTPUT);
pinMode(g3, OUTPUT);
pinMode(y3, OUTPUT);
pinMode(r3, OUTPUT);
}
void loop()
{
digitalWrite (r1, HIGH);
digitalWrite (r2, HIGH);
digitalWrite (r3, HIGH);
if(magnetPresent(sensorPin1) && !sensorState1)
{
sensorState1 = true;
}
else if(!magnetPresent(sensorPin1) && sensorState1)
{
if(sensorState1 = false);
Serial.println("detect1");
richting1();
}
if(magnetPresent(sensorPin2) && !sensorState2)
{
sensorState2 = true;
}
else if(!magnetPresent(sensorPin2) && sensorState2)
{
if (sensorState2 = false);
Serial.println("detect2");
richting2();
}
if(magnetPresent(sensorPin3) && !sensorState3)
{
sensorState3 = true;
}
else if(!magnetPresent(sensorPin3) && sensorState3)
{
if (sensorState3 = false);
Serial.println("detect3");
richting3();
}
}
void printMessage(String message){
counter++;
Serial.print(counter);
Serial.print(" ");
Serial.println(message);
}
boolean magnetPresent(int pin){
return digitalRead(pin) == LOW;
}
void richting1()
{
digitalWrite (r1, HIGH);
digitalWrite (r2, HIGH);
digitalWrite (r3, HIGH);
delay(2000);
digitalWrite (r1, LOW);
digitalWrite (r2, HIGH);
digitalWrite (r3, HIGH);
digitalWrite (g1, HIGH);
delay(10000);
digitalWrite (y1, HIGH);
digitalWrite (r2, HIGH);
digitalWrite (r3, HIGH);
digitalWrite (g1, LOW);
delay(2000);
digitalWrite (y1, LOW);
digitalWrite (r1, HIGH);
}
void richting2()
{
digitalWrite (r1, HIGH);
digitalWrite (r2, HIGH);
digitalWrite (r3, HIGH);
delay(2000);
digitalWrite (r2, LOW);
digitalWrite (r1, HIGH);
digitalWrite (r3, HIGH);
digitalWrite (g2, HIGH);
delay(10000);
digitalWrite (y2, HIGH);
digitalWrite (r1, HIGH);
digitalWrite (r3, HIGH);
digitalWrite (g2, LOW);
delay(2000);
digitalWrite (y2, LOW);
digitalWrite (r2, HIGH);
}
void richting3()
{
digitalWrite (r1, HIGH);
digitalWrite (r2, HIGH);
digitalWrite (r3, HIGH);
delay(2000);
digitalWrite (r3, LOW);
digitalWrite (y3, LOW);
digitalWrite (r1, HIGH);
digitalWrite (r2, HIGH);
digitalWrite (g3, HIGH);
delay(10000);
digitalWrite (y3, HIGH);
digitalWrite (r1, HIGH);
digitalWrite (r2, HIGH);
digitalWrite (g3, LOW);
delay(2000);
digitalWrite (y3, LOW);
digitalWrite (r3, HIGH);
}
#NickGammon made a great posts about interrupts and how to use them. It is more then complete so here is the link:
https://arduino.stackexchange.com/questions/30968/how-do-interrupts-work-on-the-arduino-uno-and-similar-boards
This is just a little opinion, but for traffic light codes, I found it a little simpler to make something like a custom delay. Take a look at this code and you'll hopefully understand it. Again, this was one of my school project in which I've used a template so you might see some useless crap in it!
/* This program acts like a pedestrian light. If the button is pressed, a light will turn on for a certain moment to let the person pass.
By : Dat HA
Date : 16/09/27
*/
//****************************** VARIABLES ******************************
const int leds[4][12] = //declaring leds - RED, YELLOW, GREEN
{
//R,Y,G,
{5, 6, 7}, //north
{8, 9, 10}, //east
{11, 12, 13}, //south
{2, 3, 4} //west
};
/* Sensors: sensorNumber - itemInArray - sensorDescription - pinDescription
1 - 0 - pushbutton
2 - 1 - photocell
3 - 2 - potentiometer
4 - 3 - distance - distanceEcho
5 - 4 - distance - distanceTrigger
6 - 5 - pushbutton (2)
7 - 6 - servo
*/
const int sensors[] = {A2, A7, A6, A4, A5, A3, A0}; //pin for each sensor
//****************************** SETUP ******************************
const int analogPins[6] = {A0, A1, A2, A3, A4, A5}; //for quick pinMode, distanceTrigger we be redeclared as an output is it is an analog input pin
void setup() {
for (int i = 2; i < 14; i++) //declaring digital pins as output for leds
{
pinMode(i, OUTPUT);
}
for (int i = 0; i < 6; i++) //declaring analog inputs
{
pinMode(analogPins[i], INPUT);
}
pinMode(sensors[4], OUTPUT); //declaring trigger pin as an output
}
//****************************** MAIN LOOP ******************************
int green = 5000;
int yellow = 3000;
int aa = 0;
void loop() {
//north, south
digitalWrite(leds[0][0], LOW); //red light off
digitalWrite(leds[2][0], LOW);
digitalWrite(leds[0][2], HIGH); //green light on
digitalWrite(leds[2][2], HIGH);
wait(green);
digitalWrite(leds[0][2], LOW); //green light off
digitalWrite(leds[2][2], LOW);
digitalWrite(leds[0][1], HIGH); //yellow light on
digitalWrite(leds[2][1], HIGH);
wait(yellow);
digitalWrite(leds[0][1], LOW); //yellow light off
digitalWrite(leds[2][1], LOW);
digitalWrite(leds[0][0], HIGH); //red light on
digitalWrite(leds[2][0], HIGH);
delay(100);
if (aa == 1)
{
flash();
}
//*************** SWITCHING SIDES ***************
digitalWrite(leds[1][0], LOW); //red light off
digitalWrite(leds[3][0], LOW);
digitalWrite(leds[1][2], HIGH); //green light on
digitalWrite(leds[3][2], HIGH);
wait(green);
digitalWrite(leds[1][2], LOW); //green light off
digitalWrite(leds[3][2], LOW);
digitalWrite(leds[1][1], HIGH); //yellow light on
digitalWrite(leds[3][1], HIGH);
wait(yellow);
digitalWrite(leds[1][1], LOW); //yellow light off
digitalWrite(leds[3][1], LOW);
digitalWrite(leds[1][0], HIGH); //red light on
digitalWrite(leds[3][0], HIGH);
delay(100);
if (aa == 1)
{
flash();
}
}
//****************************** FUNCTIONS ******************************
/* Usable functions list:
LEDS functions - do something to the leds
ledClear() turn all leds off
ledRedOn() turn all red leds on
ledYelOn() turn all yellow leds on
ledGreOn() turn all green leds on
SENSORS functions - they will return the apropriate value
pushButton() values: 0,1
photocell() values: 0-1023
potentiometer() values: 0-1023
distance() values: 0-200
*/
void ledClear() //turn off all leds
{
for (int i = 2; i < 14; i++)
{
digitalWrite(i, LOW); //turning leds off
}
}
void ledRedOn()
{
for (int i = 0; i < 4; i++)
{
digitalWrite(leds[i][0], HIGH); //turning red leds on
}
}
void ledRedOff()
{
for (int i = 0; i < 4; i++)
{
digitalWrite(leds[i][0], LOW); //turning red leds on
}
}
void ledYelOn()
{
for (int i = 0; i < 4; i++)
{
digitalWrite(leds[i][1], HIGH); //turning yellow leds on
}
}
void ledGreOn()
{
for (int i = 0; i < 4; i++)
{
digitalWrite(leds[i][2], HIGH); //turning green leds on
}
}
int pushButton() // sensor 1
{
int x = digitalRead(sensors[0]);
return x; //returning digital value of pushbutton which is either 0 or 1
}
void flash() //flash the red led
{
for (int i = 0; i < 12; i++)
{
ledGreOn();
ledRedOn(); //on
delay(200); //wait
ledRedOff(); //off
delay(200); //wait
}
ledClear();
aa = 0;
}
int wait(int x) //see if the wire is disconnected and if so, flash the red led
{
int i = 0;
unsigned long currentMillis = millis(); //current time
while (millis() - currentMillis <= x) //delay
{
if (digitalRead(A2) == HIGH) //if wire disconnected
{
aa = 1;
}
}
}

How to write a state change (edge detection) using analogRead() on Arduino?

I'm having trouble creating a toggle switch using an analog input connected to an RF receiver. I'm attempting to make a servo turn 180 degrees when the voltage reads higher than 800 and stay at 180 degrees until the button is pressed again. It only reads higher than 800 when I press a button on my key fob (RF transmitter). In my code it does this, but it doesn't hold at 180 degrees and wait until the button is pressed again to go back to 0 degrees.
#include <Servo.h>
Servo myservo;
const int analogInPin = A0;
int led = 13;
float sensorValue = 0;
void setup(){
Serial.begin(2400);
pinMode(led, OUTPUT);
myservo.attach(6);
myservo.write(0);
}
void loop(){
sensorValue = analogRead(analogInPin);
Serial.print("Voltage Output = ");
Serial.print(sensorValue);
Serial.println(" ");
delay(100);
if (sensorValue > 800) {
digitalWrite(led, HIGH);
myservo.write(180);
delay(100);
}
else{
digitalWrite(led, LOW);
myservo.write(0);
delay(100);
}
}
EDIT: I'm pretty close with this edited code. I added a variable and an if statement. It turns the LED on and it stays on, but when pressed again it doesn't turn off. So close...
const int analogInPin = A0;
int led = 13;
int ledState = 0;
float sensorValue = 0;
void setup(){
Serial.begin(2400);
pinMode(led, OUTPUT);
}
void loop(){
sensorValue = analogRead(analogInPin);
Serial.print("Voltage Output = ");
Serial.print(sensorValue);
Serial.println(" ");
delay(100);
if (sensorValue < 400 && ledState == 0) {
digitalWrite(led, HIGH);
delay(500);
ledState == 1;
}
if (sensorValue < 400 && ledState == 1) {
digitalWrite(led, LOW);
delay(500);
ledState == 0;
}
}
OK. I think I solved it! Take a look. It now turns the LED on when the button is pressed and stays on until the button is pressed again. This is how to create a switch using an analog input reading.
const int analogInPin = A0;
int led = 13;
int ledState = 0;
float sensorValue = 0;
void setup(){
Serial.begin(2400);
pinMode(led, OUTPUT);
}
void loop(){
sensorValue = analogRead(analogInPin);
Serial.print("Voltage Output = ");
Serial.print(sensorValue);
Serial.println(" ");
delay(100);
if (sensorValue < 400 && ledState == 0) {
digitalWrite(led, HIGH);
delay(100);
ledState = 1;
Serial.println(ledState);
delay(500);
}
else {
if (sensorValue < 400 && ledState == 1) {
digitalWrite(led, LOW);
delay(100);
ledState = 0;
Serial.println(ledState);
delay(500);
}
}
}

Arduino, if else LED "stuck"

So the code is not working properply, there are two leds that wont turn off "highliten" the problem. when i run the Else part of the program. i want to turn them off in the else part. :)
include
byte ledPin[] = {8, 9, 10, 11, 12, 13}; //--------------------------------.
int ledDelay; // Del 1
int direction = 1;
int currentLED = 0;
unsigned long changeTime;
int potPin = 0;
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int va;
void setup()
{
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, INPUT);
myservo.attach(3); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
for (int x=0; x<6; x++) {
pinMode(ledPin[x], OUTPUT); }
changeTime = millis();
}
void loop() {
int on = digitalRead(6);
if (on == HIGH)
{
myservo.attach(3);
// Here is the problem!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if va < 523)
{
digitalWrite(5, HIGH);
}
else if (va > 555)
{
digitalWrite(4, HIGH);
}
else
{
digitalWrite(4, LOW);
digitalWrite(5, LOW);
}
// Here is the problem!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
va = analogRead(potPin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(va, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(1); // waits for the servo to get there
}
else
{
myservo.detach();
digitalWrite(5, LOW);
digitalWrite(4, LOW);
ledDelay = analogRead(potPin) / 4;
if ((millis() - changeTime) > ledDelay)
{
changeLED();
changeTime = millis();
}
}
}
void changeLED() {
for (int x=0; x<6; x++)
{
digitalWrite(ledPin[x], LOW);
}
digitalWrite(ledPin[currentLED], HIGH);
currentLED += direction;
if (currentLED == 6) {direction = -1;}
if (currentLED == 0) {direction = 1;}
}
in advance Thank you!
Right at the end of the sketch you have the following line:
if (currentLED == 6) { direction = -1; }
I think, without actually running the program, that the problem is here. In the previous line you have added one to the value of currentLED and you are checking to see if you have gone off the end of the ledPin array. You change the direction but you don't reset the currentLED position to be back inside the ledPin range.
The next time changeLED is called it tries to call digitalWrite(ledPin[currentLED], HIGH); but the value of currentLED is 6, which is outside the ledPin array. The Arduino probably gets upset at this point.
I think you simply need to change the statement to check whether currentLED == 5 rather than 6. This will mean that next time that changeLED is called the last LED will be turned on and the value of currentLED will be decremented (direction == -1), keeping it inside the ledPin range.

Resources