Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

regarding IR encoder with arduino Uno

Status
Not open for further replies.

Sundevil007

Newbie level 3
Joined
Apr 24, 2016
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
95
Hi
I am trying to get feedback from my small wheelchair using ir encoders on the both wheels.
I have never worked before with encoders.
Can anyone help me out?

Regards
Hitesh joshi
 

Hi,

What IR decoder?

Read it's datasheet.

Klaus
 

Hi
thanks for replying..
I am using this IR dectector.
https://www.sunfounder.com/learn/Se...dance-sensor-sensor-kit-v2-0-for-arduino.html

I want to use these 2 on each wheel of wheelchair. And using both will send 5v square pulse to Arduino using hardware interrupt pins and the count will start counting.
If there will be any difference between the counts from the two encoders then it will run a software interrupt routine.
Here in the ISR (interrupt software routine) the correction signal will be generated which will increase the speed of left or the right motor, whichever is required.

Regards
Hitesh
 

initially i just need to know the speed. so that if my wheelchair doesn't go straight which inturn means that one of my wheels is turning fast and the other is turning slow. so in that case the Arduino can correct it and the wheelchair goes straight.
 

Do you need to know wheel direction only? Speed only? Both?

An obvious method has alternating light and dark stripes, illuminated by led's. A photosensor detects them. The flash frequency tells you how fast the wheels are turning.

If you wish to sense direction, put two photosensors next to each other, and sense which detects a stripe first.

- - - Updated - - -

I see your post. The device may work, although you'll need alternating obstacles (resembling a fencepost) around the wheel hubs. Some wheelchairs have thin metal spokes, and that may do the job.
 

Actually i bought a wheelchair from a store. Then i observed the signal from joystick in the Oscilloscope. Then using the various patterns for forward, Left, Right and backward, i bypassed the joystick and giving the same pattern using arduino uno.
i have written the code for Forward movement of the wheelchair. Now in this code i want to add code for IR detector for feedback so that my wheelchair goes straight.
I am posting the code for forward direction and also half worked encoder code.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
int outPin = 2;                 // digital pin 8
 
void setup()
{
  pinMode(outPin, OUTPUT);      // sets the digital pin as output
}
 
void loop()
{
  pinMode(outPin, OUTPUT);
 //1 
  digitalWrite(outPin, 0);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 0);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 1);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 0);   
  delayMicroseconds(21); 
//2
  digitalWrite(outPin, 1);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 0);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 0);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 1);   
  delayMicroseconds(21); 
//3
  digitalWrite(outPin, 0);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 1);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 1);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 1);   
  delayMicroseconds(21); 
  //4
  digitalWrite(outPin, 0);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 1);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 0);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 0);   
  delayMicroseconds(21); 
//5
  digitalWrite(outPin, 0);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 0);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 0);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 0);   
  delayMicroseconds(21); 
//6
  digitalWrite(outPin, 0);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 1);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 1);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 1);   
  delayMicroseconds(21); 
//7
  digitalWrite(outPin, 0);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 0);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 0);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 0);   
  delayMicroseconds(21); 
//8
  digitalWrite(outPin, 0);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 0);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 0);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 0);   
  delayMicroseconds(21); 
//9
  digitalWrite(outPin, 0);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 0);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 1);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 1);   
  delayMicroseconds(21); 
//10
  digitalWrite(outPin, 0);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 1);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 0);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 1);   
  delayMicroseconds(21); 
//11
  digitalWrite(outPin, 1);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 1);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 0);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 1);   
  delayMicroseconds(21); 
//12
  digitalWrite(outPin, 0);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 1);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 1);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 1);   
  delayMicroseconds(21); 
//13
  digitalWrite(outPin, 0);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 0);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 0);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 1);   
  delayMicroseconds(21); 
//14
  digitalWrite(outPin, 1);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 1);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 0);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 1);   
  delayMicroseconds(21); 
//15
  digitalWrite(outPin, 1);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 1);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 1);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 1);   
  delayMicroseconds(21); 
//16
  digitalWrite(outPin, 0);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 1);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 1);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 0);   
  delayMicroseconds(21); 
//17
  digitalWrite(outPin, 1);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 1);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 1);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 1);   
  delayMicroseconds(21); 
//18
  digitalWrite(outPin, 0);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 0);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 1);   
  delayMicroseconds(21);     
  digitalWrite(outPin, 1);   
  delayMicroseconds(21); 
 
   
  pinMode(outPin, INPUT_PULLUP);
  //delayMicroseconds(640);  
 
  delayMicroseconds(8000);  
}



This code works fine and the wheelchair moves forward.
But i am still fiddling with the other code. I am posting that too.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const int enc_l = 2;     // encoder pin 1
const int enc_r = 3;     // encoder pin 2
const int ledPin =  13;      // the number of the LED pin
const int stop_pin = 3;      // th enumber of pin which will be used to give stop command.
const int dir_1 = 4;         // direction 1 pin
const int dir_2 = 5;         // direction 2 pin
int timer1_counter;
int counter_encoder_left=0;
int counter_encoder_right=0;
int vehicle_state=0;  // 0: going straight;  1: taking any turn
int difference=0;
 
// variables will change:
volatile int counter_encoder = 0;         
volatile int buttonState = 0;   // variable for reading the pushbutton status
void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  
  //initialize encoder pins as inputs
  pinMode(enc_l, INPUT);
  pinMode(enc_r, INPUT);
  
  // Attach an interrupt to the ISR vector
  attachInterrupt(0, pin_enc_l, RISING);
  attachInterrupt(1, pin_enc_r, RISING);
 
  cli();         // disable global interrupts
  TCCR1A = 0;    // set entire TCCR1A register to 0
  TCCR1B = 0;    // set entire TCCR1B register to 0 
                 // (as we do not know the initial  values) 
  
  // enable Timer1 overflow interrupt:
  TIMSK1 |= (1 << TOIE1); //Atmega8 has no TIMSK1 but a TIMSK register
  
  // Set CS11 bit so timer runs at clock speed: (some prescaling)
  TCCR1B |= (1 << CS11); // Sets bit CS11 in TCCR1B
  timer1_counter = 30000;   // preload timer 65536-16MHz/256/2Hz
  TCNT1 = timer1_counter;   // preload timer
  // value of around 65536-415 will give 26us delay. for 5us, this will change.
  // enable global interrupts:
  sei();
 
  Serial.begin(9600);
  
  pinMode(stop_pin, OUTPUT);
  pinMode(dir_1, OUTPUT);
  pinMode(dir_2, OUTPUT);
 
  digitalWrite(stop_pin, 1);
  digitalWrite(dir_1, 0);
  digitalWrite(dir_2, 0);
}
 
void loop() 
{
    
}
 
void pin_enc_l()
{
  counter_encoder_left = counter_encoder_left+1 ;
}
 
void pin_enc_r()
{
  counter_encoder_right = counter_encoder_right+1 ;
}
 
 
ISR(TIMER1_OVF_vect)
{
  if (vehicle_state==0)
  {
    if (counter_encoder_right>counter_encoder_left)
    {
      difference=counter_encoder_right-counter_encoder_left;
      //do slight right operation here
      counter_encoder_right=0;
      counter_encoder_left=0;
      Serial.println("going right");
    }
    else if (counter_encoder_right<counter_encoder_left)
    {
      difference=counter_encoder_left-counter_encoder_right;
      //do slight right operation here
      counter_encoder_right=0;
      counter_encoder_left=0;
      Serial.println("going left");
    }
    else 
    {
      Serial.println("going straight");
    }
  }
}



If you can help me with this..
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top