Im trying to make a for loop work but it just keeps printing the #6 into the serial moniter

// C++ code
//
int i = 0;
int sensor = 0;
void setup()
{
Serial.begin(9600);
pinMode(A4,INPUT);
for (int i = 0; i < 10; ++i )
{
sensor = analogRead(A4);
Serial.println(sensor);
delay(100);
}
}
void loop()
{
}

  • What is connected to A4 ?

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

a photo resister. thats also whats printing to the serial moniter

In your first post, some days ago, you were told multiple times to read How to get the best out of this Forum and post your code into code tags, as it's instructed there.

Here you are again without the code tags. People may start to think that you're a bad listener...

The code below does the same of yours, but it's in code tags and with indentation. See how it looks better to read:

// C++ code
void setup() {
Serial.begin(9600);
pinMode(A4,INPUT);
  for (int i = 0; i < 10; ++i ) {
    int sensor = analogRead(A4);
    Serial.println(sensor);
    delay(500);
  }
}
void loop(){}

I increased the delay in order for you to see the difference in the values while you give your photo resistor some different light levels (try your cell phone flashlight).

If it keeps giving you just 6, you probably have wrong wiring.

How is it wired to the Arduino and what other components have you used in the project, if any ?

  • You need to make a voltage divider and monitor the voltage drop across the GND connection element.

@henrytroyal

Here's something that will run forever instead of you need to reset it alla time:

void setup() {
  Serial.begin(9600);
  pinMode(A4, INPUT);   // harmless, unnecessary 
}

void loop() {
  int sensor = analogRead(A4);

  Serial.println(sensor);
  delay(500);
}

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.