I know that there is a small error in the code or circuit but I cannot find it

104 views Asked by At

/* This program controls the retractable spoiler. */

int brightness = 0;
int rainSensor;
int speedSensor;
int currentState;


#define INITIAL_STATE  0
#define ST_SPOILER_DOWN   1
#define ST_SPOILER_UP     2
#define IO_I_RAINSENSOR     3
#define IO_PWM_SPEEDSENSOR  0
#define IO_PWM_LIGHT 7 

#define YES  0
#define NO  1


void setup(){
  currentState = INITIAL_STATE;
  pinMode(IO_I_RAINSENSOR, INPUT);
  pinMode(IO_PWM_SPEEDSENSOR, INPUT);
  pinMode(IO_I_RAINSENSOR, OUTPUT);
  pinMode(IO_PWM_LIGHT, OUTPUT);
  Serial.begin(9600);
  currentState = ST_SPOILER_DOWN;
  brightness = 0;
}

void loop()
{

/* In this state, the spoiler is retracted hence the circuit light is off. The code under dictates that if either the speed sensor or rain
sensor detects their event, the circuit switches to the next state. */

if(currentState == ST_SPOILER_DOWN){
  rainSensor = digitalRead(IO_I_RAINSENSOR);
  speedSensor = analogRead(IO_PWM_SPEEDSENSOR);
  if ((speedSensor > 100)||(rainSensor == YES)){
  brightness = 255;  
  analogWrite(IO_PWM_LIGHT, brightness);
  currentState = ST_SPOILER_UP;
  Serial.println("- Spoiler Down event detected. \n" );
  }
}

/* In this state, the spoiler is extended. The circuit light is on to represent this. The code under dictates that if either the speed sensor or rain sensor detects their event, the circuit switches to the next state. */

if(currentState == ST_SPOILER_UP){
  rainSensor = digitalRead(IO_I_RAINSENSOR);
  speedSensor = analogRead(IO_PWM_SPEEDSENSOR);
  if ((speedSensor <95) && (rainSensor == NO)){
  brightness = 0;
  analogWrite(IO_PWM_LIGHT, brightness);
  currentState = ST_SPOILER_DOWN;
  Serial.println("- Spoiler Up event detected. \n" );
        }
    }
}

This is the circuit the code uses made in tinkerCAD. The switch represents the rain sensor while the potentiometer represents the speed sensor. The spoiler itself is represented by the light.

When I run the circuit, the light stays off no matter what I do with the switch and the potentiometer but the code does not display any errors.

This is the circuit:

circuit

This is a general diagram of how the system should work:

diagram

1

There are 1 answers

4
Samogitian95 On

First I suggest removing pinMode(IO_I_RAINSENSOR, OUTPUT);

Then change #define IO_PWM_SPEEDSENSOR 0 to #define IO_PWM_SPEEDSENSOR A0

I don't work much with Arduino, but if I remember correctly Analog inputs are defined with 'A' in front of a pin number.