I have an arduino tracker sensor using the code;
///Arduino Sample Code
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println(digitalRead(2)); // print the data from the sensor
delay(100);
}
Edit: I typed out more information that for some reason didn’t show up.... anyways.
I want the code to count each time the output changes between 1s and 0s. It would Be nice to count the number of changes and call it variable “x” that way I can’t plug it into An equation. I’m using the sensor to count the number of times a cylinder rotates at a certain number of degrees (black tape stripes). If anyone could please help!
Need help writing state detection addition code is a bit vauge
can you give more details of the project specification?
I typed out more information that for some reason didn't show up
You should never type in code it should always be copied and pasted from the Arduino. Use the copy for forum option so the code tags are included.
void loop() {
static bool old =1;
static unsigned int counter = 0;
bool new = digitalRead(pin);
if (new != old) {
counter++;
Serial.println(counter);
old = new;
}
}
Depending on your hardware, you might need some debouncing or hysteresis as well.
Pieter
Hello, I followed the wiring diagram and code supplied in this link (for reference).
The code is as follows,
///Arduino Sample Code
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println(digitalRead(2)); // print the data from the sensor
delay(100);
}
Im trying to figure out how to adjust this code so that I can count the number of state changes High to Low or low to high, and count the number of changes. Is there also a way I can use this number of changes and named it a variable such as "x". That way I can plug it into an equation and use it to calculate the angular displacement of a rotating cylinder?
Thank you.
@MelissaMartinez, please do not cross-post. Threads merged.
Hello, I followed the wiring diagram and code supplied in this link (for reference).
The code is as follows,
///Arduino Sample Code
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println(digitalRead(2)); // print the data from the sensor
delay(100);
}
Im trying to figure out how to adjust this code so that I can count the number of state changes High to Low or low to high, and count the number of changes. Is there also a way I can use this number of changes and named it a variable such as "x". That way I can plug it into an equation and use it to calculate the angular displacement of a rotating cylinder?
Thank you.
void loop()
{
Serial.println(digitalRead(2)); // print the data from the sensor
delay(100);
}
Separate the read result into a variable. Then use that variable to print and use it to comparing it to the last time you read the pin and increment the up or down count accordingly.
Finally make the last time you read variable have the same value as the current one, so when the loop function runs again it will contain the old value.
So you need two variables, you can call them anything but lastValue and value is a good start.