Search code examples
c++arduinodrawingarduino-c++

How can I make additions to this code to print a full triangle with my arduino?


It obviously only makes half a triangle but I don't know how to fix that. I'm new to this and honestly don't really know how to work it, so help would be greatly appreciated. I got this far but now I'm lost:

void setup() 
{
  Serial.begin(9600);
  Serial.setTimeout(100000); // timeout now is 100 seconds

  // read a number from serial port
  String s = Serial.readStringUntil(10); // read a line from serial port
  int n = s.toInt(); // convert the input string to integer value

for (int i=0; i<n; i++){
  for (int j=0; j<i+1; j++){
    Serial.print("*" );
   }

Serial.print("\n");
}

}

void loop(){

}

Solution

  • replace n with rows

    for(int i = 1, k = 0; i <= rows; ++i, k = 0)
    {
        for(space = 1; space <= rows-i; ++space)
        {
            cout <<"  ";
        }
    
        while(k != 2*i-1)
        {
            cout << "* ";
            ++k;
        }
        cout << endl;
    }    
    

    here the output

    https://ideone.com/OPMeO1