Program AtMega328 standalone.

Hi all,

I’m quite new to Arduino, and use a lot of information from Google/YouTube, but here I’m facing an issue.

First of all;
My project does not work yet. Unfortunately.

What I have is a single RGB LED. On a trigger (for testing pushbutton) I want to slow-pulse the blue color for 40 seconds and after that it should go green. after 5 sec. it should be turned off and waiting for a new trigger.

2nd of all;
Once my code is running. I want to run it standalone on the AtMega328 chip.
I do have all the components and such, but my main question how to run the code standalone on the 328 chip.

Here is my code so far which is not working

/*
Timing indicator

*/
const int greenPin = 4; // LED connected to digital pin 4
const int bluePin = 5; // LED connected to digital pin 5
const int sensePin = 2;

int senseState = 0;

void setup() {
pinMode(sensePin, INPUT_PULLUP);
}

void loop() {

senseState = digitalRead(sensePin);

while (senseState == 1) {
// fade in from min to max in increments of 5 points:
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
// sets the value (range from 0 to 255):
analogWrite(bluePin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
// fade out from max to min in increments of 5 points:
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
// sets the value (range from 0 to 255):
analogWrite(bluePin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
if (millis() >= 5000){
analogWrite(bluePin, 0);
analogWrite(greenPin, 255);
delay(2500);
analogWrite(greenPin,0);
analogWrite(bluePin,0);
exit(0);
}

}
}

As for how to use an 'atmega328 Standalone' then a Google search on those terms points to several tutorials.........

Hi

Could you please be a bit more specific about how your project doesn't work?

What are you expecting it to do?

What does it actually do?

What type of switch are you testing?

How is your board wired to the switch under test?

Ian

Hi Ian,

Currently I have a RGB LED with common +5v and 220ohm resistor. I have a momentary switch connected to ground and 2
R is connected to 3
G is connected to 4
B is connected to 5

I don't use the color red for this project.

When pushing the button the LED should pulse in the color blue, adjustable in programming for speed of pulsing.

After 40 seconds I want the LED to stop pulsing in blue and lit up in green constant(no pulse)
It will stay on for 5 seconds in green and after that it should turn off and wait for a new trigger of the switch

darthvader072:
Hi Ian,

Currently I have a RGB LED with common +5v and 220ohm resistor. I have a momentary switch connected to ground and 2
R is connected to 3
G is connected to 4
B is connected to 5

I don't use the color red for this project.

When pushing the button the LED should pulse in the color blue, adjustable in programming for speed of pulsing.

After 40 seconds I want the LED to stop pulsing in blue and lit up in green constant(no pulse)
It will stay on for 5 seconds in green and after that it should turn off and wait for a new trigger of the switch

Ok

That is how it's wired and what you expect it to do. What is it actually doing?

Ian

Well, the pulsing of the blue LED works.
But, it does start without waiting for a trigger from the pushbutton.
Also, it goes on continuous. So basically of all my wishes, only the led pulsing in blue works.

Ooopsidaisie...

if (millis() >= 5000) {
 ...
 exit(0);
}

darthvader072:
Well, the pulsing of the blue LED works.
But, it does start without waiting for a trigger from the pushbutton.
Also, it goes on continuous. So basically of all my wishes, only the led pulsing in blue works.

First place to start is your logic is inverted. When you press your switch it pulls the sense input from high to low.

You are looking for the sense pin to be high.

Also your logic for the LED's is inverted, analogWrite(pinNo., 0) will set the output to low which will turn the LED fully on and writing a value of 255 will turn the LED off.

Next you don't need a while loop a simple if(senseState = LOW) will work.

Each of your ramps takes 1.53sec (255/5 x 0.03) so a full blue led cycle takes 3.06sec. To give you 40sec of blue led cycling you are going to need to repeat this 13 times. A simple for loop will accomplish this.

Not sure what you are trying to achieve with the if (millis() >= 5000) condition, millis() starts at 0 when the board is powered up or reset and only resets to zero after about 53.5 days, so it's almost always going to be greater than 5000.

Again your logic is wrong for the LED's. The blue LED should be set to 255 and the green to 0 then after the delay green should be set to 255 to turn it off.

Hope this helps and let us know how you get on.

Ian

IanCrowe:
Also your logic for the LED's is inverted, analogWrite(pinNo., 0) will set the output to low which will turn the LED fully on and writing a value of 255 will turn the LED off.

Did you invert your logic?

Danois90:
Did you invert your logic?

No I'm taking this from the OPin reply #3:-

Currently I have a RGB LED with common +5v and 220ohm resistor.

as meaning they are using a common anode RGB LED pulled up to 5V via a 220ohm resistor and so in order to turn any led on you have to pull the relevant cathode to 0V. Therefore analogWrite(pinNo., 0) is fully on and analogWrite(pinNo., 255) is fully off.

I may of course have misinterpreted what the OP was saying.

Ian

IanCrowe:
I may of course have misinterpreted what the OP was saying.

Does a RGB LED with 1 anode and 3 cathodes exist?

Danois90:
Does a RGB LED with 1 anode and 3 cathodes exist?

Yep,

both common Anode and common Cathode versions exist. Try googling RGB LED and you’ll see references to both (and also the intelligent versions such as WS2812 etc).

Ian

Ian,

Thanks so far, i’m getting close, thanks to your help.
The button now triggers the program, also the LED on/off is fixed.

After uploading, the first time the program works great, exactly how I want it to work.
However, when I trigger the program again it seems to get stuck in solid blue LED… It seems to skip it’s final part of the program where the LED should turn on in green and off.

/*
Timing indicator

*/
const int greenPin = 4; // LED connected to digital pin 4
const int bluePin = 5; // LED connected to digital pin 5
const int sensePin = 2;

int senseState = 0;

void setup() {
pinMode(sensePin, INPUT_PULLUP);
}

void loop() {

senseState = digitalRead(sensePin);

if(senseState == LOW) {
for (int x = 0; x <= 12; x++){

// fade in from min to max in increments of 5 points:
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
// sets the value (range from 0 to 255):
analogWrite(bluePin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}

// fade out from max to min in increments of 5 points:
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
// sets the value (range from 0 to 255):
analogWrite(bluePin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}
// set changes to be made after it reaches 45sec
if (millis() <= 45000){
analogWrite(bluePin, 255);
analogWrite(greenPin, 0);
delay(5000);
analogWrite(greenPin,255);
analogWrite(bluePin,255);

}
}
}

Fair enough, never stumbled on such a weird thing! If OP actually has a common anode RGB LED, then the logic is inverted. Another thing is that pin 4 has no PWM and does not support analogWrite(), that's probably why the green part does not work.

??

darthvader072:
Ian,

Thanks so far, i’m getting close, thanks to your help.
The button now triggers the program, also the LED on/off is fixed.

After uploading, the first time the program works great, exactly how I want it to work.
However, when I trigger the program again it seems to get stuck in solid blue LED… It seems to skip it’s final part of the program where the LED should turn on in green and off.

/*

Timing indicator

*/
const int greenPin = 4;    // LED connected to digital pin 4
const int bluePin = 5;    // LED connected to digital pin 5
const int sensePin = 2;

int senseState = 0;

void setup() {
pinMode(sensePin, INPUT_PULLUP);
}

void loop() {

senseState = digitalRead(sensePin);

if(senseState == LOW) {
for (int x = 0; x <= 12; x++){

// fade in from min to max in increments of 5 points:
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
 // sets the value (range from 0 to 255):
 analogWrite(bluePin, fadeValue);
 // wait for 30 milliseconds to see the dimming effect
 delay(30);
}

// fade out from max to min in increments of 5 points:
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
 // sets the value (range from 0 to 255):
 analogWrite(bluePin, fadeValue);
 // wait for 30 milliseconds to see the dimming effect
 delay(30);
 }
}
// set changes to be made after it reaches 45sec  
if (millis() <= 45000){
 analogWrite(bluePin, 255);
 analogWrite(greenPin, 0);
 delay(5000);
 analogWrite(greenPin,255);
 analogWrite(bluePin,255);

}
}
}

That’s happening because, as I said earlier, millis() does not reset with each run of the sketch only when the device is powered up, reset with the reset pin or rolls-over after approximately 53 days. Therefore the second time around millis() will return a value greater than 45000 and so on.

Why are you bothering to test the value of millis()? Surely all you want to do is after the blue LED has ramped up and down 13 times is turn the blue LED off and turn the green LED on for 5sec the turn it off again. You don’t need to do any sort of conditional test to do that.

Ian

Danois90:
Fair enough, never stumbled on such a weird thing! If OP actually has a common anode RGB LED, then the logic is inverted. Another thing is that pin 4 has no PWM and does not support analogWrite(), that's probably why the green part does not work.

Yep.

In fact if you check most PC motherboards with RGB controls on them are set up to drive common anode LED strips. They supply +12V and R, G and B signals to the strips (sometimes also a W(hite) signal). So common anode is fairly common.

Ian

Yep, it's fixed. Program is running, thanks Ian.

Which brings me to my 2nd issue. Making it run on a single atmega328p chip.

I've already tried it with an older program i've done before.
I'm facing issues on burning the bootloader. I get or these responses:

avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x03
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x03
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x03
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x03
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x03
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x03
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x03
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x03
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x03
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x03
Error while burning bootloader.

Or else a constant messsage with Burning to I/O board, this can take a minute (which takes way longer, and does not stop)
Any suggestions perhaps?

Check your wiring.

How are you connecting to the 328p?

Ian