Solved

Convert C++ to acceptable Arduino C++

Posted on 2014-09-08
1
121 Views
Last Modified: 2014-12-27
I have the following working C++ code in GCC and want to convert it to its Arduino C++ compatible version to produce the output listed just below this code:

#include <vector>
#include <iostream>
#include <algorithm>

void fadeOn(int maxvalue, int maxlength) {
    int counter;
    int maxcounter = 1;
    int vtemp = 0;
    
    std::vector<int> v{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 
    
    for (counter=1; counter < maxlength + maxvalue; counter++) {
      // simple rotation to the right
      
      std::cout << "simple rotate right " << counter << ": ";
      for (int n: v)
          std::cout << n << ' ';
      std::cout << '\n';
      
      vtemp = v[0];
      
      std::rotate(v.rbegin(), v.rbegin() + 1, v.rend());
      
      if (maxcounter < maxvalue) {
        v[0] = vtemp + 1;
      } else {
        v[0] = maxvalue;
      }
      
      maxcounter++;
    }
}

int main()
{
    fadeOn(7,13);
}

Open in new window


CURRENT OUTPUT of above code:

simple rotate right 1: 1 0 0 0 0 0 0 0 0 0 0 0 0
simple rotate right 2: 2 1 0 0 0 0 0 0 0 0 0 0 0
simple rotate right 3: 3 2 1 0 0 0 0 0 0 0 0 0 0
simple rotate right 4: 4 3 2 1 0 0 0 0 0 0 0 0 0
simple rotate right 5: 5 4 3 2 1 0 0 0 0 0 0 0 0
simple rotate right 6: 6 5 4 3 2 1 0 0 0 0 0 0 0
simple rotate right 7: 7 6 5 4 3 2 1 0 0 0 0 0 0
simple rotate right 8: 7 7 6 5 4 3 2 1 0 0 0 0 0
simple rotate right 9: 7 7 7 6 5 4 3 2 1 0 0 0 0
simple rotate right 10: 7 7 7 7 6 5 4 3 2 1 0 0 0
simple rotate right 11: 7 7 7 7 7 6 5 4 3 2 1 0 0
simple rotate right 12: 7 7 7 7 7 7 6 5 4 3 2 1 0
simple rotate right 13: 7 7 7 7 7 7 7 6 5 4 3 2 1
simple rotate right 14: 7 7 7 7 7 7 7 7 6 5 4 3 2
simple rotate right 15: 7 7 7 7 7 7 7 7 7 6 5 4 3
simple rotate right 16: 7 7 7 7 7 7 7 7 7 7 6 5 4
simple rotate right 17: 7 7 7 7 7 7 7 7 7 7 7 6 5
simple rotate right 18: 7 7 7 7 7 7 7 7 7 7 7 7 6
simple rotate right 19: 7 7 7 7 7 7 7 7 7 7 7 7 7

Now I would like to have the same outcome with Arduino C++ but I don't want to use STL libraries because I have tried and it simply makes a mess.

Any ideas?  Queues, circle buffers, ring buffers, etc... ?
0
Comment
Question by:wfninpa
1 Comment
 
LVL 33

Accepted Solution

by:
sarabande earned 500 total points
ID: 40311476
you may try the following code not using stl.

#include <stdio.h>

void fadeOn(int maxvalue, int maxlength) {
    int counter;
    int maxcounter = 1;
    int vtemp = 0;
    
    int v[] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 
    
    for (counter=1; counter < maxlength + maxvalue; counter++) {
      // simple rotation to the right
      
     printf ("simple rotate right %d", counter);
     for (int n = 0; n < 12; ++n)
          printf("%d ", n);
      printf("'\n");

      
      vtemp = v[0];
      for (int r = 11; r > 0; --r)
            v[r] = v[r-1];
      
      if (maxcounter < maxvalue) {
        v[0] = vtemp + 1;
      } else {
        v[0] = maxvalue;
      }
      
      maxcounter++;
    }
}

Open in new window


Sara
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to gracefully close the c++ 11 thread? 3 83
How can i compile this github project?? 2 76
What is sub-make ? 2 40
Error creating a new C++ project in ,net 20 21
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now