Go Down

Topic: Infrared Photoelectronic sensor (Read 456 times) previous topic - next topic

ekwon123

I'm working on a car right now for a competition. I am using the photoelectronic sensor to measure the number of rotations it will go, and then cut the power to brake. This is a sensor with the encoder disk. I'm having trouble with the programming, and it will not work. Any help is appreciated! Also, there may be mistakes, first time programming with arduino. This is the sensor:
https://www.amazon.com/gp/product/B00EERJDY4/ref=oh_aui_detailpage_o06_s01?ie=UTF8&psc=1
Code: [Select]
#include <TimerOne.h>
unsigned int counter=0;
 
int in1 = 8;  // L298N inC
int in2 = 9;  // L298N inD
int enA = 10; // Needs to be a PWM pin to be able to control motor speed
int rotation = ("rotation", &rotation); //Setting variable for rotation

void docount()  // counts from the speed sensor
{
  counter++;  // increase +1 the counter value
}
 
void timerIsr()
{
  //not important, just need it for compiling
}
 
void setup()
{
  analogWrite(enA, 52);  // set speed of motor (0-255)
  digitalWrite(in1, LOW); // motor direction
  digitalWrite(in2, HIGH); // same as above
  attachInterrupt(0, docount, RISING);  // increase counter when speed sensor pin goes High
  interrupts();
  {
    void loop(); // to continuously count the rotations
  }
  while(rotation < 10)
  {
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    if(rotation >= 10)
    {
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    break;
    }
  }
}
 
void loop()
{
  rotation = round((counter/20)*100)/100;  // divide by number of holes in wheel. This function makes the rotations come out as a number in 2 decimal places
}


Isaac96

The problem is that you are running loop() in setup(). The loop continuously runs after setup finishes. Move everything after the attachInterrupt line to loop(). One thing-what does interrupts() do?
Do not PM me for help. I will delete immediately.
CONNECT THE GROUNDS!

After Tuesday, even the calendar goes W T F

PaulS

Quote
The problem is that you are running loop() in setup().
No. There is a function prototype for a function called loop, located in the setup() function. But, loop() is not being called from setup().

Code: [Select]
  while(rotation < 10)
  {
    digitalWrite(in1, LOW);
    digitalWrite(in2, HIGH);
    if(rotation >= 10)
    {
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
    break;
    }
  }

While rotation is less than 10, set the state of two pins. If, by some miracle, the value in rotation has changed, and is now greater than 9, break out of the loop.

What miracle is going to make the value in rotation change? How likely is it that that miracle will happen? Trump's chances of reelection are better.
The art of getting good answers lies in asking good questions.

Go Up