-2

/* 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

Gerhardh
  • 11,688
  • 4
  • 17
  • 39
  • C or C++? I am actually not sure if I removed the right tag, but it isnt both – 463035818_is_not_an_ai Apr 28 '21 at 08:50
  • 3
    So what is the problem? Does your code not compile? Are there error messages somewhere? Does the code run? Does it run but does it not do what you expect? What do you expect the program to do? Please take the [tour] and read this: [ask]. – Jabberwocky Apr 28 '21 at 08:54
  • 1
    what is the error? What is the observed behavior vs the expected behavior? – bolov Apr 28 '21 at 08:54
  • @largest_prime_is_463035818 The code is in C, apologies. – Alex Berbenitskiy Apr 28 '21 at 09:19
  • @Jabberwocky 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. – Alex Berbenitskiy Apr 28 '21 at 09:25
  • First you set IO_I_RAINSENSOR as input and later you change it to output. Why is that? Also, could you provide the diagram of how everything is connected? – Samogitian95 Apr 28 '21 at 09:46
  • @Samogitian95 I have added a diagram in now. I was told to have an output for the rain sensor by my teacher, I was also unsure why I need it and I didn't have an opportunity to ask. – Alex Berbenitskiy Apr 28 '21 at 09:56

1 Answers1

0

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.

Samogitian95
  • 121
  • 4
  • Thank you, I have made the changes but unfortunately the light still does not react to any events. – Alex Berbenitskiy Apr 28 '21 at 10:38
  • @AlexBerbenitskiy make sure your LED is working and polarity is correct. After you did all that, try turning LED on in setup, just to see that it works. – Samogitian95 Apr 28 '21 at 10:45
  • The LED does work as it is on for a split second when I launch the application. However after that it does not respond to anything. – Alex Berbenitskiy Apr 28 '21 at 12:13
  • @AlexBerbenitskiy try using digitalWrite() instead analogWrite() just for testing purposes. If you must use analogWrite(), make sure the pin suports it: https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/ – Samogitian95 Apr 29 '21 at 06:10