hello everyone, in this project i'm using 5 different soil sensors and i'm using two different arrays. one for sensors pins and the other for sensor level.The output of the program is random number of 0 and 1.
need help.
const int sensors[5]={0,1,2,3,4};
int sensorvalue[5];
int i,j;
void setup() {
Serial.begin (9600);
for(i=0;i<5;i++){
pinMode(sensors[i],INPUT);
}
}
void loop() {
for (i=0;i<5;i++,j++){
sensorvalue[j]=digitalRead(sensors[i]);
Serial.println(sensorvalue[j]);
delay(500);
}
}
Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup (the italics in your code, for example), leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn't show the posting toolbar then you can just manually add the code tags:
[code]
[color=blue]// your code is here[/color]
[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.
Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you're using the Arduino Web Editor you will not have access to this useful tool but it's still unacceptable to post poorly formatted code. I recommend you to use the standard IDE instead.
You have to use the twoo arrays in the same way, or better you have to call every element like arrayname [elementnumber] one is used well, but not the other
Hello everyone, in this project i'm using 5 different soil sensors and i'm using two different arrays. one for sensors pins and the other for sensor level.The output of the program is random number of 0 and 1.
need help.
const int sensors[]={0,1,2,3,4};
int sensorvalue[5];
int i,j;
void setup() {
Serial.begin (9600);
for(i=0;i<5;i++){
pinMode(sensors[i],INPUT);
}
}
void loop() {
for (i=0;i<5;i++){
sensorvalue[i]=digitalRead(sensors[i]);
Serial.println(sensorvalue[i]);
delay(500);
}
}
/*
The code can't compile, I se a i insteed a j.
*/
And if you have 5 sensors you have to use numbers from 0 to 5, not to 4.
How can you know that the output isn't correct? Test property the corrent/tension, decause the digitalRead is the right function (if the sensors are digital and not analogical)
Your original example used analogRead. analogRead will output a number from 0 to 1023. Your current code appears to use digitalRead. digitalRead outputs a 0 or a 1.
I'd suggest you read up here on the difference between analog and digital.