how to control servo motor using embedded c using simple code? - avr

i'm working on a school project "automatic railway system"
my project suppose to close the gate when the train coming to the station with a buzzer on with 90 sec count down display on 7-seg. and a led flashing.
after the train leaving the station, the gate opens and the buzzer off and the led off .
i tried to use a dc motor to open and close the gate but it didn't give me the accurate angle that i need to i try to use a servo motor .
so i need it to open the gate at position zero and close it at position 90 .
all the code i found on the internet they using PWM and timers which i didn't take it in my course , so can anyone help me to do this with simple code ,please ?
i'm using Atmega32 running at 16000000 HZ

Its depend on your analog servo (which is controlled by PWM) frequency specification. After you learn about the servo specification, you can set your PWM using build-in features on cvavr compiler, or you can do some research about PWM registers.
Here is some example of PWM setup
//using OC0 (B.3)
DDRB.3 = 1; //set B.3 as output
TCCR0=0b0111 0001;
TCNT0=0; //set to Phase Correct PWM mode, no prescaler, and inverted output
//to assign a value to your PWM
OCR0 = 127 //50% duty cycle since it was 8 bit

Related

PIC24F issues reading analog AC microphone values

I am using a PIC24FJ128GA010 to read the microphone values. When I read the raw values from the sensor I am getting like 270 but over time the value will go down to 90 and stop. 3.3V is being used and when noise is made the values will change even when its slowly going down to 90. I am not sure why this is the case. I just have 3.3V, ground and an analog input pin connected to the microphone. The microphone I believe picks up noise from -1.7V to 1.7V.
I am setting AD1CON2 = 0x0000; where AVss and AVdd is used as Vref.
Is there a way I can fix the readings from the microphone to display 90 right away? I know that 90 represents 0 and probably will need to fix that so the values would be read just on the positive side. Would using a capacitor help fix this issue?
I am very new to programming microcontrollers and would appreciate any help.

Keep ESP32 non-RTC GPIO pin state HIGH in deep sleep

I'm using Arduino IDE, and I'm trying to keep a pin's state held (HIGH, in my case).
I have GPIO16, GPIO17, GPIO18 (which I believe are all non-RTC GPIO pins). They're connected to three P-channel MOSFETs, which power an RGB LED. So my three pins need to be held HIGH while sleeping.
When my ESP32 goes into deep sleep, the RGB LED slowly fades up to full white brightness 😅
After searching around, the method posted elsewhere doesn't work as expected. I've tried the below, but no luck.
Arduino IDE:
gpio_hold_en((gpio_num_t) 16);
gpio_hold_en((gpio_num_t) 17);
gpio_hold_en((gpio_num_t) 18);
gpio_deep_sleep_hold_en();
delay(10000);
esp_deep_sleep_start();
Strangely though, now the blue LED (on GPIO 18) doesn't come on, just the other two (16 and 17) snap to full brightness as soon as I call deep sleep.
Does it matter that they're GPIO vs RTC GPIO?
Is it possible to hold them HIGH during deep sleep? I've also tried INPUT_PULLUP but no luck either.
They're SMD soldered on custom PCBs so I'm trying to solve this in software, before I consider swapping for N-Channel MOSFETs or just making new boards entirely.

Servo movement from side to side with PWM Arduino uploaded code

I'm using Arduino-Uno micro-controller with Adafruit servo shield. To move a quite fast 0.07 sec/60° mini digital servo motor with desired angle, for example from C# application:
myport.WriteLine(val.ToString());
I using PWM library:
pwm.setPWM(1, 0, Serial.parseInt());
Which moves motor to the assigned angle successfully, but I'm trying to figure out, how to move servo motor from one angle to another with full reaching of end points from uploaded code.
So if maximum and minimum of range is:
#define SERVOMIN 160
#define SERVOMAX 500
And servo motor performs from 210 to 450, here is a my attempt:
pwm.setPWM(1, 0, 210);
delay(12);
pwm.setPWM(1, 0, 450);
delay(12);
pwm.setPWM(1, 0, 210);
This way motor does not completes movement or even makes single direction movement without return. What should be a proper way, to make such movement within uploaded code.
Any advice, guide or example would be very helpful
You can't.
Normal servos don't provide any feedback on their position. It's not possible with software alone, unless you add long enough delay to make sure it completes the move even under load.
You either need a different servo or additional sensors.
More on Arduino Stack Exchange: How long does it take for a servo to change rotation?

How to use ad7490 with android things?

How to use ad7490 ADC in Android Things?
I have a sensor 16x16 Sensor Matrix. For this sensor matrix i want to read data of the each cell and position. I want to implement ad7490 ADC with the help of SPI and GPIO Pins connection.
My requirement is, how to read ad7490 A2D value on SPI pin.
Note : I had also implemented this on arduino uno using ic 7495 shift register and 74hc4067 mux. By these i am able to generate 16x16 matrix and read Analog data of each position. Now i want to implement same thing in android things with ad7490

Is NodeMCU's PWM limited to one frequency?

In the NodeMCU doc, it appears PWM on a pin may be set one of two ways, either through PWM or GPIO.
In the PWM doc it says that if you set PWM on one pin for a certain frequency then that frequency is set for all other pins using PWM. This means that you don't have RGB but rather 50 shades of grey.
The GPIO section does not mention this limitation. Is it on place? Is there only one timer on the chip itself available for PWM?
You seem to be confusing the effects of Frequency and Duty cycle on an RGB led, you will have 3 pins connected on which you generate a PWM, where you can set the duty cycle to change the intensity, whether all those frequencies are the same or not doesn't matter.

Resources