Skip to main content
added 111 characters in body
Source Link
chrisl
  • 16.6k
  • 2
  • 18
  • 27

I have been trying many variations of code like this, cannot seem to get this to work with how I logically would think it would work. Am I missing something with syntax or breaking some rules? Everything works independently but not jointly.

int switchS = 0;

int var = 1;


void setup() {

pinMode(1,INPUT);

pinMode(7,OUTPUT);

digitalWrite(7,LOW); }


void loop() {
switchS = digitalRead(1);
while(var == 1)
{
digitalWrite(7,HIGH);
delay(100);
digitalWrite(7,LOW);
delay(100);
if(switchS == HIGH)
{
var = 0;
}
}
delay(5000);
var = 1; }

int switchS = 0;
int var = 1;   
    
void setup() {
    pinMode(1,INPUT);
    pinMode(7,OUTPUT);
    digitalWrite(7,LOW);
}

void loop() {  
    switchS = digitalRead(1);  
    while(var == 1)  
    {  
        digitalWrite(7,HIGH);  
        delay(100);  
        digitalWrite(7,LOW);  
        delay(100);  
        if(switchS == HIGH)  
        {  
            var = 0;  
        }  
    }  
    delay(5000);  
    var = 1;
}

I have been trying many variations of code like this, cannot seem to get this to work with how I logically would think it would work. Am I missing something with syntax or breaking some rules? Everything works independently but not jointly.

int switchS = 0;

int var = 1;


void setup() {

pinMode(1,INPUT);

pinMode(7,OUTPUT);

digitalWrite(7,LOW); }


void loop() {
switchS = digitalRead(1);
while(var == 1)
{
digitalWrite(7,HIGH);
delay(100);
digitalWrite(7,LOW);
delay(100);
if(switchS == HIGH)
{
var = 0;
}
}
delay(5000);
var = 1; }

I have been trying many variations of code like this, cannot seem to get this to work with how I logically would think it would work. Am I missing something with syntax or breaking some rules? Everything works independently but not jointly.

int switchS = 0;
int var = 1;   
    
void setup() {
    pinMode(1,INPUT);
    pinMode(7,OUTPUT);
    digitalWrite(7,LOW);
}

void loop() {  
    switchS = digitalRead(1);  
    while(var == 1)  
    {  
        digitalWrite(7,HIGH);  
        delay(100);  
        digitalWrite(7,LOW);  
        delay(100);  
        if(switchS == HIGH)  
        {  
            var = 0;  
        }  
    }  
    delay(5000);  
    var = 1;
}
Source Link

Nesting loops - if statement in a while loop

I have been trying many variations of code like this, cannot seem to get this to work with how I logically would think it would work. Am I missing something with syntax or breaking some rules? Everything works independently but not jointly.

int switchS = 0;

int var = 1;


void setup() {

pinMode(1,INPUT);

pinMode(7,OUTPUT);

digitalWrite(7,LOW); }


void loop() {
switchS = digitalRead(1);
while(var == 1)
{
digitalWrite(7,HIGH);
delay(100);
digitalWrite(7,LOW);
delay(100);
if(switchS == HIGH)
{
var = 0;
}
}
delay(5000);
var = 1; }