Trying to count how many "High" my sensor outputs( extremely desperate!!)

I have an Omron Sensor photoelectric sensor, or a “U” shaped sensor, that is plugged into a digital pin. the connections are fine. I just want to count how many times it says a “1” vs a “0”. For example if it reads 10101010 it should have counted 4 “1”. I am trying to get an RPM reading, and there are two holes in the disk that helps encode it. So if I have 4 “1” , the number of revolutions that the disk has spun is twice. So if I were to say that I get 2 revolutions in 1 second the RPM should be 120 RPM.

The motor is an HDD motor with a potentiometer to control it’s speed. I know that the potentiometer’s values will change, and my group has already found a way to compare the previous values of the potentiometer to the current value, and adjust accordingly. However, the code below gives me a value of 120 RPM when the disk is not even spinning.

Sorry, for the long request, but I was wondering if there was a more simple way to achieve this. I was hoping that my group and I would come up with a code that has the agreed thought process in the 1st paragraph but the code is able to read the sensor and the potentiometer, but it takes the arduino to long process a disk that is spinning at upto 5500 RPM. The code is below

Motor_Pot_controlled.ino (1.19 KB)

Please post your code in code tag (</> icon) so everyone can read it.

Weedpharma

You may be picking up the 120 Hz flicker from fluorescent lights, try shielding the sensors from ambient light.
That's the way I check my photo tach calibration.

To weedpharma: thanks I have rewritten it here…

//Motor_pot_controlled

#include<Servo.h>
Servo HDD;

int val; //Value for motor speed.
int analogPin = A4; //Potentiometer on analog pin 4
int servoPin = 10; //ESC on pin 10
int sensorPin= 2; //Omron on pin 2
int led=13;
long time=micros();
int rev=0; 
float rpm;
int starting=1;
unsigned long startt;
unsigned long endt;
unsigned long timehalf;
int prevsen;
int preval;
void setup(){

Serial.begin(9600);
HDD.attach(servoPin);
HDD.writeMicroseconds(1000);
delay(300);
}

void loop(){
  
int val = analogRead(analogPin); //Make a analogue reading. This is from 0-700.
val = map(val, 0, 1050, 1060, 1860); //Scale input to use with motor in microseconds.
int sensor = digitalRead(sensorPin);

if(starting==1)
{
endt=1;
startt=micros();
sensor=prevsen;
starting=0;
preval=val;
}

if (sensor==1&& prevsen==0) 
{
endt=micros();
timehalf=endt-startt;
startt=endt;
  }
//Serial.print(val);
//     Serial.print('\t');
//   Serial.print(sensor);
//     Serial.print('\t');
// Serial.print(prevsen);
//   Serial.print('\t');
// Serial.print(timehalf/1000.0);
  Serial.print("\t rpm: ");
   Serial.println(float (1.0/(timehalf*2.0/60/1000000.0)));
 prevsen=sensor;
 if (preval!=val)
HDD.writeMicroseconds(val);
preval=val;
}

moderator: added code tags

By asking you to use code tags, I was referring to the icon at the top of the editor that looked like </>.

This automatically puts the code tags for you to insert you code between. The code box makes it easier for others to copy the code to test it and stops any code being interpreted as formatting commands.

You can edit your post to place     [code]  and [/code]  around the code.

Also try shading as suggested by outsider.

Weedpharma

@aliyahk2013, you really should have read the How to use this forum - please read post at the top of the index page and How to use this forum before posting.

ie As weedpharma says, your code and any error messages should always be placed between code tags. Posting it inline as you have done makes it much harder to read or copy and paste for diagnosis.

It's still not too late to edit your post and do this. You'll make potential helpers much happier. :slight_smile:

//Motor_pot_controlled

#include<Servo.h>
Servo HDD;

int val; //Value for motor speed.
int analogPin = A4; //Potentiometer on analog pin 4
int servoPin = 10; //ESC on pin 10
int sensorPin= 2; //Omron on pin 2
int led=13;
long time=micros();
int rev=0; 
float rpm;
int starting=1;
unsigned long startt;
unsigned long endt;
unsigned long timehalf;
int prevsen;
int preval;
void setup(){

Serial.begin(9600);
HDD.attach(servoPin);
HDD.writeMicroseconds(1000);
delay(300);
}

void loop(){
  
int val = analogRead(analogPin); //Make a analogue reading. This is from 0-700.
val = map(val, 0, 1050, 1060, 1860); //Scale input to use with motor in microseconds.
int sensor = digitalRead(sensorPin);

if(starting==1)
{
endt=1;
startt=micros();
sensor=prevsen;
starting=0;
preval=val;
}

if (sensor==1&& prevsen==0) 
{
endt=micros();
timehalf=endt-startt;
startt=endt;
  }
//Serial.print(val);
//     Serial.print('\t');
//   Serial.print(sensor);
//     Serial.print('\t');
// Serial.print(prevsen);
//   Serial.print('\t');
// Serial.print(timehalf/1000.0);
  Serial.print("\t rpm: ");
   Serial.println(float (1.0/(timehalf*2.0/60/1000000.0)));
 prevsen=sensor;
 if (preval!=val)
HDD.writeMicroseconds(val);
preval=val;
}

I know you guys are trying to help me better use the site. For that I’m thankful, but I am under a very tight deadline. I was assigned this problem, when I still do not know C++. I learned little MATLAB and a little JAVA, but that is it. I am still learning all this, and unfortunately, Mechanical Engineering did not prioritize learning different coding languages. Even though they should have. So Please help! I’m not trying to be rude, but all I need is help understanding what this code is doing. The values change up and down when the disk that is spinning is at a “constant” speed. For example, the speed may be 500 RPM, but the serial monitor says anything from 120 to 986.