I'm trying to let a Grove Light Sensor v1.2 controle a Grove Mini Fan. But for some reason I can't get it working, I'm already trying different stuff for days. (yes, i'm quite the beginner.)
Unfortunately I couldn't find anyone who did something similar.
This is the code that seemed most promising to me, but isn't working.
It would be ideal if I could also change the speed of the fan, but I don't think thats possible.
Please! I really hope someone can help me! Thank you!!
You are not reading a value from your lightpin - you will need to use analogRead.
You are trying to use analogWrite on a pin that likely does not support it - try pin 6 or check which of the pins on your Arduino model are appropriate.
Thank you for your quick response!
I'm using a base shield from seeed studio and from what I read they recommend pin 7, but I tried to use pin 6 either way.
I'm not sure if I put analogRead on the right spot, or do I need to do it instead of analogWrite? It's still not working..
To use analogRead, you need to put the value it returns in a variable. That variable is what you should be checking in your if statement, not lightpin itself.
Thanks again, I think I slowly start to understand it. I did some changes that (at least that's what I thought) should solve the problem, but still my fan won't move. I did manage to get the info from my light sensor! Is there still something I'm doing wrong? Sorry...
int motorPin = 6;
int lightPin = A0;
int analogValue = 0;
int val = 0;
void setup(){
pinMode(motorPin, OUTPUT);
pinMode(lightPin, INPUT);
Serial.begin(9600);
}
void loop() {
if(analogValue < 180){
analogRead(analogValue);
int motorValue = 0;
analogWrite(motorPin, motorValue);
delay(30);
}
if(analogValue > 180){
analogRead(analogValue);
int motorValue = 255;
analogWrite(motorPin, motorValue);
delay(30);
}
val = analogRead(analogValue);
Serial.println(val);
delay(500);
}