Photoresistor Project with an If Statement

Project: send 5v signal through a Photo-resistor, then through a 330R Resistor (which is also grounded) Then connect to analog pin A1. If the electric current into A1 is less than 3Volts turn the power to PWM pin 12 on so a second circuit is made. The code is taken from the provided pre=made code in the Arduino IDE, and tweaked to fit my needs and previous process of troubleshooting.

Code:

int sensorPin = A1; // select the input pin for the potentiometer
int pin12 = 12; // select the pin for the LED
int sensorValue = A1; // variable to store the value coming from the sensor

void setup() {
Serial.begin(9600); //Start serial communication with computer over USB
// declare the powerPin as an OUTPUT:
pinMode(pin12, OUTPUT);
pinMode(sensorValue, INPUT);
int sensorValRead = analogRead(A1); //Reads resistance value from A1
float voltage = sensorValRead * (5.0 / 1023.0);// calculates voltage level input by A1
Serial.println(voltage);
}
void loop() {
int sensorValRead = analogRead(A1);//Reads resistance value from A1
float voltage = sensorValRead * (5.0 / 1023.0);// calculates voltage level input by A1
Serial.println(voltage);

if(voltage >= 3.0);
{
// turn the ledPin on
digitalWrite(pin12, HIGH);
// stop the program for milliseconds:
delay(voltage);
}
digitalWrite(pin12, HIGH);

}

What is the question?

Talle50:
Project: send 5v signal through a Photo-resistor, then through a 330R Resistor (which is also grounded) Then connect to analog pin A1. If the electric current into A1 is less than 3Volts turn the power to PWM pin 12 on so a second circuit is made. The code is taken from the provided pre=made code in the Arduino IDE, and tweaked to fit my needs and previous process of troubleshooting.

int sensorPin = A1;    // select the input pin for the potentiometer

int pin12 = 12; // select the pin for the LED
int sensorValue = A1;  // variable to store the value coming from the sensor

void setup() {
  Serial.begin(9600); //Start serial communication with computer over USB
  // declare the powerPin as an OUTPUT:
  pinMode(pin12, OUTPUT);
  pinMode(sensorValue, INPUT);
  int sensorValRead = analogRead(A1); //Reads resistance value from A1
  float voltage = sensorValRead * (5.0 / 1023.0);// calculates voltage level input by A1
  Serial.println(voltage);
}
  void loop() {
  int sensorValRead = analogRead(A1);//Reads resistance value from A1
  float voltage = sensorValRead * (5.0 / 1023.0);// calculates voltage level input by A1
  Serial.println(voltage);
 
  if(voltage >= 3.0);
  { 
  // turn the ledPin on
  digitalWrite(pin12, HIGH);
  // stop the program for milliseconds:
  delay(voltage);                 
  }
  digitalWrite(pin12, HIGH);
 
 
}

You wanted pin12 to have an PWM signal yet you use digitalWrite to it. This will just be a solid, unmodulated 5v. You also have the command in twice. (once conditionaly and again without any condition)

So you should have analogWrite(pin12,someValueHere) within your conditional statment and then you should delete the unconditional digitalWrite that follows it.

nilton: my original issue was that the analog A0 is not receiving an input. I've made the changes KenF suggested, and the A0 port is still not showing it getting a signal.

Post a sketch (or photo) of your wiring - it almost sounds as though you have the A1 signal wired to ground.

if(voltage >= 3.0);

If the voltage is greater than 3.0, do nothing ( ; ). Otherwise, do nothing. Well, OK.

if(voltage >= 3.0);
  { 
  // turn the ledPin on
  digitalWrite(pin12, HIGH);
  // stop the program for <sensorValue> milliseconds:
  delay(voltage);                 
  }
  digitalWrite(pin12, HIGH);

Then, turn the pin on, wait a while, and turn the pin on. I'm sensing something wrong here. Can't quite put my finger on it, though.

Hi,

float voltage = sensorValRead * (5.0 / 1023.0);// calculates voltage level input by A1
Serial.println(voltage);

try this added first

Serial.print(sensorValRead);
Serial.print(" ");
float voltage = sensorValRead * (5.0 / 1023.0);// calculates voltage level input by A1
Serial.println(voltage);

It will probably showa value for sensorValRead,but 0 for voltage.
I think you have to declare sensorValRead as a float for the equation to work correctly.
So edit this line to add float;

float voltage = float sensorValRead * (5.0 / 1023.0);// calculates voltage level input by A1
Serial.println(voltage);

Hope it, helps, mind getting into gear, first coffee of the day.
Tom......... :slight_smile:

I think you have to declare sensorValRead as a float for the equation to work correctly.

Nope. There needs to be at least one float in the equation. There are already two.

Update: I have tried the suggestions up until the one suggested by TomGeorge, been very busy with academics but will try each suggested solution once more and give another update after that. My apologies for the mess of code originally posted.

How do I make a post where there is the specific section for code rather than posting it as normal text?

Select your code, then press this

Or just type the code tags before and after your code like this:

[code]
// your code
[code]