state detection help

I am doing a project with sensor
function:
if sensor sense object and sensorvalue >400
press button left and right and motor on
However I got problem that although sensorvalve > 400 & right button and left button is press nothing happened.
And is Serial monitor didnt show 'ON'

int pMR_DIR = 4;
int pMR_PWM = 5;
int pRightButton = 2;
int pLeftButton = 3;

int RightbuttonStatus = 0;
int LeftbuttonStatus = 0;

int MR_Spd = 70;
int ML_Spd = 70;

int Right_IR = A2;

int SensorState = 0;
int LastSensorState = 0;

float Right_IR_Value;

void setup() 
{
  pinMode(pMR_DIR,OUTPUT);
  pinMode(pMR_PWM,OUTPUT); 
  pinMode(pRightButton,INPUT);
  pinMode(pLeftButton,INPUT);
  Serial.begin(9600);

}
void loop()
{
int Right_IR_Value = analogRead(Right_IR);
int RightbuttonStatus = digitalRead(pRightButton);
int LeftbuttonStatus = digitalRead(pLeftButton);
  Serial.print("Right_IR=" );
  Serial.println(Right_IR_Value);
if (Right_IR_Value > 400) //Sensor sense object
 {
 SensorState = 1;
 }
if (Right_IR_Value < 400) //no object
 {
  SensorState = 0 ;
 }
 if (!SensorState == LastSensorState)
 {
  Serial.print(''ON'');
  if (RightbuttonStatus == HIGH && LeftbuttonStatus == HIGH ) //2 button press
    {
    digitalWrite(pMR_DIR,LOW);
    analogWrite(pMR_PWM, MR_Spd*1.3);
    }
 }
 delay(500);
 }

So what is the problem ?

Should you just be assigning a value to sensorstate, instead of using it in the digitalwrite function.

digitalWrite(SensorState, HIGH); .... this appears to be wrong

Should it be SensorState=1 ?

Steve

UKHeliBob:
So what is the problem ?

I got problem that although sensorvalve > 400 & right button and left button is press nothing happened.
And is Serial monitor didnt show 'ON'

This if statement is part of the problem:

if (!SensorState == LastSensorState)

Both those variables are set to zero when initialized and there is nothing in your code that changes them.

Also, use of SensorState to specify a pin in digitalWrite calls means you're manipulating pin zero, which is used by Serial.

C strings take double quotes, not single. This:

  Serial.print('ON');

Should be:

  Serial.print("ON");

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

How have you got your buttons wired, do you have pullup or pull down resistors?

What model Arduino are you using?

Thanks.. Tom.. :slight_smile:

And, is there a pin called SensorState???

digitalWrite(SensorState, HIGH);

Steve

TomGeorge:
Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

How have you got your buttons wired, do you have pullup or pull down resistors?

What model Arduino are you using?

Thanks.. Tom.. :slight_smile:

Using arduino uno and I used normal resistor and all the switch and motor is working perfectly before I doing this program

steve1001:
And, is there a pin called SensorState???

digitalWrite(SensorState, HIGH);

Steve

Nope,and I edit now

digitalWrite(SensorState, 1);

digitalWrite(SensorState, 1);

What do you think that command will do ?
Which pin number is SensorState ?

UKHeliBob:

digitalWrite(SensorState, 1);

What do you think that command will do ?
Which pin number is SensorState ?

I edited this line to
SensorState = 1

Please post your complete program as it is now and describe any problems you are having

Hi,
Please do not update your code in old posts, please post a fresh code each time you edit.
Changing old code in old posts does not help anyone trying to use this thread to help with their code problems.

Thanks.. Tom... :slight_smile: