Related
(I am using Arduino IDE)
The Thing I am trying to achieve is it saving the "progress" even when the microcontroller turns off. I have tried for a while tweaking the code but I have yet to get it to actually save/read values from EEPROM properly, so here's the part of my code(Ignore the rest for running the display+i have removed all of the irrelevant code):
#include <EEPROM.h>
int hunger = 10000;
int happiness = 10000;
int y = 0;
int z = 0;
int sensorPin = A0;
int sensorPin2 = A1;
void setup() {
Serial.begin(9600);
EEPROM.write(5, 10000);
EEPROM.write(5, 10000);
hunger = EEPROM.read(8)- '0';
happiness = EEPROM.read(5)- '0';
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
display.clearDisplay();
display.display();
Serial.begin(9600);
pinMode(sensorPin,INPUT);
}
void loop() {
int value = happiness;
int value2 = hunger;
EEPROM.write(5, happiness);
EEPROM.write(8, hunger);
hunger -= 1;
happiness -= 1;
if(analogRead(sensorPin)>1020){
testhappy();
happiness+=2500;
}
else{
testsad();
}
while(analogRead(sensorPin2)>1020){
testeat();
hunger+=5000;
}
}
void testhappy(void){
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.clearDisplay();
display.print(hunger/100);display.print("/");display.print(happiness/100);
display.drawBitmap(30, 32, Happy32_glcd_bmp, 32, 32, 1);
display.display();
delay(1);
display.display();
delay(1);
for(y=0;y<2;y++){
for(z=0;z<5;z++){
display.setCursor(0,0);
display.clearDisplay();
display.print(hunger/100);display.print("/");display.print(happiness/100);
display.drawBitmap((30+z), 32, Happy32_glcd_bmp, 32, 32, 1);
display.display();
}
for(z=5;z!=0;z-=1){
display.setCursor(0,0);
display.clearDisplay();
display.print(hunger/100);display.print("/");display.print(happiness/100);
display.drawBitmap((30+z), 32, Happy32_glcd_bmp, 32, 32, 1);
display.display();
}
for(z=0;z> -5;z-=1){
display.setCursor(0,0);
display.clearDisplay();
display.print(hunger/100);display.print("/");display.print(happiness/100);
display.drawBitmap((30+z), 32, Happy32_glcd_bmp, 32, 32, 1);
display.display();
}
for(z=0-5;z!=0;z+=1){
display.setCursor(0,0);
display.clearDisplay();
display.print(hunger/100);display.print("/");display.print(happiness/100);
display.drawBitmap((30+z), 32, Happy32_glcd_bmp, 32, 32, 1);
display.display();
}
}
display.stopscroll();
}
void testsad(void){
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.clearDisplay();
display.print(hunger/100);display.print("/");display.print(happiness/100);
display.drawBitmap(30, 32, Mild32_glcd_bmp, 32, 32, 1);
display.display();
delay(1);
}
void testeat(void){
delay(200);
for(y=0;y<6;y++){
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.clearDisplay();
display.print(hunger/100);display.print("/");display.print(happiness/100);
display.drawBitmap(30, 32, Eat32_glcd_bmp, 32, 32, 1);
display.drawBitmap(60, 38, Cookie18_glcd_bmp, 8, 8, 1);
display.display();
delay(200);
display.clearDisplay();
display.setCursor(0,0);
display.print(hunger/100);display.print("/");display.print(happiness/100);
display.drawBitmap(60, 38, Cookie28_glcd_bmp, 8, 8, 1);
display.drawBitmap(30, 32, Mild32_glcd_bmp, 32, 32, 1);
display.display();
delay(200);
}
}
void testdrawbitmap(const uint8_t *bitmap, uint8_t w, uint8_t h) {
uint8_t icons[NUMFLAKES][3];
// initialize
for (uint8_t f=0; f< NUMFLAKES; f++) {
icons[f][XPOS] = random(display.width());
icons[f][YPOS] = 0;
icons[f][DELTAY] = random(5) + 1;
Serial.print("x: ");
Serial.print(icons[f][XPOS], DEC);
Serial.print(" y: ");
Serial.print(icons[f][YPOS], DEC);
Serial.print(" dy: ");
Serial.print(icons[f][DELTAY], DEC);
}
while (1) {
// draw each icon
for (uint8_t f=0; f< NUMFLAKES; f++) {
display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, WHITE);
}
display.display();
delay(200);
// then erase it + move it
for (uint8_t f=0; f< NUMFLAKES; f++) {
display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, BLACK);
// move it
icons[f][YPOS] += icons[f][DELTAY];
// if its gone, reinit
if (icons[f][YPOS] > display.height()) {
icons[f][XPOS] = random(display.width());
icons[f][YPOS] = 0;
icons[f][DELTAY] = random(5) + 1;
}
}
}
}
(I am getting no errors when compiling the code)
Look here at the page for EEPROM.write here. https://www.arduino.cc/en/Reference/EEPROMWrite
There it says the arguments are defined as:
EEPROM.write(address, value)
Parameters address: the location to write to, starting from 0 (int)
value: the value to write, from 0 to 255 (byte)
Now look at the number you're trying to write into EEPROM:
EEPROM.write(5, 10000);
Hopefully now you can see what the problem is. Your second argument is far too large. You should break it into two bytes or use a different method like EEPROM.put (look that up) which doesn't suffer this limitation.
Trying to read the value's from two analog sensors.
Can you use the for loop to read analog pins?
int i;
for (i = 0; i < 2; i = i + 1) {
x[I]=(analogRead(A[i]);
Working Code:
int AnalogpIn[2];
int MapValue[2];
void setup() {
Serial.begin(9600);
}
void loop() {
delay(100);
AnalogpIn[0] = analogRead(A0);
MapValue[0] = map(AnalogpIn[0], 0, 1023, 0, 255);
delay(100);
AnalogpIn[1] = analogRead(A1);
MapValue[1] = map(AnalogpIn[1], 0, 1023, 0, 255);
int i;
for (i = 0; i < 2; i = i + 1) {
Serial.print(AnalogpIn[i]);
Serial.print("-");
Serial.print(MapValue[i]);
Serial.println("");
delay (100);
}
}
Try this out,
I think this is what you are looking for. Have made few changes.
void loop(){
int i=0;
for (int i = 0; i < 2; i++) {
AnalogpIn[i] = analogRead(i);
MapValue[i] = map(AnalogpIn[i], 0, 1023, 0, 255);
Serial.print(AnalogpIn[i]);
Serial.print("-");
Serial.print(MapValue[i]);
Serial.println("");
}
delay (100);
}
How do I just create some data on device and then send/read it on the host?
I tried following but does not seem to work.
#include <stdio.h>
#include <stdlib.h>
#include "vectors.h"
#include "sphere.h"
#include "shading.h"
#include "ray.h"
#include "stdbool.h"
#include <CL/cl.h>
#if defined __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#elif defined (WIN32)
#include <GL/freeglut.h>
#else
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/freeglut_std.h>
#endif
#include <time.h>
VECTOR3D light;
SPHERE sphere[NSPHERES];
static PIXEL pixel;
VIEWPORT viewport;
VECTOR3D view_point;
VEC_BASIS camera_frame;
cl_double focal_distance;
double color;
//double kd_red, kd_green, kd_blue;
//double ks_red, ks_green, ks_blue;
double red, green, blue;
double light_intensity, ambi_light_intensity;
double theta, reflected_theta;
int bShadow = 0;
int direction[NSPHERES];
int intersection_object = -1; // none
double current_lambda = 0x7fefffffffffffff; // maximum positive double
double current_reflected_lambda = 0x7fefffffffffffff; // maximum positive double
// window identifier:
static int win;
void Timer (int obsolete) {
glutPostRedisplay();
glutTimerFunc(10, Timer, 0);
}
// opencl stuff
typedef struct cl_struct {
cl_platform_id platform_id;
cl_device_id device_id;
cl_context context;
cl_command_queue queue;
} cl_struct;
#define MAX_SOURCE_SIZE (0x100000)
void disp2(void) {
int i,j;
PIXEL* pCurrentPixel;
PIXEL* pPixels;
int VPWIDTH = viewport.xvmax - viewport.xvmin;
int VPHEIGHT = viewport.yvmax - viewport.yvmin;
pPixels = (PIXEL*)(viewport.pPixels);
//clear all pixels:
glClear(GL_COLOR_BUFFER_BIT);
// For all pixels:
for (i=0; i<VPWIDTH; i++) {
for (j=0; j<VPHEIGHT; j++) {
pCurrentPixel = (PIXEL*)(pPixels + VPWIDTH*i + j);
//set color for the current pixel:
glColor3f(pCurrentPixel->rgb[0] , pCurrentPixel->rgb[1], pCurrentPixel->rgb[2]);
// draw pixel
glBegin(GL_POINTS);
glVertex2i(i, j);
glEnd();
} // j
} //i
//glFlush();
glutSwapBuffers();
}
void init(void) {
direction[0] = 1;
direction[1] = 0;
direction[2] = 1;
pixel.i = 0;
pixel.j = 0;
// set scene:
// 1. define viewport
viewport.xvmin = -VIEWPLANE;
viewport.yvmin = -VIEWPLANE;
viewport.xvmax = VIEWPLANE;
viewport.yvmax = VIEWPLANE;
// 2. allocate enough space for pixels in viewport
viewport.pPixels = (PIXEL *) malloc(sizeof(PIXEL) * (viewport.xvmax - viewport.xvmin) * (viewport.yvmax- viewport.yvmin));
// 3. set camera:
camera_frame.u.x = 1.0;
camera_frame.u.y = 0.0;
camera_frame.u.z = 0.0;
camera_frame.v.x = 0.0;
camera_frame.v.y = 1.0;
camera_frame.v.z = 0.0;
camera_frame.n.x = 0.0;
camera_frame.n.y = 0.0;
camera_frame.n.z = 1.0;
view_point.x = (viewport.xvmax - viewport.xvmin) / 2.0 ;
view_point.y = (viewport.yvmax - viewport.yvmin) / 2.0 ;
view_point.z = 0.0;
// 4. set light:
light.x = view_point.x - 1300;
light.y = view_point.y + 1300 ;
light.z = view_point.z - 300;
ambi_light_intensity = 1.0;
light_intensity = 1.0;
focal_distance = FOCALDIST;
// 5. put spheres behind the viewport:
sphere[0].radius = RADIUS/1.5;
sphere[0].center.x = view_point.x - (RADIUS+30);
sphere[0].center.y = view_point.y ;
sphere[0].center.z = view_point.z - focal_distance - (2*RADIUS+20);
// the first sphere is blue:
set_rgb_array(sphere[0].kd_rgb, 0.0, 0.0, 0.8);
set_rgb_array(sphere[0].ks_rgb, 1.0, 1.0, 1.0);
set_rgb_array(sphere[0].ka_rgb, 0.0, 0.0, 0.2);
sphere[0].shininess = 100.0;
sphere[0].mirror = false;
sphere[1].radius = RADIUS/1.2;
sphere[1].center.x = view_point.x + 0;
sphere[1].center.y = view_point.y + 50;
sphere[1].center.z = view_point.z - focal_distance - (3*RADIUS+20);
// the second sphere is green:
set_rgb_array(sphere[1].kd_rgb, 0.0, 0.5, 0.0);
set_rgb_array(sphere[1].ks_rgb, 1.0, 1.0, 1.0);
set_rgb_array(sphere[1].ka_rgb, 0.0, 0.2, 0.0);
sphere[1].shininess = 10.0;
sphere[1].mirror = false;
sphere[2].radius = RADIUS;
sphere[2].center.x = view_point.x + (2*RADIUS+30);
sphere[2].center.y = view_point.y + 100;
sphere[2].center.z = view_point.z - focal_distance - (4*RADIUS+20);
// the third sphere is red:
set_rgb_array(sphere[2].kd_rgb, 1.0, 0.0, 0.0);
set_rgb_array(sphere[2].ks_rgb, 1.0, 1.0, 1.0);
set_rgb_array(sphere[2].ka_rgb, 0.2, 0.0, 0.0);
sphere[2].shininess = 100.0;
sphere[2].mirror = false;
sphere[3].radius = 1*RADIUS;
sphere[3].center.x = view_point.x ;
sphere[3].center.y = view_point.y - 100*RADIUS-130;
sphere[3].center.z = view_point.z - focal_distance - (4*RADIUS+20);
// the third sphere is red:
set_rgb_array(sphere[3].kd_rgb, 0.5, 0.5, 0.5);
set_rgb_array(sphere[3].ks_rgb, 1.0, 1.0, 1.0);
set_rgb_array(sphere[3].ka_rgb, 0.5, 0.5, 0.5);
sphere[3].shininess = 100.0;
sphere[3].mirror = false;
// set clearing (background) color to white:
glClearColor(0.0, 0.0, 0.0, 0.0);
// specify that ortgogonal 2D projection is to be used to
// map context of 2D world coordinats to the screen. We use the
// world-coordinate rectangle of the same aspect ratio as the display window
// so ther is no distortion:
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, WINDOW, 0.0, WINDOW);
}
int main(int argc, const char * argv[]) {
clock_t startCPU, endCPU, startGPU, endGPU;
// init glut:
glutInit (&argc, argv);
// specify the display mode to be RGB and single buffering:
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
// specify the initial window position:
glutInitWindowPosition(100, 100);
// specify the initial window size:
glutInitWindowSize(WINDOW,WINDOW);
// create the window and set title:
win = glutCreateWindow("Basic Ray Tracer by Pa3cio, UL FRI");
init();
// Create the two input vectors
int i, j, k, l;
int VPWIDTH = viewport.xvmax - viewport.xvmin;
int VPHEIGHT = viewport.yvmax - viewport.yvmin;
// PIXEL* pixels = (PIXEL*) malloc(sizeof(PIXEL) * VPWIDTH * VPHEIGHT);
PIXEL* pPixelsFromGPU = (PIXEL*) malloc(sizeof(PIXEL) * VPWIDTH * VPHEIGHT);
PIXEL* pCurrentPixel;
PIXEL* pPixels;
RAY ray, shadow_ray;
SPHERE_INTERSECTION intersection, current_intersection, shadow_ray_intersection;
// Load the kernel source code into the array source_str
FILE *fp;
char *source_str;
size_t source_size;
fp = fopen("/home/rokj/sula/vpsa/seminarska/kernel.cl", "r");
if (!fp) {
fprintf(stderr, "Failed to load kernel.\n");
exit(1);
}
source_str = (char*)malloc(MAX_SOURCE_SIZE);
source_size = fread( source_str, 1, MAX_SOURCE_SIZE, fp);
fclose( fp );
// Get platform and device information
cl_platform_id platform_id = NULL;
cl_device_id device_id = NULL;
cl_uint ret_num_devices;
cl_uint ret_num_platforms;
cl_int ret = clGetPlatformIDs(1, &platform_id, &ret_num_platforms);
ret = clGetDeviceIDs( platform_id, CL_DEVICE_TYPE_CPU, 1,
&device_id, &ret_num_devices);
// Create an OpenCL context
cl_context context = clCreateContext( NULL, 1, &device_id, NULL, NULL, &ret);
// Create a command queue
cl_command_queue command_queue = clCreateCommandQueue(context, device_id, 0, &ret);
// Create memory buffers on the device for each vector
cl_mem output = clCreateBuffer(context, CL_MEM_WRITE_ONLY|CL_MEM_ALLOC_HOST_PTR,
VPWIDTH * VPHEIGHT * sizeof(PIXEL), NULL, &ret);
// Create a program from the kernel source
cl_program program = clCreateProgramWithSource(context, 1,
(const char **)&source_str, (const size_t *)&source_size, &ret);
// Build the program
ret = clBuildProgram(program, 1, &device_id, NULL, NULL, NULL);
// Create the OpenCL kernel
cl_kernel kernel = clCreateKernel(program, "compute_ray", &ret);
// Set the arguments of the kernel
ret = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&output);
if (ret != CL_SUCCESS) {
fputs("error setting CL kernel arg 1\n", stderr);
exit(1);
}
ret = clSetKernelArg(kernel, 1, sizeof(VECTOR3D), &view_point);
if (ret != CL_SUCCESS) {
fputs("error setting CL kernel arg 2\n", stderr);
exit(1);
}
ret = clSetKernelArg(kernel, 2, sizeof(VECTOR3D), &camera_frame.n);
if (ret != CL_SUCCESS) {
fputs("error setting CL kernel arg 3\n", stderr);
exit(1);
}
ret = clSetKernelArg(kernel, 3, sizeof(VECTOR3D), &camera_frame.u);
if (ret != CL_SUCCESS) {
fputs("error setting CL kernel arg 4\n", stderr);
exit(1);
}
ret = clSetKernelArg(kernel, 4, sizeof(VECTOR3D), &camera_frame.v);
if (ret != CL_SUCCESS) {
fputs("error setting CL kernel arg 5\n", stderr);
exit(1);
}
ret = clSetKernelArg(kernel, 5, sizeof(cl_int), &viewport.xvmin);
if (ret != CL_SUCCESS) {
fputs("error setting CL kernel arg 6\n", stderr);
exit(1);
}
ret = clSetKernelArg(kernel, 6, sizeof(cl_int), &viewport.yvmin);
if (ret != CL_SUCCESS) {
fputs("error setting CL kernel arg 7\n", stderr);
exit(1);
}
ret = clSetKernelArg(kernel, 7, sizeof(cl_double), &focal_distance);
if (ret != CL_SUCCESS) {
fputs("error setting CL kernel arg 7\n", stderr);
exit(1);
}
ret = clSetKernelArg(kernel, 8, sizeof(cl_int), &VPWIDTH);
if (ret != CL_SUCCESS) {
fputs("error setting CL kernel arg 9\n", stderr);
exit(1);
}
ret = clSetKernelArg(kernel, 9, sizeof(cl_int), &VPHEIGHT);
if (ret != CL_SUCCESS) {
fputs("error setting CL kernel arg 10\n", stderr);
exit(1);
}
ret = clFinish(command_queue);
// Execute the OpenCL kernel on the list
size_t global_item_size = VPWIDTH * VPHEIGHT; // Process the entire lists
size_t local_item_size = 1024; // Divide work items into groups of 64
ret = clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL,
&global_item_size, &local_item_size, 0, NULL, NULL);
// Read the memory buffer C on the device to the local variable C
ret = clEnqueueReadBuffer(command_queue, output, CL_TRUE, 0,
VPWIDTH * VPHEIGHT * sizeof(PIXEL), pPixelsFromGPU, 0, NULL, NULL);
// Display the result to the screen
//for(i = 0; i < LIST_SIZE; i++)
// printf("%d + %d = %d\n", A[i], B[i], C[i]);
// Clean up
// ret = clFlush(command_queue);
// ret = clFinish(command_queue);
// ret = clReleaseKernel(kernel);
// ret = clReleaseProgram(program);
// ret = clReleaseMemObject(b_mem_obj);
// ret = clReleaseCommandQueue(command_queue);
// ret = clReleaseContext(context);
pPixels = (PIXEL*) (pPixelsFromGPU);
// For all pixels:
for (i=0; i<VPWIDTH; i++) {
for (j=0; j<VPHEIGHT; j++) {
//pCurrentPixel = (PIXEL*)(pPixels + VPWIDTH*i + j);
// here I try to get pixel set on GPU, but it does not work
pCurrentPixel = &pPixels[i*VPWIDTH+j];
} //j
} //i
viewport.pPixels = (PIXEL*) (pPixelsFromGPU);
// register callback function to display graphics:
glutDisplayFunc(disp2);
// call Timer():
Timer(0);
// enter tha main loop and process events:
glutMainLoop();
free(pPixelsFromGPU);
return 0;
}
Definitions on host:
#include <CL/cl.h>
#ifndef DEFS_H
#define DEFS_H
#define _BLINNPHONG
//#define _LAMBERT
//#define _NOSHADING
#define NSPHERES 4
#define VIEWPLANE 500
#define WINDOW VIEWPLANE*2
#define FOCALDIST 1000
#define RADIUS 200
//#define _ANIMATE
// typedef enum {false=0, true=1} BOOL;
// typedef enum {down=0, up=1} DIRECTION;
#define CRED 0
#define CGREEN 1
#define CBLUE 2
#define true 1
#define false 0
/* --------------- VECTORS -------------------- */
typedef struct Vector3D{
cl_double x;
cl_double y;
cl_double z;
cl_double dummy1;
} VECTOR3D;
typedef struct ray {
VECTOR3D origin;
VECTOR3D direction;
} RAY;
/* ------------------- PIXEL ------------------ */
typedef struct pixel {
RAY ray;
cl_double rgb[4];
cl_int i;
cl_int j;
cl_int dummy1;
cl_int dummy2;
cl_int dummy3;
cl_int dummy4;
cl_int dummy5;
cl_int dummy6;
} PIXEL;
/* ----------------- VIEWPORT ----------------- */
typedef struct vp {
cl_int xvmin;
cl_int yvmin;
cl_int xvmax;
cl_int yvmax;
PIXEL* pPixels;
} VIEWPORT;
/* ---------------- SPHERE -------------------- */
typedef struct sp_intersection {
cl_double lambda_in;
cl_double lambda_out;
VECTOR3D normal;
VECTOR3D point;
cl_int valid;
} SPHERE_INTERSECTION;
typedef struct sph {
VECTOR3D center;
cl_double radius;
cl_double kd_rgb[3];
cl_double ks_rgb[3];
cl_double ka_rgb[3];
cl_double shininess;
cl_int mirror;
}SPHERE;
/* ------------------- RAY --------------------- */
/* --------------- VECTOR BASIS ---------------- */
typedef struct vb {
VECTOR3D u;
VECTOR3D v;
VECTOR3D n;
} VEC_BASIS;
#endif
In kernel:
typedef struct pixel {
RAY ray;
double rgb[4];
int i;
int j;
int dummy1;
int dummy2;
int dummy3;
int dummy4;
int dummy5;
int dummy6;
} PIXEL;
And kernel:
typedef struct Vector3D {
double x;
double y;
double z;
double dummy1;
} VECTOR3D;
typedef struct ray {
VECTOR3D origin;
VECTOR3D direction;
} RAY;
typedef struct pixel {
RAY ray;
double rgb[4];
int i;
int j;
int dummy1;
int dummy2;
int dummy3;
int dummy4;
int dummy5;
int dummy6;
} PIXEL;
void vec_sub(VECTOR3D *v1, VECTOR3D *v2, VECTOR3D *v3) {
v1->x = v2->x - v3->x;
v1->y = v2->y - v3->y;
v1->z = v2->z - v3->z;
}
void vec_add(VECTOR3D *v1, VECTOR3D *v2, VECTOR3D *v3) {
v1->x = v2->x + v3->x;
v1->y = v2->y + v3->y;
v1->z = v2->z + v3->z;
}
void vec_scale(double scale, VECTOR3D *v1, VECTOR3D *v2) {
v1->x = scale * v2->x;
v1->y = scale * v2->y;
v1->z = scale * v2->z;
}
double dotproduct(VECTOR3D *v1, VECTOR3D *v2) {
return v1->x * v2->x + v1->y * v2->y + v1->z * v2->z;
}
void normalize_vector(VECTOR3D *v) {
double magnitude;
// 1. calculate the magnitude (length):
magnitude = sqrt( dotproduct(v, v) );
// 2. normalize the vector:
v->x = v->x / magnitude;
v->y = v->y / magnitude;
v->z = v->z / magnitude;
}
__kernel void compute_ray(
write_only global PIXEL *output,
VECTOR3D view_point,
VECTOR3D camera_frame_n,
VECTOR3D camera_frame_u,
VECTOR3D camera_frame_v,
const int viewport_xvmin,
const int viewport_yvmin,
const double distance,
const int w, const int h
)
{
float u, v;
VECTOR3D v1, v2, v3, v4, dir;
RAY ray;
PIXEL pixel;
int gi = get_global_id(0);
int i = gi / w;
int j = gi % w;
u = (float)(viewport_xvmin) + (float)(i) + 0.5f;
v = (float)(viewport_yvmin) + (float)(j) + 0.5f;
vec_scale(-distance, &v1, &camera_frame_n);
vec_scale(u, &v2, &camera_frame_u);
vec_scale(v, &v3, &camera_frame_v);
ray.origin.x = 22;
ray.origin.y = 22;
ray.origin.z = 22;
vec_add(&v4, &v1, &v2);
vec_add(&dir, &v4, &v3);
normalize_vector(&dir);
ray.direction.x = 11;
ray.direction.y = 11;
ray.direction.z = 11;
pixel.ray = ray;
pixel.i = 33;
pixel.j = 33;
output[i*w*j] = pixel;
}
I intentionally set i, j, origin and direction structures to fixed number so I could see if numbers are set.
Then I try to get pixel set on GPU in line
pCurrentPixel = &pPixels[i*VPWIDTH+j];
but pCurrentPixel->i for example is 0 instead of 33.
Code compiles with following commands:
gcc -c main.c -o main.o
gcc -c shading.c -o shading.o
gcc -c sphere.c -o sphere.o
gcc -c ray.c -o ray.o
gcc -c vectors.c -o vectors.o
gcc -I/usr/include -L/usr/lib/x86_64-linux-gnu main.o shading.o sphere.o ray.o vectors.o -lGL -lglut -lGLU -lX11 -lm -lrt -lOpenCL -o main
Besides setting the right VPWIDTH and VPHEIGHT output[i*w*j] = pixel; had to be changed to output[i*w+j] = pixel; in C code.
I am relatively new to both Arduino and Processing and I have been working on a code that utilizes serial communication between the two. My Arduino code is reading and printing values from a piezo sensor and then sending the values to Processing, which sketches certain shapes based on the values. The code has worked previously, but for some reason it is no longer working. Everything compiles, but when I run the Processing code the sketch window is empty and remains empty. A few times the "error, disabling serialEvent()" showed up, but I just unplugged my Arduino board, closed out the programs, and restarted everything. The error no longer shows up, but my Processing sketch is still not displaying on the screen. Can someone please let me know what is wrong with my code? I really appreciate the help.
Arduino Code:
int ledPin = 13;
int knockSensor = A0;
byte val = 0;
int statePin = LOW;
int THRESHOLD = 5;
int sensorReading = 0;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
sensorReading = analogRead(knockSensor);
if(sensorReading > 0)
{
Serial.println(sensorReading, DEC);
}
if (sensorReading != 0)
Serial.println(sensorReading);
delay(100);
}
Processing Code:
import processing.serial.*;
Serial port;
int centerX = 550;
int centerY = 400;
float val;
float ellipseX;
float ellipseY;
float ellipseW;
float ellipseH;
float ellipseXX;
float ellipseYY;
float ellipseWW;
float ellipseHH;
float lineX;
float lineY;
float lineXX;
float lineYY;
void setup(){
background(255);
size(1100,800);
frameRate(10);
smooth();
String portname = "/dev/tty.usbmodem1411";
//String portname = Serial.list()[0];
port = new Serial(this, portname, 9600);
println(Serial.list());
//port.bufferUntil('\n');
}
void drawEllipse(float val)
{
if(val > 0 && val < 50)
{
ellipseX = random(540,560);
ellipseY = random(390,410);
ellipseW = val + 10;
ellipseH = val + 10;
stroke(0);
fill(random(255), random(200,255));
}
}
void drawLines(float val)
{
if(val > 50 && val < 70)
{
lineX = random(500, 600);
lineY = random(360, 440);
lineXX = random(500, 600);
lineYY = random(360, 440);
stroke(0);
}
}
void drawEllipse2(float val)
{
if(val > 70 && val < 120)
{
ellipseXX = random(460, 640);
ellipseYY = random(330, 470);
ellipseWW = val + random(20);
ellipseHH = val + 10;
stroke(0);
fill(random(50, 100), random(50, 100), random(50, 100), random(220, 255));
}
}
void serialEvent(Serial port)
{
String inString = port.readStringUntil('\n');
if (inString != null)
{
val = Float.parseFloat(inString);
}
drawEllipse(val);
drawLines(val);
drawEllipse2(val);
println(val);
}
Maybe using Serial.write() will be better. So the code will look like this.
Arduino Code:
int ledPin = 13;
int knockSensor = A0;
byte val = 0;
int statePin = LOW;
int THRESHOLD = 5;
int sensorReading = 0;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
sensorReading = analogRead(knockSensor);
if(sensorReading > 0)
{
Serial.println(sensorReading, DEC);
}
if (sensorReading != 0)
Serial.write(map(sensorReading, 0, 1023, 0, 255));
delay(100);
}
Processing Code:
import processing.serial.*;
Serial port;
int centerX = 550;
int centerY = 400;
float val;
float ellipseX;
float ellipseY;
float ellipseW;
float ellipseH;
float ellipseXX;
float ellipseYY;
float ellipseWW;
float ellipseHH;
float lineX;
float lineY;
float lineXX;
float lineYY;
void setup(){
background(255);
size(1100,800);
frameRate(10);
smooth();
String portname = "/dev/tty.usbmodem1411";
//String portname = Serial.list()[0];
port = new Serial(this, portname, 9600);
println(Serial.list());
//port.bufferUntil('\n');
}
void drawEllipse(float val)
{
if(val > 0 && val < 50)
{
ellipseX = random(540,560);
ellipseY = random(390,410);
ellipseW = val + 10;
ellipseH = val + 10;
stroke(0);
fill(random(255), random(200,255));
}
}
void drawLines(float val)
{
if(val > 50 && val < 70)
{
lineX = random(500, 600);
lineY = random(360, 440);
lineXX = random(500, 600);
lineYY = random(360, 440);
stroke(0);
}
}
void drawEllipse2(float val)
{
if(val > 70 && val < 120)
{
ellipseXX = random(460, 640);
ellipseYY = random(330, 470);
ellipseWW = val + random(20);
ellipseHH = val + 10;
stroke(0);
fill(random(50, 100), random(50, 100), random(50, 100), random(220, 255));
}
}
void serialEvent(Serial port)
{
if (0 < port.available()) {
val = map(port.read(), 0, 255, 0, 1023);
}
if (val > 0) {
drawEllipse(val);
drawLines(val);
drawEllipse2(val);
println(val);
}
}
I've been working on an LED wall and came into a RAM issue. Basically I am using teensy 3.0 and trying to load the following script, however, the script errors out with .bss will not fit into region 'RAM'
Please help! Any information would be greatly appreciated! Thanks!
/*
Nike NFL draft LED wall program
OctoWS2811 BasicTest.ino - Basic RGB LED Test
http://www.pjrc.com/teensy/td_libs_OctoWS2811.html
Copyright (c) 2013 Paul Stoffregen, PJRC.COM, LLC
*/
#include <OctoWS2811.h>
const int ledsPerStrip = 290;
DMAMEM int displayMemory[ledsPerStrip*6];
int drawingMemory[ledsPerStrip*6];
const int config = WS2811_GRB | WS2811_800kHz;
OctoWS2811 leds(ledsPerStrip, displayMemory, drawingMemory, config);
#define ORANGE 0xE05800
#define WHITE 0xFFFFFF
#define BLACK 0x000000
#define BLACK2 0x1E1E1E
void setup() {
leds.begin();
leds.show();
}
static int widths[] = { 30, 30, 50, 90, 40, 60 };
static int speeds[] = { 5, 5, 10, 16, 11, 13 };
static int locations[] = { 0, 0, 0, 0, 0, 0 };
static int counter = 0;
//static int location = 0;
static boolean reverse = false;
int blend(int source , float alpha) {
int source_r = (source >> 16);
int source_g = ((source >> 8) & 0x00FF);
int source_b = (source & 0x0000FF);
source_r = source_r * alpha;
source_g = source_g * alpha;
source_b = source_b * alpha;
return source_b | (source_g << 8) | (source_r << 16);
}
void loop() {
int microsec = 2000000 / leds.numPixels(); // change them all in 2 seconds
int location;
int offset;
int width;
int current;
int min;
int color;
float alpha = 0.95; // Set brightness of head
int head_width = 3; // Set width of head
delay(20);
int i;
for(i = 0; i < 6; ++i) {
location = locations[i];
width = widths[i];
color = 0xFFFFFF;
offset = i*ledsPerStrip;
location = location + speeds[i];
if(location > ledsPerStrip + width) {
location = 0;
}
locations[i] = location;
if(location < width) {
current = location;
min = 0;
} else if(location >= width) {
current = location;
min = location - width + 1;
}
for(current; current >= 0; --current) {
if(current >= min) {
if(current < ledsPerStrip) {
if(!reverse) {
leds.setPixel(current + offset, color);
} else {
leds.setPixel((ledsPerStrip - current) + offset, color);
}
}
if(current < (location - head_width)) {
color = blend(color, alpha);
}
} else {
if(!reverse) {
leds.setPixel(current + offset, BLACK);
} else {
leds.setPixel((ledsPerStrip - current) + offset, BLACK);
}
}
}
}
leds.show();
counter++;
}
And the returned error:
This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.
Arduino: 1.0.5 (Windows 7), Board: "Teensy 3.0"
c:/program files/arduino/hardware/tools/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/4.7.2/../../../../arm-none-eabi/bin/ld.exe: Nike_NFL_Program.cpp.elf section .bss' will not fit in regionRAM'
c:/program files/arduino/hardware/tools/arm-none-eabi/bin/../lib/gcc/arm-none-eabi/4.7.2/../../../../arm-none-eabi/bin/ld.exe: region `RAM' overflowed by 1028 bytes
collect2.exe: error: ld returned 1 exit status
Thanks!
Your simply running out of memory. sizing it down from 6 to 5 compiles. Note that the 3.0 has 16384 of SRAM. Each multiple of consumes a large chunk of the limited 16384 of SRAM.
I think you should put the code of static or variable to DRAM.
Such as the code you supply.
From:
const int ledsPerStrip = 290;
DMAMEM int displayMemory[ledsPerStrip*6];
int drawingMemory[ledsPerStrip*6];
const int config = WS2811_GRB | WS2811_800kHz;
static int widths[] = { 30, 30, 50, 90, 40, 60 };
static int speeds[] = { 5, 5, 10, 16, 11, 13 };
static int locations[] = { 0, 0, 0, 0, 0, 0 };
static int counter = 0;
To:
#include "link_defs.h"
__SECTION(dram.rodata) const int ledsPerStrip = 290;
__SECTION(dram.bss) DMAMEM int displayMemory[ledsPerStrip*6];
__SECTION(dram.bss) int drawingMemory[ledsPerStrip*6];
__SECTION(dram.rodata) const int config = WS2811_GRB | WS2811_800kHz;
__SECTION(dram.data) static int widths[] = { 30, 30, 50, 90, 40, 60 };
__SECTION(dram.data) static int speeds[] = { 5, 5, 10, 16, 11, 13 };
__SECTION(dram.data) static int locations[] = { 0, 0, 0, 0, 0, 0 };
__SECTION(dram.data) static int counter = 0;