Changing state with nano

this line edited: I am using a sample program from UKHeliBob

For some reason it doesn't work for me.
I am using a nano so my question is: will this line work with a nano:
digitalWrite(ledPin, ! digitalRead(ledPin));

unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 3000;  //the value is a number of milliseconds
const byte ledPin = 10;    //using the built in LED

void setup()
{
  Serial.begin(115200);  //start Serial in case we need to print debugging info
  pinMode(ledPin, OUTPUT);
  startMillis = millis();  //initial start time
}

void loop()
{
  currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  if (currentMillis - startMillis >= period)  //test whether the period has elapsed
  {
    digitalWrite(ledPin, ! digitalRead(ledPin));  //if so, change the state of the LED.  Uses a neat trick to change the state
    startMillis = currentMillis;  //IMPORTANT to save the start time of the current LED state.
  }
}

What do you mean by "doesn't work"? Please explain what you expected to happen, and what happened instead.

There are several Arduinos called "nano". Which one do you have?

Yes. It inverts the state of ledPin as long as that pin is set to be an output

And when you tried it, what happened ?

I expected the led to blink, it does not.

It works fine on my Uno if I change ledpin to be 13.

Show us your wiring, a good image please.

the led stays on

no camera available but simple pin 13 to led + and Led _ to ground on nano it's a 12v led so it works fine without a resistor.

“no camera available but simple pin 13 to led + and Led _ to ground on nano it’s a 12v led so it works fine without a resistor.”

The code says pin:

const byte ledPin = 10; //using the built in LED

10

ya I see it says 10 now, I changed it from 13 to 10 trying to utilize a different pin to zero in on the problem. it doesn't work on 10 or 13. it this a nano thing?

UNO same as Nano

Show us the code that does not work for pin 10.

LED should be similar to D6 Or D5 as seen here.

I don't have the pullup resistor in the pin but it is already high. I assume because it is on all the time. Please feel free to correct me if im wrong.

You don’t need a pull up.

Try this:


unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 3000;  //the value is a number of milliseconds
const byte ledPin = 10;    //using the built in LED
byte state = LOW;


void setup()
{
  Serial.begin(115200);  //start Serial in case we need to print debugging info
  pinMode(ledPin, OUTPUT);
  startMillis = millis();  //initial start time
}

void loop()
{
  currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  if (currentMillis - startMillis >= period)  //test whether the period has elapsed
  {
    digitalWrite(ledPin, state);  //if so, change the state of the LED.  Uses a neat trick to change the state
    state = !state;
    startMillis = currentMillis;  //IMPORTANT to save the start time of the current LED state.
  }
}

Can you blink the LED with the blink example as found in the examples in the IDE?

I read you using a 12 volt LED and feeding it 5 volts, does that even work?

Try putting 5 volts directly to the LED.

Then try a regular LED with an appropriate series resistor.

Wait, what? "the led stays on"

a7

the sketch does not compile, state not declared in this scope. I can

Yes, I have added state as a global variable now.

Try the sketch again.

the led works fine it is lit all the time during the sketch operation except during the initialization stage. thanks

Get wife to buy you a camera :wink: .

1 Like

Or draw it with a Sharpie and hold it up to the camera on your computer.

a7