Hello everyone this is my first post and i am totally new to arduino projects i am learning basic sketch and need help on a project i am learning.
Project- i know how to flash led using the sketch output pin high or low and using delay to generate pattern, but i want a toggle switch to toggle between different patterns of led in the sketch.
When the toggle switch is pressed first time- pattern 1
When pressed 2nd time pattern 2
When pressed 3rd time pattern 3
And so on, and when pressed at last pattern goes again to 1st pattern.
Sorry for bad English. It will be nice if someone help with the sketch.thank you
Well, have you looked at the digitalRead() function? Can you see how that could look at the HIGH/LOW state of a switch?
One tip that's not mentioned in that reference is you should connect the switch between the pin and ground and set the pinMode() to INPUT_PULLUP. That way the pin will read LOW when the switch is closed.
Have you looked at the if(){...}else{...} construct? Can you see how that could use a value from a digitalRead() to make a decision on what piece of code to run?
To be able to select different patterns depending on how many times the switch was pressed, you would need a counter to count or possibly a State Machine.
In addition to what @MorganS has said you will need some means to store the different patterns.
That might involve creating a different function to produce each pattern and calling the appropriate function depending on the number in the variable that counts the switch presses.
Another way might be to have an array with a row of data that defines each sequence.
And, if you want to be able to change from one sequence to another instantly do NOT use delay() to manage the timing of the sequence. Have a look at how millis() is used to manage timing without blocking in several things at a time
here is my sketch what i have wrote
void setup() {
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
}
//PATTERN 1
void loop() {
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
delay(100);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
delay(100);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
delay(100);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
delay(100);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
delay(100);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
delay(500);
}
//PATTERN 2
void loop() {
digitalWrite(7, HIGH);
delay(80);
digitalWrite(7, LOW);
delay(80);
digitalWrite(7, HIGH);
delay(80);
digitalWrite(7, LOW);
delay(80);
digitalWrite(8, HIGH);
delay(80);
digitalWrite(8, LOW);
delay(80);
digitalWrite(8, HIGH);
delay(80);
digitalWrite(8, LOW);
delay(80);
}
//PATTERN 3
void loop() {
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
delay(100);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
delay(100);
}
now in this sketch to which i want to add a single toggle switch to toggle between the pattern 1, 2, and 3
what additional code should i wirte or make changes.
please help me with modifing my code.
and thank you Robin2 and MorganS for reply.
Swapnils456:
please help me with modifing my code.
and thank you Robin2 and MorganS for reply.
Have you thought about how you might use the information we have already given you?
And have you tried implementing it?
...R
void setup() {
int ledPin1 = 7; // LED connected to digital pin 7
int ledPin2 = 8;// LED connected to digital pin 8
int inPin = 6; // pushbutton connected to analog pin 6
int val = 0; // variable to store the read value
void setup()
{
pinMode(ledPin1, OUTPUT); // sets the digital pin 7 as output
pinMode(ledPin2, OUTPUT); // sets the digital pin 8 as output
INPUT_PULLUP(inPin, INPUT); // sets the analog pin 6 as input
}
void loop()
{
digitalRead(inPin); // read the input pin
digitalWrite(ledPin1, val); // sets the LED to the button's value
digitalWrite(ledPin2, val); // sets the LED to the button's value
}
}
//PATTERN 1
void loop() {
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
delay(100);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
delay(100);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
delay(100);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
delay(100);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
delay(100);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
delay(500);
}
//PATTERN 2
void loop() {
digitalWrite(7, HIGH);
delay(80);
digitalWrite(7, LOW);
delay(80);
digitalWrite(7, HIGH);
delay(80);
digitalWrite(7, LOW);
delay(80);
digitalWrite(8, HIGH);
delay(80);
digitalWrite(8, LOW);
delay(80);
digitalWrite(8, HIGH);
delay(80);
digitalWrite(8, LOW);
delay(80);
}
//PATTERN 3
void loop() {
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
delay(100);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
delay(100);
}
}
this is what i came too
i am a self learner about programming and have no knowlege of programming
need help with it
when push button is pressed for 1st time pattern 1 should start and keep looping, when push button is pressed again pattern 2 should start and keep looping, when push button pressed again pattern 3 should start and keep looping and this should go on when ever the push button is pressed. that is when button pressed from pattern 1 to 2, 2 to 3, 3 to 1 and so on. how can i implement this in my code.
i am a self learner about programming and have no knowlege of programming
Yep we are all self learners.
The first thing to learn is to learn how to post code on this forum.
Please read this:-
How to use this forum
Because your post is breaking the rules about posting code
The second thing to understand is to read the replies and try and understand them. If you don't understand them then ask about what you don't understand.
The third thing to learn is that as long as you use the delay function you can't do anything else like break out of a sequence.
The fourth is to learn about state machines. See my
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
Or Robin2's several things at once
http://forum.arduino.cc/index.php?topic=223286.0
need help with it
No, you want us to do it for you and give you the answer. You are lazy. You did not read the forum rules, and you have broken them twice already. You are rude.
I am extremely sorry about breaking the rules of forum i am weak with understanding and writing English. Now on i will take as much time it will require to understand everything valuable eveyone has replied regarding my project and work on it. Sorry for my mistakes. Thank you very much for guidance. I will again start from scratch and appreciate you help. I am using google translator to understand each and every word and even to write anything. I will not break any rule now on.
You could make a good start by editing the posts you have made to include the correct code tags, it is the </> icon in the reply box you need to use.
Thank you Grumpy_Mike, PaulRB, Robin2 and MorganS for guidance i am working on my mistakes and understanding.