How to arduino question

Hi,

I am new in coding with Arduino for this school project, and all I found was mostly about using the servo library to control the servo movements, however one of the project requirements is not to use these libraries.

Thank you so much everyone for your suggestions and your valuable help.

Welcome to the forum.

It is just a bunch of pulses, as you can see in these pictures: https://forum.arduino.cc/t/controlling-a-digital-and-analogue-servo-with-one-arduino-uno/868138/7

Use pinMode(), digitalWrite(), delay() and delayMicroseconds() and try to move a servo motor to a position.
The pulses should run continuously.

1 Like

Many of the built in timers can be programmed to produce servo control pulses (which is basically what the servo library does).

1 Like

As jremington posted timers are the solution.
Here is a 'bare metal solution' that reads 2 pots and outputs 2 servo pulses accordingly.

/*
*
*	Author : Zedric
*
*/

uint16_t a {};
uint16_t b {};
const uint16_t TIME {39999};
const uint16_t MIN {};
const uint16_t MAX {1023};
const uint16_t MIN_2 {1805};  // Spektrum @100% travel = 1102 to 1898 | @150% = 903 to 2097
const uint16_t MAX_2 {4193};  //
uint16_t chan[2] {0, 0};
volatile uint8_t i {};

uint16_t remap(long x, long in_min, long in_max, long out_min, long out_max);

ISR(TIMER1_OVF_vect)
{
  ADCSRA |= (1 << ADSC);
}

ISR(ADC_vect)
{
  chan[i] = ADC;
  i ^= (1 << 0);
  ADMUX ^= (1 << MUX0);
}

void setup()
{
  DDRB |= (1 << PORTB5) | (1 << PORTB2) | (1 << PORTB1);
  ADMUX |= (1 << REFS0);
  ADCSRA |= (1 << ADEN) | (1 << ADSC) | (1 << ADIE) | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
  DIDR0 |= (1 << ADC1D) | (1 << ADC0D);
  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1 = 0;
  ICR1 = TIME;
  OCR1A = MIN_2;
  OCR1B = MIN_2;
  TIMSK1 = (1 << TOIE1);
  TCCR1A |= (1 << COM1A1) | (1 << COM1B1) | (1 << WGM11);
  TCCR1B |= (1 << WGM13) | (1 << WGM12) | (1 << CS11);  //Mode (14) Fast PWM & Clk/8
  sei();
  Serial.begin(115200);
}

void loop()
{
  if (a != chan[0] || b != chan[1])
  {
    a = chan[0];
    b = chan[1];
    OCR1A = remap(chan[0], MIN, MAX, MIN_2, MAX_2);
    OCR1B = remap(chan[1], MIN, MAX, MIN_2, MAX_2);
    Serial.print(F("Channel 1 = "));
    Serial.print(OCR1A);
    Serial.print(" ");
    Serial.print(F("Channel 2 = "));
    Serial.println(OCR1B);
  }
}

uint16_t remap(long x, long in_min, long in_max, long out_min, long out_max)
{
  return out_min + (x - in_min) * (out_max - out_min) / (in_max - in_min);
}
1 Like

Sorry for the lack of comments, I'm not used to posting code on forums :grimacing:

1 Like

The sketch by zedric in a simulation:

1 Like

My team members are more incline to use registers, can they be incorporated with the functions you just mentioned?

I couldn't, I think, wish for a better comment tho, I'm going to try that with the draft we have for our code and see how the servo reacts, thank you so much, will keep you updated on the outcome.

Totally agree, except that we aren't allowed to use libraries per say, I think they want us to dig deeper and understand the howabouts of how things work.

Thank you :slight_smile:

Just remember that data sheets are your best friend!

I've learned almost all I know about the Atmega328p from reading the data sheet.

Arduino functions and libraries are very handy but bare metal register manipulation is so much fun and a rewarding experience. :v:

1 Like

Absolutely, thank you so much!

Tip: We use the green button at the bottom for a generic reply.
A reply to a post does not look well on this forum.

If you select text with the mouse, then a "Quote" pops up, and if you click that, then the quote is pasted in your post.
Like this:

Yes, but you might get stuck to one type of microcontroller. The Arduino functions create a platform that is compatible with many boards.
Stay away from Timer0, that is used by Arduino.

1 Like

Why is that? I'm not familiar with forum etiquette apart from the obvious.

Very true, Arduino has put a substantial amount of work into this.

Absolutely, from the little I know, Timer0 is used for delay(), Millis(), Micros() etc...

1 Like

The forum uses "Discourse".
When someone replies to a post, then I have to scroll back to see what the question was and sometimes it is hard to find.

Neither do I :rofl:
A common problem is the XY-problem. We see that a lot.
This is not a question-and-answer forum, we give background information, ask questions that might seem silly, and sometimes you get many different opinions. There is even a person that talks about the Wokwi simulator all the time :clown_face:
When you get different opinions, just pick the one that suits you best.

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.