I know this seems like a simple solution, but have you tried switching the definition of int x; to int x = 0;
this is only important for the first iteration of the loop as when the function Serial.println calls x as an argument, it would have no assigned value (at least initially) otherwise the code looks good!
int pressure = A0;
int ledsolenoid = 5;
int set1 = 8 ;
int set2 = 9 ;
int set3 = 10 ;
int set4 = 11 ;
int x = 0;
void setup() {
pinMode(pressure, INPUT);
pinMode(set1, INPUT);
pinMode(set2, INPUT);
pinMode(set3, INPUT);
pinMode(set4, INPUT);
pinMode(ledsolenoid, OUTPUT);
Serial.begin(9600);
}
void loop() {
Serial.println(x);
delay(500);
Serial.println(analogRead(pressure));
delay(500);
{
if (digitalRead(8)== HIGH){
x = 200;}
if (digitalRead(set2)== HIGH){
x = 300;}
if (digitalRead(set3)== HIGH){
x = 400;}
if (digitalRead(set4)== HIGH){
x = 100;}
else {
x = 0;}
if (analogRead(pressure) >= x){
digitalWrite(ledsolenoid, HIGH);
delay(100);
digitalWrite(ledsolenoid, LOW);
delay(100);
digitalWrite(ledsolenoid, HIGH);
delay(100);
digitalWrite(ledsolenoid, LOW);
delay(100);
digitalWrite(ledsolenoid, HIGH);
delay(100);
digitalWrite(ledsolenoid, LOW);
delay(100);
digitalWrite(ledsolenoid, HIGH);
delay(100);
digitalWrite(ledsolenoid, LOW);
delay(100);
digitalWrite(ledsolenoid, HIGH);
delay(100);
digitalWrite(ledsolenoid, LOW);
delay(100);
digitalWrite(ledsolenoid, HIGH);
delay(100);
digitalWrite(ledsolenoid, LOW);
delay(100);
}
else {
digitalWrite(ledsolenoid, LOW);
}
}
hey mate,
just tried that, but it doesn’t seem to change what the x value is??? it stays at 0 and when i change the pin it doesn’t change the x value
Doing so and trying to compile I get the following error message:
C:\Users\Support\AppData\Local\Temp\arduino_modified_sketch_754425\BareMinimum.ino: In function 'void loop()':
BareMinimum:72:1: error: expected '}' at end of input
}
^
exit status 1
expected '}' at end of input
BareMinimum:72:1 means the error is in the file BareMinimum, line 72, character 1. (My file was named BareMinimum, this’ll be whatever yours is named). Noting this is the end of the file, and it’s looking for a close brace, there’s a good chance you haven’t closed off all your braces, or you’ve got an extra open one somewhere.
delay(500);
{
if (digitalRead(8)== HIGH){
x = 200;}
if (digitalRead(set2)== HIGH){
x = 300;}
if (digitalRead(set3)== HIGH){
x = 400;}
if (digitalRead(set4)== HIGH){
x = 100;}
else {
x = 0;}
You have an additional { after delay(500);.
And after your series of if statements, you’ll only ever get x=100 (if pin11 is high), and x=0 in all other cases.
You should re-write this to:
Serial.println(analogRead(pressure));
delay(500);
if (digitalRead(8)== HIGH){
x = 200;}
else if (digitalRead(set2)== HIGH){
x = 300;}
else if (digitalRead(set3)== HIGH){
x = 400;}
else if (digitalRead(set4)== HIGH){
x = 100;}
else {
x = 0;}
sweet
I had to change to your edited code ( thankyou for that ) and am now getting a reading of 200 and 0 depending on the location of the pin.
I have a number of pins connected to my rotary switch all with a empty slot separating them, whenever i am on an empty pin the computer sets x to 0 but whenever on on a connected slot connected to the arduino im getting 200
this is the updated code
int pressure = A0;
int ledsolenoid = 5;
int x = 0;
void setup() {
pinMode(pressure, INPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);
pinMode(10, INPUT);
pinMode(11, INPUT);
pinMode(ledsolenoid, OUTPUT);
Serial.begin(9600);
}
void loop() {
Serial.println(x);
delay(500);
Serial.println(analogRead(pressure));
delay(500);
if (digitalRead(8)== HIGH){
x = 200;}
else if (digitalRead(9)== HIGH){
x = 300;}
else if (digitalRead(10)== HIGH){
x = 400;}
else if (digitalRead(11)== HIGH){
x = 100;}
else {
x = 0;}
if (analogRead(pressure) >= x){
digitalWrite(ledsolenoid, HIGH);
delay(100);
digitalWrite(ledsolenoid, LOW);
delay(100);
digitalWrite(ledsolenoid, HIGH);
delay(100);
digitalWrite(ledsolenoid, LOW);
delay(100);
digitalWrite(ledsolenoid, HIGH);
delay(100);
digitalWrite(ledsolenoid, LOW);
delay(100);
digitalWrite(ledsolenoid, HIGH);
delay(100);
digitalWrite(ledsolenoid, LOW);
delay(100);
digitalWrite(ledsolenoid, HIGH);
delay(100);
digitalWrite(ledsolenoid, LOW);
delay(100);
digitalWrite(ledsolenoid, HIGH);
delay(100);
digitalWrite(ledsolenoid, LOW);
delay(100);
}
else {digitalWrite(ledsolenoid, LOW);}
}
Could you please send us through a picture of your wiring, it’s important to note that in the logic which you’re currently using that the value will be set to the first HIGH reading that the microcontroller reads.