Understanding the language, error messages, etc.
by kerunaru » Thu Nov 05, 2015 9:03 am
Okay… So yesterday I realised I don't know programming, at least, not all I thought I knew. One thing is web development and another thing is C programming for a tiny system like Gamebuino.
Auto-critic done, here goes my question. I'm trying to make a simple sine wave scrolling demo. My problem is, basically, I don't know how to board the problem to make a correct solution… I only achieved to show a static sine wave, so I thought you, guys, could show me the path to walk.
Thanks in advance.
-
kerunaru
-
- Posts: 7
- Joined: Thu Jul 23, 2015 6:50 am
- Location: Gaia
by wwtom » Sat Nov 07, 2015 3:54 pm
Hey,
I think your static sine programm looks like
- Code: Select all
for(int i = 0; i < 84; i++){
gb.display.drawPixel(i, (sin(2 * Pi / 360 * i * 5) * 22 + 22));
}
(Maybe a bit less complicated, but that was just what I did in a few Minutes to help you)
I hope you now what I did there with those numbers.
To make this wave moving, you just have to add a variable, like "im" (imove).
- Code: Select all
for(int i = 0; i < 84; i++){
gb.display.drawPixel(i, (sin(2 * Pi / 360 * (i + im) * 5) * 22 + 22));
}
Now you just have to add 1 to "im" each time the whole loop finished and the wave will move
http://gph.is/1PuIsWGFinished programm:
- Code: Select all
#include <SPI.h>
#include <Gamebuino.h>
Gamebuino gb;
int im = 0;
float Pi = 3.1415;
void setup() {
gb.begin();
gb.titleScreen(F("SIN WAVING"));
}
void loop() {
if(gb.update()){
im ++;
for(int i = 0; i < 84; i++){
gb.display.drawPixel(i, (sin(2 * Pi / 360 * (i + im) * 5) * 22 + 22));
}
}
}
-
wwtom
-
- Posts: 16
- Joined: Tue Jun 02, 2015 6:28 pm
by kerunaru » Tue Nov 10, 2015 11:10 am
wwtom wrote:Hey,
- Code: Select all
for(int i = 0; i < 84; i++){
gb.display.drawPixel(i, (sin(2 * Pi / 360 * i * 5) * 22 + 22));
}
(Maybe a bit less complicated, but that was just what I did in a few Minutes to help you)
I hope you now what I did there with those numbers.
If I understood well you are calculating the y position of the pixel based on the sine of a degree times the current index of the for loop in order to obtain a different position on each iteration; I think the times 5 is used to get a higher frecuency in the wave… The rest is just to acommodate the result line on the screen.
So yes, if I put another variable to add to the current index, the wave is going to move as it will have a different y position in function of the value of the "im" variable which is updated after each screen update.
Am I right?
-
kerunaru
-
- Posts: 7
- Joined: Thu Jul 23, 2015 6:50 am
- Location: Gaia
by wwtom » Wed Nov 11, 2015 10:11 am
I couldnt describe it better!
You're right
-
wwtom
-
- Posts: 16
- Joined: Tue Jun 02, 2015 6:28 pm
Return to Programming Questions
Who is online
Users browsing this forum: Google [Bot] and 18 guests