using flags to call up behaviour

Hi i’m fairly new to arduino so this might be an obvious question.

I am trying to program a set of behaviours using an object usion just one motor and one led. I want to program the behaviours then be able to call those beahaviours for a given situation…i.e. person at certain proximity.

The code i have so far is as follows:

int motPin = 9; // choose the pin for the MOTOR
int inLdr = 0; // choose input pin for Ldr
int LDR = 0; // variable for reading the pin status
int ledPin = 10; // select pin for led
int Direction = 1; //pulse direction
int brightness = 0; // led brightness
int sleep = 1; //behaviour state

//DECALRE INPUTS AND OUTPUTS
void setup() {
beginSerial(9600);
pinMode(motPin, OUTPUT); // declare motor as output
pinMode(inLdr, INPUT); // declare Ldr as input
pinMode(ledPin, OUTPUT); //
}

void loop(){

LDR = analogRead(inLdr); // read input value Ldr

printString(“A”); // Example identifier for the sensor
printInteger(LDR); // print sensor reading
serialWrite(10);
serialWrite(13);

printString(“B”); // Example identifier for the sensor
printInteger(sleep); // print sensor reading
serialWrite(10);
serialWrite(13);
delay(20);

//BEHAVIOURs

if (LDR > 300) {
sleep = 0;
}
if (LDR <300) {
sleep = 1;
}
if (brightness >= 254) {
Direction = 0;
}
if (brightness == 0) {
Direction = 1;
}

//sleeping()

if (sleep == 0) {
if (Direction == 1) {
brightness = brightness+ 1 ;
}else{
brightness = brightness- 0.8 ;
}
analogWrite(ledPin, brightness);
delay (8);
}

if (sleep == 1) {
if (Direction == 1) {
brightness = brightness+ 1 ;
}else{
brightness = brightness- 0.8 ;
}
analogWrite(ledPin, brightness);
delay (8);
}

}

The problems are:

1: the pulse is not working. The same code works by itself but not in the code i’m using here…any ideas?

2: how do i use the ‘sleep’ flag to call up a behaviour is this on the right track?

any help greatly appreciated.
thanks

Tobyu

Looks like you're on the right track, but you're passing the wrong parameters to analogWrite(). The second number should be between 0 (always off) and 255 (always on), not 0 and 1. In addition, you want to avoid using floating point (decimal) numbers like 0.8 as they're not really supported by the Arduino board.

HI,

Thnaks for the message.

I am trying to send the brightness value as analogWrite. This should be 0-255, from the calculation above to create the pulse. I'm not sure what you mean by the second number. Do you mean the second instruction to analogWrite, i.e. if (sleep == 1)?

Thanks again

T

I was talking about "brightness", but I think I was misreading your code. Try changing the "0.8"s to "1" and see if it works. Using floating point numbers like 0.8 can cause your program to become too big for the ATmega8's program space, making your program not work (even though it appears to upload correctly).

The next version of Arduino will give an error if your program is too big.