Go Down

Topic: using an Arduino board as an 'Programmer (Read 563 times) previous topic - next topic

Draht

I changed The Breadboard (a new one) and setup everything new on it and suddendly i could upload the Bootloader and my blinking sketch. Have no Idea what was wrong on the old Breadboard.

Draht

Now i Uoloaded my servosketch to the ATmega and want to run the servo only with the ATmega un the breadboard. I disconnected the Arduino and put my servo connected it to a exteral vcc5V and GD and the signal to Pin 13 of the Atmega. If i connect Pin6 to GD the Servo should do the Loop shown in the Code down below, but nothing hapens. Did i made a mistake?

Code: [Select]
#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
bool press1 ;
bool oldpress1 = false;
bool sweep1state = false;
int button2 = 5; //button pin, connect to ground to move servo
bool press2 ;
bool oldpress2 = false;
bool sweep2state = false;
Servo servo1;


void setup()
{
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  servo1.attach(7);
  Serial.begin(9600);
  Serial.println(" make servo sweep to 170  and back to 90 when button 1 pressed ");
  Serial.println(" make servo sweep  to 0 and backk to 90 when button 2 pressed ");
  servo1.write(90);              // initialises the servo to 90
}


void loop()
{
  press1 = digitalRead(button1);  // check button1
  if (press1 == LOW && oldpress1 == HIGH) // test if button has been pressed
  {
    sweep1state = HIGH;         // if yes
  }
  if (sweep1state == HIGH)      // button1 has been pressed, sweep to 170 and back to 90
  {
    servo1.write(170);
    delay(2000);
    servo1.write(90);
    sweep1state = LOW;          // sweep accomplished.
  }
  oldpress1 = press1;           //update press1 for next program loop


  press2 = digitalRead(button2);
  if (press2 == LOW && oldpress2 == HIGH) // test if button has been pressed
  {
    sweep2state = HIGH;         // if yes
  }
  if (sweep2state == HIGH)      // button2 has been pressed, sweep 0 to 0 and back to
  {
    servo1.write(0);
    delay(2000);
    servo1.write(90);
    sweep2state = LOW;          // sweep accomplished.
  }
  oldpress2 = press2;           //update press1 for next program loop
}

pert

When something is going wrong, the key to troubleshooting is to eliminate as many unnecessary variables as possible. With the sketch you're using, the problem could be a bug in the code, it could be that the buttons are not correctly wired, it could be that the servo is not correctly wired.

So you need to test each component individually. Start with a simple sketch that just moves the servo back and forth. You can find one written for you at File > Examples > Servo > Sweep. If the servo moves with this sketch uploaded, then you know the servo is wired correctly and is working.

Next would be a simple sketch to test your buttons. Generally, I would recommend a sketch that reads the button pin and prints the state to the Serial Monitor. But I don't think you have a serial to USB adapter connected to your ATmega328P, so you don't have that output option. Next best would be an LED that turns on or off depending on the button pin reading. If you don't have an LED, then you'll have to use the servo as an output to indicate whether the button is being read correctly.

One common mistake people make when starting to work with bare microcontroller chips is thinking the physical pin numbers are the Arduino pin numbers. They are not. You need to study the pin mapping to know which physical pins on the microcontroller are mapped to each Arduino pin number:
https://www.arduino.cc/en/Hacking/PinMapping168

You can see that, for example, Arduino pin 4 is physical pin 6 on the chip (PD4).

Draht

Thank you, and i thought the same thats why i tested my Atmega with the LED Blinkingsketch of the Arduino Libary.
And i also found a Mapping overview like the one you posted.
No i dont have serial to USB adapter. (not yet :-)

Right now i couldnt manage to do the Atmega the blinking of the LED. But i will try to find the mistake and tell here. What i was not sure is: do i have to change the setup un the breadboard to run the program on the Atmega or can i keep it like when i program the chip. only disconnet pin 13, 12, 11 and10?

pert

No i dont have serial to USB adapter. (not yet :-)
It's a very useful tool to have. You can get them for around $1 USD with free shipping on eBay if you don't mind waiting for shipping from China. Make sure to get one with the DTR or RTS pin broken out so that you can set it up to do the auto-reset during uploads, otherwise you'll need to manually reset the board at just the right time during each upload.

But you can get by with just having an LED or two to blink as output to tell you what is happening in the program.

Right now i couldnt manage to do the Atmega the blinking of the LED.
Definitely get that working first before trying anything more complicated.

do i have to change the setup un the breadboard to run the program on the Atmega or can i keep it like when i program the chip. only disconnet pin 13, 12, 11 and10?
No, you can leave your Arduino as ISP programmer connected if you like. The sketch will start running right after you do the "Upload Using Programmer". The only reason you would need to disconnect any of the pins is if you wanted to use them in your sketch.

Draht

Ok, thank you. yes i will try running the blinking LED. Right noe i got stuck with uploading the sketch again. I will have to find why. I think it has to do something with the connections.

Draht

Ok, the LED are blinking now with the disconnected Arduino board. ;)

Draht


Draht

The sweep is working, but not the Code below. I was testing the Code before with the Arduino and everything went fine. With Arduino Pin 4  or pin 5 had to be connected to GD to let the servo do the movment.
Now with the ATmege if connect Pin6 or Pin11 to GD the servo should move, No?

Code: [Select]
#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
bool press1 ;
bool oldpress1 = false;
bool sweep1state = false;
int button2 = 5; //button pin, connect to ground to move servo
bool press2 ;
bool oldpress2 = false;
bool sweep2state = false;
Servo servo1;


void setup()
{
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  servo1.attach(7);
  Serial.begin(9600);
  Serial.println(" make servo sweep to 170  and back to 90 when button 1 pressed ");
  Serial.println(" make servo sweep  to 0 and backk to 90 when button 2 pressed ");
  servo1.write(90);              // initialises the servo to 90
}


void loop()
{
  press1 = digitalRead(button1);  // check button1
  if (press1 == LOW && oldpress1 == HIGH) // test if button has been pressed
  {
    sweep1state = HIGH;         // if yes
  }
  if (sweep1state == HIGH)      // button1 has been pressed, sweep to 170 and back to 90
  {
    servo1.write(170);
    delay(2000);
    servo1.write(90);
    sweep1state = LOW;          // sweep accomplished.
  }
  oldpress1 = press1;           //update press1 for next program loop


  press2 = digitalRead(button2);
  if (press2 == LOW && oldpress2 == HIGH) // test if button has been pressed
  {
    sweep2state = HIGH;         // if yes
  }
  if (sweep2state == HIGH)      // button2 has been pressed, sweep 0 to 0 and back to
  {
    servo1.write(0);
    delay(2000);
    servo1.write(90);
    sweep2state = LOW;          // sweep accomplished.
  }
  oldpress2 = press2;           //update press1 for next program loop
}

Draht

Everything is fine now. i took by mistake Pin15 of the ATmega instead of 13.

pert

I'm glad to hear it's working now. Thanks for taking the time to post an update. Enjoy!

Draht

#26
Jan 15, 2020, 01:27 pm Last Edit: Jan 15, 2020, 01:56 pm by Draht
I'm glad to hear it's working now. Thanks for taking the time to post an update. Enjoy!
Yes me too :) I think its important to comunicate all the steps also for other users in a forum. Only like that we have the chance to learn and get better.
Its great forums like this one are existing. Thanks again for your help.

(Just beside i own now a serial to USB- Converter :)

Go Up