Hello!
As a fairly rookie programmer, I have been experiencing some persistent problems with my latest program. I want to make a function that fades a LED in in a few different places in my code, but I can't even get to dim it properly using analogWrite somehow. And I can't seem to figure out what's wrong.
For some reason, analogWrite(255) turns the led on, so far everything normal, but any value below 255 turns the led off, as if it were a digital signal.
I can run the normal fade code to make the led fade without any problems, so I'm guessing it's probably a problem with the rest of my code?
Here's my code. Any help or tips would be most welcome!
/*This code is for the hackable guardian robot project from Thingiverse
*
* Besides the eye led, the head-turn servo and led string in the body,
* it has been edited to support a sound sensor as wel. This allows the guardian robot to react to loud souds
* in a more or less game-accurate way, by waking up, looking around and falling back into a standby modus.
*
*Switches between the original 'permanently on' and responsive mode using a pushbutton.
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int MIN = 0; // Set servo values, control the guardian's head turn angles
int MAX = 180;
int NTRL = 90;
const int minSound = 0; //sound sensitivity of the sound sensor. A higher number will respond at slighter sounds.
const int switchButton = 2; // Mode-switch pin
const int soundSensor = A0; // Sound sensor pin
const int bodyLED = 9; // Body led pin
const int eyeLED = 10; // Eye led pin
// Set servo to pin 8;
boolean switchState = true; // the current state of the mode-switch pin
int reading; // the current reading from the input pin
int previous = 0; // the previous reading from the input pin
long time = 0; // the last time the mode-switch was toggled
long debounce = 200; // the debounce time, increase if the output flickers
unsigned long t; // Head-turn timer for active mode
int lastLookTime = 0; // Time of last head-turn
boolean active = false;
int i;
int fadeIn()
{
analogWrite(bodyLED,255); // <------------------------ HERE'S WHERE MY PROBLEM COMMAND IS DECLARED.
delay (1000); //ANALOGWRITE(BODYLED,<255)SOMEHOW TURNS OFF THE LED INSTEAD OF DIMMING IT
}
//-------------------------------------------------------------------------------------------------------------------------
void setup()
{
Serial.begin(9600);
myservo.attach(6);
pinMode(soundSensor,INPUT);
pinMode(switchButton, INPUT);
pinMode(bodyLED, OUTPUT);
pinMode(eyeLED, OUTPUT);
}
//------------------------------------------------------------------------------------------------------------------------
void loop()
{
int sensorValue = analogRead(soundSensor); // Read the sound sensor
reading = digitalRead(switchButton); //Operates the switch between the fully active and awoken/decayed state
if (reading == true && previous == false && // Toggles between switchState == TRUE (fully active mode) and switchState == FALSE (decayed/awoken mode) every time the button is pushed
millis() - time > debounce)
{
if (switchState == true)
switchState = false;
else
switchState = true;
time = millis();
}
previous = reading;
if (switchState == true) //FULLY ACTIVE (when switch button pushed once)
{
if (active == false)
{
fadeIn(); // <----------------- AND MY PROBLEM FUNCTION CALLLED HERE...
digitalWrite (eyeLED,HIGH); // Activates body- and eye leds
active = true;
}
if(random(10) == 0) //Activates and blinks eye led
{
digitalWrite(eyeLED, LOW);
delay(random(50, 250));
digitalWrite(eyeLED, HIGH);
}
t = millis();
if((t - lastLookTime) > 500) // If more than 1/2 second has passed since last head turn...
{
if(random(10) == 0)
{ // There's a 1-in-10 chance...
myservo.write(random(MIN, MAX)); // ...of randomly moving the head in a new direction:
lastLookTime = t; // Save the head-turn time for future reference
}
}
}
else if (sensorValue < minSound && switchState == false)
{ //AWOKEN STATE (temporary active after noise input)
active = true;
fadeIn(); //<--------------------------- AND HERE.
digitalWrite(eyeLED, HIGH);
for (int i =0; i<(random(4,10));i++) //Determines the 'time' being awake by cycling through a blink
{
digitalWrite(eyeLED, LOW); //Makes the eye led blink
delay (random(50,250));
digitalWrite(eyeLED, HIGH);
delay(random(300, 1000));
if(random(0,2) == 0 ) // And every other time it goes trough the loop, here's a 50% chance...
{
myservo.write(random(MIN, MAX)); // ...of randomly moving the head in a new direction
}
}
}
else //DECAYED STATE (off)
{
if (active == true) // if the guardian robot was still active..
{ // it will turn the servo into neutral mode and turn off leds one by one. If not, it will just do nothing until further input
myservo.write (NTRL);
delay (1000);
digitalWrite (eyeLED, LOW);
delay (1000);
analogWrite (bodyLED,1000);
active = false;
}
else
{
}
}
Serial.print(" status = "); // Print to serial monitor for debugging
Serial.print(switchState);
Serial.print("\t Digital = ");
Serial.println(sensorValue);
delay(100);
}