Hello, I am trying to make a simple Robot that moves based on the reading from 5 IR Sensors. I don't want the code to be large or time consuming so I wanted the readings to be stored in an Array and that when I call the array it prints the value in Serial Monitor.
int Sensor1 = 2;
int Sensor2 = 3;
int Sensor3 = 4;
int Sensor4 = 5;
int Sensor5 = 6;
void setup() {
Serial.begin(38400);
pinMode(Sensor1,INPUT);
pinMode(Sensor2,INPUT);
pinMode(Sensor3,INPUT);
pinMode(Sensor4,INPUT);
pinMode(Sensor5,INPUT);
}
int s1 = digitalRead(Sensor1);
int s2 = digitalRead(Sensor2);
int s3 = digitalRead(Sensor3);
int s4 = digitalRead(Sensor4);
int s5 = digitalRead(Sensor5);
//Reading Array for Simplification
int SensorRead[3] = {s1,s2};
void loop() {
Serial.print("Sensor Reading: ");
Serial.println(SensorRead[1]);
delay(1000);
}
I know that this code prints the value of Sensor 1 if the code is right.But Serial Monitor is not changing the value.Perhaps my approach is wrong or something.So can anyone help?
PS: I was very curious to find out what happens if I put the Serial Baud Rate Speed (SBRS) @ 38400 BRS.Let me know if that was wrong!
Actually it prints the value of s2 because the array index starts at zero but as you never update the values stored in the array in the loop() function it will not change
int SensorRead[3] = {s1,s2};
does not bind the value of s1 and s2 to the array. You need to update the values explicitly
@Idahowalker 1) no other values than 0 and 1.
2)OK I think I should put these readings into the void loop so that they get updated
3) Let me see @Idahowalker.
Your code seems to be written on the assumption that once you have executed
int SensorRead[3] = {s1, s2};
that every time you change the value of s1 or s2 then the corresponding value held in the array will be updated, but this is not the case. In order to update a value held in the array you must explicitly change its value using its position in the array as the array index
An array int q[5] = {0}; makes an array holding 5 values filled with 0's.
q[0]=digitairead(pinSpankie) puts the state of the digitial pin into the array cell 0. If you want, you can write assembler code or use DMA. If the concept of q[0]=digitairead(pinSpankie) is proving difficult then assembler or using DMA will be even more difficult.
You can use a for() loop something like or look it up on the internet on how do to a for()
int q[5] = {0};
for( int i = 0; i<5; i++ )
{
q[i]=digitialRead(i+2); // for pins 2 - 6. i=0+2 = pin 2, i=1+2 pin 3,
}
OK but I wanted to create an array to reduce typing/updating the values over and over again.Sigh! I have to drop this method.I am really a beginner in Arduino. Thanks so much for all your help!
don't doubt that the digitalRead works, but a digital read results in either a 0 or 1, not a 5. i get the above output when i test the code without anything connected to those pins
if you expect a sensor value other that 0/1 wouldn't you need to use analogRead()? what type of sensor is it?