Please modify your post to post code properly. First delete all the code from your post. Start the IDE, if it is not running. Use Tools + Auto format to make the code presentable. Delete the extraneous white space. Then, select the # icon and then paste your formatted code.
If you put each { on its own line, I think you'll find the structure of the program easier to see/understand. Certainly the extraneous curly braces will be easier to see. Delete them.
Why switch from the poor val to the ever worse button prefix? You have switches attached to the Arduino and buttons attached to shirts.
delay(100);
That's an incredibly long time for a switch to bounce.
if (button0 != buttonState1)
Really, now, names like currState and prevState make it clear what is being compared here. button0 and buttonState1 do not.
the above programme hangs inthe middle from swithpin4 hangs
How do you know this? There are no debug outputs that indicate that you are properly reading any switches, or where the program is getting to, or what it is doing.
You are not activating the internal pullup resistors, so external resistors are required - either pullup or pulldown. Which do you have? How are the switches wired?
int buttonState1 = LOW;
int button0, button1;
int buttonState2 = LOW;
int button2, button3;
int led8Pin = 8;
int led4Pin = 4;
int led6Pin = 6;
int led11Pin = 11;
int led0Pin = 13;
int switchPin1= 3;
int switchPin2= 7;
int switchPin3= 9;
int switchPin4= 10;
int val1;
int val2;
void setup() {
Serial.begin(9600);
pinMode(switchPin1, INPUT);
digitalWrite(switchPin1, HIGH);
pinMode(switchPin2, INPUT);
digitalWrite(switchPin2, HIGH);
pinMode(switchPin3, INPUT);
digitalWrite(switchPin3, HIGH);
pinMode(switchPin4, INPUT);
digitalWrite(switchPin4, HIGH);
pinMode(led0Pin, OUTPUT);
pinMode(led8Pin, OUTPUT);
pinMode(led4Pin, OUTPUT);
pinMode(led6Pin, OUTPUT);
pinMode(led11Pin, OUTPUT);
digitalWrite(led8Pin, LOW);
digitalWrite(led4Pin, LOW);
digitalWrite(led6Pin, LOW);
}
void loop()
{ val1 = digitalRead(switchPin1);
delay(50);
if (val1 == LOW) {
digitalWrite(led8Pin, HIGH);
digitalWrite(led0Pin, HIGH);
delay(1000);
digitalWrite(led0Pin, LOW);
delay(1000);
digitalWrite(led11Pin, HIGH);
delay(2000);
digitalWrite(led11Pin, LOW);
delay(18000);
digitalWrite(led8Pin, LOW);
}
val2 = digitalRead(switchPin2);
if (val2 == LOW) {
digitalWrite(led11Pin, HIGH);
delay(800);
digitalWrite(led11Pin, LOW);
delay(2000);
}
{ button0 = digitalRead(switchPin3);
delay(100);
button1 = digitalRead(switchPin3);
if (button0 == button1) {
if (button0 != buttonState1) {
buttonState1 = button0;
if (buttonState1 == LOW) {
Serial.println("D");
digitalWrite(led4Pin, HIGH);
delay(2000);
digitalWrite(led4Pin, LOW);
delay(2000);
digitalWrite(led11Pin, HIGH);
delay(800);
digitalWrite(led11Pin, LOW);
delay(2000);
}
}
}
}
{
button2 = digitalRead(switchPin4);
delay(300);
button3 = digitalRead(switchPin4);
if (button2 == button3) {
if (button2 != buttonState2) {
buttonState2 = button2;
if (buttonState2 == HIGH) {
Serial.println("B");
digitalWrite(led6Pin, HIGH);
delay(2000);
digitalWrite(led6Pin, LOW);
delay(1000);
}
}
}
}
}
Moderator edit: I added CODE TAGS and merged the two threads because I thought they looked similar. If they're not, let me know and I'll split them again.
please help me the above program works correctly but some times it hangs in the middle the upper half works correctly but when the button program hangs some times it has give o/p when i/p goes high to low i.e edge trigger similarly for button2 it has give o/p when i/p goes low to high edge triggar for both buttons i/p is same
yes i will simply explain the requirement ok iam having i/p pin1 when it is low the o/p1 must go high for 1000ms and low, for i/p 2 when it is low the o/p2 must go high for 1000ms and low, similarly iam having 2button pins if buttonpin 1 goes low to high the o/p3 must go high for 1000ms and low,if buttonpin 2 goes high to low the o/p4 must go high for 1000ms and low where as the both buttons i/p is common,this is single program please suggest me the code thanking you in advance hoping for solution
Right now. Let's get rid of some abbreviations. I don't understand "o/p1", and "iam" is another strange word.
Try making multiple sentences, what you wrote could mean almost anything. I am guessing that in response to an input you want an output to change. Can you try doing that in multiple lines? Try to imagine that we can't read your mind, and you need to explain things in simple terms.
Try and take a little care with what you are writing. For example, "I" is capitalized in English. Taking care is a sign of respect to the people you are hoping will reply.
dear sir my english is littlebit poor hope excuse me if found anything wrong
as said program :
when input 1 is low
the output 1 should go high for delay 1000ms and to low state
when input 2 is low
the output 2 should go high for delay 1000ms and to low state
similarly for two pushbuttons connected to common output
when button1 is pressed
i.e when input goes from low state to high the output 3 must go high for 1000ms and to low
when button2 is pressed
i.e when input goes from low state to high the output 4 must go high for 1000ms and to low
That will do whatever thousands of times a second, whenever val1 is low. Is that really what you want? Or do you mean, when it WAS high, and is NOW low? If so, you didn't do that.
Why not? You are not detecting a transition of the switch state, which you need to do.
To do this, you need to keep track of the previous state.
int prevState1 = HIGH; // Keep track of previous state of pin 1
void loop()
{
int currState1 = digitalRead(switchPin1);
if(currState1 != prevState1)
{
// A transition occurred
if(currState1 == LOW)
{
// The transition is to pressed
}
else
{
// The transition is to released
}
}
prevState1 = currState1;
}
Now that you know when a transition occurs, you can take action only once when the switch is pressed, only once when the switch is released, or whenever the switch is pressed or released.
You can add the code to set the pin HIGH, twiddle your thumbs and do nothing for a second, then set the pin LOW.
Or, you can get smart and skip the delays and simply set the pin HIGH, record when it was set HIGH, and use millis() to determine if it is time to turn the pin back off. This would allow multiple output pins to be HIGH at once.
Using delay will allow only one at a time to be HIGH.
Sounds like you want to use the Blink without delay method here, get rid of all the delays, make the program more responsive to button pushes.
unsinged long button1starttime;
void setup(){
// pinmodes, all that stuff
}
void loop(){
if ( (digital Read (button1) == 1) & (output1state == LOW) )
{ // if button is pressed, and LED1 is not currently on:
button1starttime = millis(); // capture the time
output1state = HIGH; // set a flag showing the new state
digitalWrite (output1pin, output1state); // change the state of the out pin
}
if ( (output1state == HIGH) & ( millis() >= (button1starttime + 1000) ) )
{ // if the output is HIGH, and enough time has passed:
output1state = LOW; // set the flag showing the new state
digitalWrite (output1pin, output1state); // change the state of the out pin
}
// repeat for the other buttons/inputs
}