micro switches act as binary

ok, here's my idea, just need help on the execution. I'm using a microswitch block with 4 switches.
I want them to act like binary count.

when HIGH
switchA = 1
switchB = 2
switchC = 4
switchD = 8

where/how exactly would I add that to my code?
I'm also going to use these total values to control a function X amount of time

function is

 digitalWrite(relay,HIGH);
 delay(delayValueH * 10);
 digitalWrite(relay,LOW);
 delay(delayValueL * 10);

Main Code.... so far

const int relay = 2; // digitalWrite to relay coil
const int sensor = 7; // on/off sensor
int onTime = A0; // pot to determine digitalWrite HIGH
int offTime = A2; // pot to determin digitalWrite LOW
const int switchA = 22; // on/off switch A value of 1 when high
const int switchB = 26; // on/off switch B value of 2 when high
const int switchC = 30; // on/off switch C value of 4 when high
const int switchD = 34; // on/off switch D value of 8 when high
int switchAvalue // 1
int switchBvalue // 2
int switchCvalue // 4
int switchDvalue // 8
long delayValueH; // delay high value
long delayValueL; // delay low value



void setup() {
  pinMode(relay,OUTPUT);
  pinMode(sensor,INPUT);
  pinMode(countA,INPUT);
  pinMode(countB,INPUT);
  pinMode(countC,INPUT);
  pinMode(countD,INPUT);
  delayValueH = (analogRead,onTime);
  delayValueL = (analogRead,offTime);

You seem to have invented your own syntax and a whole lot of it is wrong.
Please work through basic examples and note how things are done.

You have no loop(). You never declared/defined countA etc. The 'int switchAvalue' is rubbish, has no ; and errors should have shown up in the IDE if you bothered verifying.

Using a UNO or Nano, I would connect switches 1 - 4 to pins 8,9,10,11 then read PINB:

switchesVal = PINB & 0x0F;

edgemoron:
Using a UNO or Nano, I would connect switches 1 - 4 to pins 8,9,10,11 then read PINB:

switchesVal = PINB & 0x0F;

I'm using an Elegoo Mega 2560.

@ INTP

ok, I removed them. I'm new to coding, I figured I had to make a "value" int so that it would assign a value to the HIGH from that pin (similar to the analog situation in my code)....other than that what syntax was made up? (not trying to be a smart @$$, just trying to learn) can you point me in some sort of direction on how to assign those values and make them count my "function" as needed??

Well, you don't need a 16 bit "int" to hold 4 bits, a "byte" that holds 8 is big enough, I'm not familiar with the Mega's port scheme and pinouts though.
BTW, did you shake those pin numbers out of a bottle? They should be consecutive. :slight_smile:

LandonW:
ok, here's my idea, just need help on the execution. I'm using a microswitch block with 4 switches.
I want them to act like binary count.

when HIGH
switchA = 1
switchB = 2
switchC = 4
switchD = 8

byte value = (digitalRead (switchD) ? 8 : 0) |
             (digitalRead (switchC) ? 4 : 0) |
             (digitalRead (switchB) ? 2 : 0) |
             (digitalRead (switchA) ? 1 : 0);

Although it would simpler to use an array for the switch pins:

byte value = 0 ;
for (byte i = 0 ; i < 4 ; i++)
  value = (value << 1) | (digitalRead (switches[i]) ? 1 : 0) ;

More like:

const byte relayPin = 2; // digitalWrite to relay coil
const byte sensorPin = 7; // on/off sensor
const byte onTimePin = A0; // pot to determine digitalWrite HIGH
const byte offTimePin = A2; // pot to determin digitalWrite LOW
const byte switchApin = 22; // on/off switch A value of 1 when high
const byte switchBpin = 26; // on/off switch B value of 2 when high
const byte switchCpin = 30; // on/off switch C value of 4 when high
const byte switchDpin = 34; // on/off switch D value of 8 when high

long delayValueH; // delay high value
long delayValueL; // delay low value

void setup() {
  pinMode(relayPin, OUTPUT);
  pinMode(sensorPin, INPUT);
  pinMode(switchApin, INPUT);
  pinMode(switchBpin, INPUT);
  pinMode(switchCpin, INPUT);
  pinMode(switchDpin, INPUT);
}

void loop() {
  delayValueH = analogRead(onTimePin);
  delayValueL = analogRead(offTimePin);

  int value = (digitalRead(switchDpin) * 8) + (digitalRead(switchCpin) * 3) + (digitalRead(switchBpin) * 2) + digitalRead(switchApin);
}

edgemoron:
Well, you don't need a 16 bit "int" to hold 4 bits, a "byte" that holds 8 is big enough, I'm not familiar with the Mega's port scheme and pinouts though.
BTW, did you shake those pin numbers out of a bottle? They should be consecutive. :slight_smile:

I spaced the pins out because I was going to use a prototyping shield and I wanted room to solder freely... I'm not bad at it but I was just giving myself some wiggle room.

INTP:
You have no loop(). You never declared/defined countA etc. The 'int switchAvalue' is rubbish, has no ; and errors should have shown up in the IDE if you bothered verifying.

yeah I changed count to switch and forgot to change it in the setup. that's all that was... I may have gotten ahead of myself before asking for help. but those I would have found. the help or guidance that I need is in how to arrange the values and make the count work.. and the loop isn't there yet because I'm trying to figure out how the function count needs to go/operate..

THANK YOU JOHN.

johnwasser:
More like:

const byte relayPin = 2; // digitalWrite to relay coil

const byte sensorPin = 7; // on/off sensor
const byte onTimePin = A0; // pot to determine digitalWrite HIGH
const byte offTimePin = A2; // pot to determin digitalWrite LOW
const byte switchApin = 22; // on/off switch A value of 1 when high
const byte switchBpin = 26; // on/off switch B value of 2 when high
const byte switchCpin = 30; // on/off switch C value of 4 when high
const byte switchDpin = 34; // on/off switch D value of 8 when high

long delayValueH; // delay high value
long delayValueL; // delay low value

void setup() {
  pinMode(relayPin, OUTPUT);
  pinMode(sensorPin, INPUT);
  pinMode(switchApin, INPUT);
  pinMode(switchBpin, INPUT);
  pinMode(switchCpin, INPUT);
  pinMode(switchDpin, INPUT);
}

void loop() {
  delayValueH = analogRead(onTimePin);
  delayValueL = analogRead(offTimePin);

int value = (digitalRead(switchDpin) * 8) + (digitalRead(switchCpin) * 3) + (digitalRead(switchBpin) * 2) + digitalRead(switchApin);
}

shouldn't switchCpin be *4 not *3

Hey, you are a programmer now :wink:

larryd:
Hey, you are a programmer now :wink:

uh,, thanks I think. I know I'm still green

Your goal is still unclear.
You have 4 switches. Do you actually mean switches (a lever that has 2 distinct latched states) or perhaps a button of momentary action?
If you have proper switches, what do you mean abcd-1248? You only need 3 for a range of 8. If you only want 4 discrete values, are you only planning on one switch being on at a time? If 4 discrete values only, you only need 2 switches.
Perhaps you have some idea that binary is neat but don't understand when to or when not to use it.

LandonW:
shouldn't switchCpin be *4 not *3

Of course. I was just testing you. :slight_smile:

A lot of coding is basically telling either the compiler what to do or the arduino running a script what to do

"I have 4 micro switchs."

ok I need to tell the compiler that I have 4 switchs and I don't want them to be changed from the pins I assign to them. The compiler understands English so we can use words like switchB so it knows that that anytime we say switchB it means 26. This also allows us to use really long names that makes reading the code easier with no cost to the memory

#define switchA 22
#define switchB 26
#define switchC 30
#define switchD 34

next I need to set the buttons in set up to be inputs

#define switchA 22
#define switchB 26
#define switchC 30
#define switchD 34

void setup() {
  // put your setup code here, to run once:
  pinMode (switchA, INPUT);
  pinMode (switchB, INPUT);
  pinMode (switchC, INPUT);
  pinMode (switchD, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:

}

now I want to press button A and get a result of 1, b2, c4, d8

my highest number is going to be 8 so I need to tell the compiler that I need a place to store this number. Im also going to tell the compiler that when it starts I want the number to be 0.

research what the max number for a signed byte, int , long is.
also research what signed and unsigned means when they over flow.
then look at what a float is as you will need that at some point.

#define switchA 22
#define switchB 26
#define switchC 30// could have used a const instead of define
#define switchD 34

byte number=0;//global......research what that means

void setup() {
  // put your setup code here, to run once:
  pinMode (switchA, INPUT);
  pinMode (switchB, INPUT);
  pinMode (switchC, INPUT);
  pinMode (switchD, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:

}

The arduino will not look at the inputs unless I tell it to so I have to either tell it to look at certain times or I can have the arduino check the inputs every loop. Its easier to check every loop.
To do this im going to have to make a memory address so the arduino remembers what state the button was in so I can test it later

#define switchA 22
#define switchB 26
#define switchC 30
#define switchD 34

byte number = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode (switchA, INPUT);
  pinMode (switchB, INPUT);
  pinMode (switchC, INPUT);
  pinMode (switchD, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly
  byte inputSwitchA = digitalRead (switchA);
  byte inputSwitchB = digitalRead (switchB);
  byte inputSwitchC = digitalRead (switchC);
  byte inputSwitchD = digitalRead (switchD);
}

Now all I need to do is assign a value to "number" when a switch is pressed

 // put your main code here, to run repeatedly
  byte inputSwitchA = digitalRead (switchA);
  byte inputSwitchB = digitalRead (switchB);
  byte inputSwitchC = digitalRead (switchC);
  byte inputSwitchD = digitalRead (switchD);


  if (inputSwitchA == HIGH) {
    number = 1;
  }
  if (inputSwitchB == HIGH) {
    number = 2;
  }
  if (inputSwitchC == HIGH) {
    number = 4;
  }
  if (inputSwitchD == HIGH) {
    number = 8;
  }
}

now after every change im using alt T to check the code then compiling it to see if ive made a mistake or forgot something.

Ok now what do I want number to equal when none of the switch's have been pushed because 0,1,2,4 or 8 was written to the memory address call "number" and it will stay there until I write over it with a new value.

maybe I could use what ever is in the memory address "number" to run a few lines of code then -1 until it equals 0 then stop running that code until the button is pressed again.

p.s when you find that this code is not working correctly and seems kinda random go to the learning section and look at INPUT_PULLUP then do a little reading.

INTP:
Your goal is still unclear.
You have 4 switches. Do you actually mean switches (a lever that has 2 distinct latched states) or perhaps a button of momentary action?
If you have proper switches, what do you mean abcd-1248? You only need 3 for a range of 8. If you only want 4 discrete values, are you only planning on one switch being on at a time? If 4 discrete values only, you only need 2 switches.
Perhaps you have some idea that binary is neat but don't understand when to or when not to use it.

I'm using the four on/off switches ( https://www.amazon.com/gp/product/B00DN7BKB0/ref=oh_aui_detailpage_o00_s00?ie=UTF8&psc=1 ) to act as the first four "bits" to count. so depending on the 4 switches and the combination of on/off I could get a count of 0 to 15. then I plan to use that number to control the amount "count" of how many times the relay coil gets energized and not energized....

the POT's are to change how long its energized and how long its not energized

Maybe show info on this 'microswitch block' because no one has any idea what that is. I'm equally envisioning a 4-switch dip switch or a cheap module with 4 buttons and 5 pins.

LandonW:
I'm using the four on/off switches ( https://www.amazon.com/gp/product/B00DN7BKB0/ref=oh_aui_detailpage_o00_s00?ie=UTF8&psc=1 ) to act as the first four "bits" to count. so depending on the 4 switches and the combination of on/off I could get a count of 0 to 15. then I plan to use that number to control the amount "count" of how many times the relay coil gets energized and not energized....

the POT's are to change how long its energized and how long its not energized

sounds like fun.

The biggest problem is going to be slowing the input readings down unless you can press all 4 buttons with in a few milli-seconds and release them all at the same time

That means you are going to have to learn about detecting switch states changes

gpop1:
The biggest problem is going to be slowing the input readings down unless you can press all 4 buttons with in a few milli-seconds and release them all at the same time

Not too hard. Start a timer each time the value changes. If the timer reaches one second, you have a new value to act on.

void loop() {
  static int previousValue = 0;
  static unsigned long lastValueChangeTime;
 
  delayValueH = analogRead(onTimePin);
  delayValueL = analogRead(offTimePin);
  int value = (digitalRead(switchDpin) * 8) +
              (digitalRead(switchCpin) * 4) +
              (digitalRead(switchBpin) * 2) +
              digitalRead(switchApin);
  if (value != previousValue) {
    lastValueChangeTime = millis();  // Start the timer
    previousValue = value;
  } else {
    // Value has not changed
    if (lastValueChangeTime  != 0 && millis() - lastValueChangeTime >= 1000) {
      // New has been steady for one second or more
      lastValueChangeTime = 0;  // Timer has ended
      //
      //  Act on new value.
      //
    }
  }
}

Dual Inline Package (DIP) switches.