How do I do this program?

I have to write a program for homework that implements the power of 2^n using bitshift operators. The program should start when I press a push button and must do the following powers: 2^0, 2^1, 2^2, 2^3, 2^4...
For the result, I need to print 2^n in decimal and binary.
Could you help me? :frowning:

It is called bit shifting: result = 2 << n

And where would I put that on the program itself? (I'm a beginner)

Everything you need to know is in these pages:
https://www.arduino.cc/en/pmwiki.php?n=Reference/Bitshift

https://www.arduino.cc/reference/en/language/functions/communication/serial/print/

https://www.arduino.cc/reference/en/language/structure/control-structure/for/

But I've already tried those functions and I seriously can't do it, partially because I don't reallyunderstand what my teacher is asking me to do ¯_(ツ)_/¯
Could you show me how you would do it? :frowning:

Here you are more likely to receive help if you help yourself. I suggest you start reading up on the basics of how to write an Arduino program.

Then, come back and check the links I gave you.

Try to program it and post your results here.

Short of that I don't think you'll get someone to write the program for you.

Welp, ok, thanks for the help! :slight_smile:

Show us what you've tried and we can help

How could you trust the bridge you designed and built if you passed the design and building off to someone else?

Please give it a try, then if it don't work, show your best effort, post, he code, and ask for help.

If you want to be spoon fed go to gigs and collaborations and see if you can pay someone to do it for you.

I probably butchered this but here's the code anyway:

int x=0;
int buttonState;
void setup() {
  Serial.begin(9600);
  pinMode(7, INPUT_PULLUP);
    buttonState=digitalRead(7);
}
void loop() {
  if((buttonState==LOW)&&(x<=4)){
    Serial.print(2 << x);
    Serial.print(x, DEC)
    Serial.println(x, BIN);
  }
  x=x+1;
}

It's a good start, but the button read needs to be in loop, not startup.

I think you mean "1 << x"

Please remember to use code tags when posting code

After you fix the code, as per post#11, post the new code in code tag </> thingies.

The IDE is also jam-packed with useful worked examples, like how to detect when a switch becomes closed, as opposed to detecting when it is closed.

Sorry, I've edited the code!

You want a new power of 2 with each press of the button, or the first button should start an automatic sequence?

That ellipsis has me worried. The variable you store the value in will eventually overflow. What's your plan for when it does? You stop there? You still go on and observe what the overflow does to your values? You go back in reverse order?

But it's not the code you posted before, so my comments make little sense.

Guess how quickly x gets incremented

It should start an automatic sequence...

I... don't understand what you're asking.

Alright, I've restored the code and here's the "new" one:

int x=0;
int buttonState;
void setup() {
  Serial.begin(9600);
  pinMode(7, INPUT_PULLUP);
}
void loop() {
    buttonState=digitalRead(7);
  if((buttonState==LOW)&&(x<=4)){
    Serial.print(1 << x);
    Serial.print(x, DEC);
    Serial.println(x, BIN);
  }
  x=x+1;
}

Ok.
Think about what is happening to x every time through loop.
As the code stands, there is no point reading buttonState in setup...and you've changed the posted code again.

You are starting a sequence that is mathematically infinite. On real-world hardware, something will happen before you reach infinity. If you have no idea what may happen, start by reading this.