Variable output wrong

Hi Guys

My first sort of scrapped together code.
Im waving a magnet in front of a hall effect sensor, most of it is working.
I have 3 variables,

  • a live value of the sensor (Hall01live) to either 0 or 1 (working when I wave magnet)
  • setting a value of what the sensor was (Hall01was) at the end of a if statement (not working)
  • setting an angle and a variable of the same name, starting at 200, expecting it to change to 199 when I wave a magnet in front of it, and it to stay as 199 until I remove the magnet then it returns to 200
    No idea what I am doing wrong, please help
    int Hall01was = 0;
    int Hall01live = 0;
    int angle = 200;
    // the setup routine runs once when you press reset:
    void setup() {
    // initialize serial communication at 9600 bits per second:
    Serial.begin(9600);

}

void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);

//detecting magnet, setting Hall01 to 1 or 0
if ( sensorValue > 200) Hall01live = 1;
if (sensorValue < 200) Hall01live = 0;
Hall01was=Hall01was;

//angle test
//this tests to see if there is a change, changes the variable angle and then reset the check ('was')
if (Hall01was = 0 && Hall01live = 1){ angle-1 ; Hall01was ==1;}
//this tests to see if there is a change, changes the variable angle and then reset the check ('was')
if (Hall01was == 1 && Hall01live == 0){ angle -1 ; Hall01was ==0;}

//display stuff
Serial.println ("Hall01was");
Serial.println(Hall01was);
Serial.println ("Hall01live");
Serial.println(Hall01live);
Serial.println("angle");
Serial.println(angle);
Serial.println("sensor");
Serial.println(sensorValue);

delay(2000);
}

int Hall01was = 0;
int Hall01live = 0;
int angle = 200;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);

}

void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);

//detecting magnet, setting Hall01 to 1 or 0
if ( sensorValue > 200) Hall01live = 1;
if (sensorValue < 200) Hall01live = 0;

//angle test

if (Hall01was == 0 && Hall01live == 1){ angle -1 ;}
if (Hall01was == 1 && Hall01live == 0){ angle +1 ;}
Serial.println ("Hall01was");
Serial.println(Hall01was);
Serial.println ("Hall01live");
Serial.println('Hall01live');
Serial.println("angle");
Serial.println(angle);
Serial.println("sensor");
Serial.println(sensorValue);

delay(2000); // delay in between reads
}

Looks like you've posted the code twice, and you'll need to use code tags. Highlight to full code and click </> or you can click code tags then paste in the code.

EDIT: Both your codes are different, so I deleted my post and will wait until you post the final code with tags.

What is the significance of "angle"? You cant use a single hall sensor to detect an angle.

You need to provide a LOT more information - like the specific arduino you are using, a link to the sensor and a schematic. As described here.

//this tests to see if there is a change, changes the variable angle and then reset the check ('was')
if (Hall01was = 0 && Hall01live = 1)
{
  angle - 1;  // This does nothing.  The result of the subtraction is not saved.
  Hall01was == 1;  // This does nothing.  The result of the comparison is not saved.
}
//this tests to see if there is a change, changes the variable angle and then reset the check ('was')
if (Hall01was == 1 && Hall01live == 0)
{
  angle - 1;  // This does nothing.  The result of the subtraction is not saved.
  Hall01was == 0;  // This does nothing.  The result of the comparison is not saved.
}

It looks like what you intended was:

//this tests to see if there is a change, changes the variable angle and then reset the check ('was')
if (Hall01was = 0 && Hall01live = 1)
{
  angle -= 1;  // Shortcut for angle = angle - 1;
  Hall01was = 1;
}
//this tests to see if there is a change, changes the variable angle and then reset the check ('was')
if (Hall01was == 1 && Hall01live == 0)
{
  angle -= 1;  // Shortcut for angle = angle - 1;
  Hall01was = 0;
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.