Don't ad-block us - support your favorite websites. We have safe, unobstrusive, robotics related ads that you actually want to see - see here for more.
0 Members and 1 Guest are viewing this topic.
case 101:break; case 110:break;
int s[1]; //2 sensorsvoid setup(){ pinMode(2, INPUT); pinMode(12, INPUT); pinMode(13, OUTPUT);}void loop(){ s[0]= digitalRead(2); s[1]= digitalRead(12); switch (s[]){case {1,0}:digitalWrite(13, LOW);//led connected to pin 13break;case {1,1}:digitalWrite(13, HIGH);break;}}
int s; //2 sensorsvoid setup(){ pinMode(2, INPUT); pinMode(12, INPUT); pinMode(13, OUTPUT);}void loop(){ s = digitalRead(2); //Read Pin 2 into s s = (s << 1) + digitalRead(12); //Shift s one and add pin 12 //At this point s contains a value from 0 - 3 which is a 2 bit representation of the two pins. //Use s in your switch statement and act on it as a single int.switch (s){case 0: // This is the { 0,0 } casebreak;case 1: // This is the { 0,1 } casebreak;case 2: //This is the { 1,0 } casedigitalWrite(13, LOW);//led connected to pin 13break;case 3: //This is the { 1,1 } case.digitalWrite(13, HIGH);break;default: break}}
You can not use an array with a case statement. The easiest method would be to convert your input data into a single int using shift and add. Here is a modified verions of your original code.Code: [Select]int s; //2 sensorsvoid setup(){ pinMode(2, INPUT); pinMode(12, INPUT); pinMode(13, OUTPUT);}void loop(){ s = digitalRead(2); //Read Pin 2 into s s = (s << 1) + digitalRead(12); //Shift s one and add pin 12 //At this point s contains a value from 0 - 3 which is a 2 bit representation of the two pins. //Use s in your switch statement and act on it as a single int.switch (s){case 0: // This is the { 0,0 } casebreak;case 1: // This is the { 0,1 } casebreak;case 2: //This is the { 1,0 } casedigitalWrite(13, LOW);//led connected to pin 13break;case 3: //This is the { 1,1 } case.digitalWrite(13, HIGH);break;default: break}}Hope this helps
Ok i understand. But does it do a binary addition or decimal addition?Like what does it do when it comes across an addition like 101+1. Does it give 110 or 102?
Started by ishka Software
Started by saba_rish91 Software
Started by obiwanjacobi Software