Warning: I'm new to C++ (and most of programming in general) and am learning the language to be able to write code for my arduino
I'm learning how to use a shift register using the Arduino IDE which i believe is in C++.
Here is the code:
const int latchPin = 12; // connected to ST_CP of 74HC595
const int clockPin = 8; // connected to SH_CP of 74HC595
const int dataPin = 11; // connected to DS of 74HC595
// display 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, b, C, d, E, F
int datArray[16] = (252, 96, 218, 242, 102, 182, 190, 224, 254, 246, 238, 62, 156, 122, 158, 142);
void setup()
{
//set pins to output
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
void loop()
{
//loop from 0 to 256
for(int num = 0; num < 16; num++)
{
digitalWrite(latchPin, LOW); //ground latchPin and hold low for as long as you are transmitting
shiftOut(dataPin, clockPin, MSBFIRST, datArray[num]); //MSBFIRST means most significant bit first
// return the latch pin high to signal chip that it no longer needs to listen for information
digitalWrite=(latchPin, HIGH); // pull the latchpin to save the data
delay(1000); // wait for a second
}
}
and here is the error which I do not understand what it means and what I should do about it
shift_register:5:92: error: array must be initialized with a brace-enclosed initializer
int datArray[16] = (252, 96, 218, 242, 102, 182, 190, 224, 254, 246, 238, 62, 156, 122, 158, 142);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
C:\Users\HP\Desktop\VSc Code\Arduino\shift_register\shift_register.ino: In function 'void loop()':
shift_register:22:33: error: assignment of function 'void digitalWrite(uint8_t, uint8_t)'
digitalWrite=(latchPin, HIGH); // pull the latchpin to save the data
^
shift_register:22:33: error: cannot convert 'int' to 'void(uint8_t, uint8_t) {aka void(unsigned char, unsigned char)}' in assignment
exit status 1
array must be initialized with a brace-enclosed initializer
This line is the problem:
int datArray[16] = (252, 96, 218, 242, 102, 182, 190, 224, 254, 246, 238, 62, 156, 122, 158, 142);
Change it to:
int datArray[16] = {252, 96, 218, 242, 102, 182, 190, 224, 254, 246, 238, 62, 156, 122, 158, 142};
Related
I am trying to output a song using the Piezoelectric buzzer on my Arduino. Running the code without RTOS functionalities works well, however, when i place the code into an RTOS task, the buzzer does not work. I am buzzled by this.
Running the code without RTOS Task function works but when implemented with RTOS, it doesn't work. It seems like the tone() function does not execute.
#include <Arduino.h>
#include <avr/io.h>
#include <FreeRTOS.h>
#include <task.h>
//Tones
#define d 294 // 294 Hz
#define e 329 // 329 Hz
#define fSharp 370 // 369.994 Hz
#define g 392 // 392 Hz
// Define a special note, 'R', to represent a rest
#define R 000
#define babySharkOutput 11
int babyShark[] = { //Note of the song, 0 is a rest/pulse
d, e, g, g, g, g, g, g,
d, e, g, g, g, g, g, g,
d, e, g, g, g, g, g, g,
g, g, fSharp, R
};
int babySharkDuration[] = {
400, 200, 50, 50, 75, 50, 50, 75,
400, 200, 50, 50, 75, 50, 50, 75,
400, 200, 50, 50, 75, 50, 50, 75,
75, 75, 250, 1000
};
int babySharkNotePause[] = {
50, 60, 25, 20, 25, 15, 25, 40,
50, 60, 25, 20, 25, 15, 25, 40,
50, 60, 25, 20, 25, 15, 25, 40,
30, 20, 50, 200
};
void songTask(void *p) {
TickType_t xLastWakeTime = 0 ;
const TickType_t xPeriod = pdMS_TO_TICKS(1000);
while(1) {
for (int i = 0; i < 28; i ++){
tone(babySharkOutput, babyShark[i], babySharkDuration[i]);
int songDelay = babySharkNotePause[i] * songSpeed;
vTaskDelay(songDelay);
}
vTaskDelayUntil( &xLastWakeTime, xPeriod);
}
}
void setup() {
pinMode(babySharkOutput, OUTPUT);
}
void loop() {
xTaskCreate(songTask, "songTask", STACK_SIZE, NULL, 1, NULL);
vTaskStartScheduler();
}
Hmm I expected this would run well because it can be run without the RTOS features, but there's something wrong here and I can't figure it out.
pyhon matplot misses bars in bar graph. I expect it to generate bar at [0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750, 800, 850, 900, 950, 1000] but it shows only at [250, 350, 450, 550, 650, 750]
import matplotlib.pyplot as plt
nsamples = 1000 + 1
vpp = 25
timestamps = [t for t in range(0, nsamples, vpp)]
processings = [0 for t in range(0, nsamples, vpp)]
transmissions = [0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 650, 700, 750, 800, 850, 900, 950, 1000]
for transmission in transmissions:
processings[transmission / vpp] = 1
plt.bar(timestamps, processings)
plt.show()
OK. I got it working somehow and i don't know details but when i reduced the sample size by using range(0, nsamples/vpp, 1) instead of range(0, nsamples, vpp) then graph was plotted as expected (even though number of elements to be plotted against on x axis was still same.) it seems plt.bar assumes unit step between axis values in argument list instead of just taking the list itself and plotting it.
This question already has answers here:
How do multidimensional arrays in C++ function? [duplicate]
(4 answers)
Closed 4 years ago.
I need to use 2 dimension array in my game so;
First I'M declaring it like this in my header file called "HelloWorldScene.h":
using namespace cocos2d;
class HelloWorld : public Layer
{
private:
...
int upgradeCosts[4][4];
...
}
Then I'm tryin to define it in my source file called "HelloWorldScene.cpp":
#include "HelloWorldScene.h"
...
bool HelloWorld::init()
{
....
upgradeCosts[4][4] = {{150, 250, 900, 1800},
{100, 200, 600, 1200},
{200, 350, 1200, 2000},
{150, 300, 1000, 1900}};
...
}
But it gives me this error: "Excess elements in scalar initializer"
The entire code between brackets {{150, ...... , 1900 }} are underlined with red. And i dont know what to do, please help me.
This is absolutely wrong to initialize the whole array like this :
upgradeCosts[4][4] = {{150, 250, 900, 1800},
{100, 200, 600, 1200},
{200, 350, 1200, 2000},
{150, 300, 1000, 1900}};
Because you just set the [4][4] element, not the whole array. This what the Excess elements in scalar initializer error refers to. An int is a scalar, it can only be initialised with 1 value, yet you attempt to initialise it with 16 values.
To correct this you would assume you could do this:
bool HelloWorld::init()
{
upgradeCosts = {{150, 250, 900, 1800},
{100, 200, 600, 1200},
{200, 350, 1200, 2000},
{150, 300, 1000, 1900}};
}
However this will not work because upgradeCosts has already been default initialised during the construction of HelloWorld and cannot be initialised again.
A naive solution to this would be like the following:
bool HelloWorld::init()
{
int data[4][4] = {{150, 250, 900, 1800},
{100, 200, 600, 1200},
{200, 350, 1200, 2000},
{150, 300, 1000, 1900}};
for(int i = 0;i < 3;i++)
for(int j = 0;j < 3;j++)
upgradeCosts[i][j] = data[i][j];
...
}
However, this involves pointless copying. A better solution would be to move the array initialisation out of the init method altogether. You can do this by either just initialising the array in the header file:
// hpp file
class HelloWorld
{
private:
int upgradeCosts[4][4] = {{150, 250, 900, 1800},
{100, 200, 600, 1200},
{200, 350, 1200, 2000},
{150, 300, 1000, 1900}};
public:
bool init();
};
Or you could initialise it in the constructor of HelloWorld:
// hpp file
class HelloWorld
{
private:
int upgradeCosts[4][4];
public:
HelloWorld();
bool init();
};
// cpp file
HelloWorld::HelloWorld()
: upgradeCosts{{150, 250, 900, 1800},
{100, 200, 600, 1200},
{200, 350, 1200, 2000},
{150, 300, 1000, 1900}}
{ }
Or if you know that your upgradeCosts should be the same across all instances of HelloWorld you can use a static member and initialise it like below :
// hpp file
class HelloWorld
{
private:
const static int upgradeCosts[4][4];
public:
bool init();
};
// cpp file
const int HelloWorld::upgradeCosts[4][4] = {{150, 250, 900, 1800},
{100, 200, 600, 1200},
{200, 350, 1200, 2000},
{150, 300, 1000, 1900}};
Here is my code. It is supposed to draw a bar chart every time it loops in main. It correctly displays the bar chart initially, but when looped, it seems like the line function does not work anymore....can anyone help me with this?
graphics library available here: http://www.mediafire.com/file/qtdy239vvr19qyi/CODE+BLOCKS+with+Graphic.h.rar
Note: I am using codeblocks 17.12
#include <graphics.h>
#include <conio.h>
#include <fstream>
#include <iostream>
using namespace std;
void graph(string f){
cleardevice();
int gd = DETECT, gm;
initgraph(&gd, &gm, "X:\\TC\\BGI");
settextstyle(BOLD_FONT,HORIZ_DIR,2);
outtextxy(275,0, "BAR CHART");
setlinestyle(SOLID_LINE,0,2);
/* Draw X and Y Axis */
line(90,410,90,50); //Y axis
line(90,410,1100,410); //X axis
line(85,60,90,50); //Arrow code last 4 line
line(95,60,90,50);
line(1095,405,1100,410);
line(1095,415,1100,410);
line(84,360 ,95, 360);
line (84, 310 ,95,310);
line (84, 260, 95, 260);
line(84, 210, 95, 210);
line(84, 160, 95, 160);
line (84, 110, 95, 110);
outtextxy(34, 350, "50");
outtextxy(34, 300, "100");
outtextxy(34, 250, "150");
outtextxy(34, 200, "200");
outtextxy(34, 150, "250");
outtextxy(34, 100, "300");
outtextxy(150, 420, "Jan");
outtextxy(225, 420, "Feb");
outtextxy(300, 420, "Mar");
outtextxy(375, 420, "April");
outtextxy(450, 420, "May");
outtextxy(525, 420, "June");
outtextxy(600, 420, "Jul");
outtextxy(675, 420, "Aug");
outtextxy(750, 420, "Sept");
outtextxy(825, 420, "Oct");
outtextxy(900, 420, "Nov");
outtextxy(975, 420, "Dec");
outtextxy(65,60,"Y");
outtextxy(1080,415,"X");
outtextxy(70,415,"O");
/* Draw bars on screen */
//setfillstyle(XHATCH_FILL, RED);
bar(150,110,200,410);
bar(225,160,275,410);
bar(300,120,350,410);
bar(375,170,425,410);
bar(450,135,500,410);
getch();
closegraph();
//cleardevice();
}
int main(){
int choice = 1;
do{
graph();
cout<<"Key in zero to loop:..";
cin>>choice;
}while (choice == 0);
return 0;
}
I would like to draw 8-bit grayscale image data to a wxPaintDC, but I have been having trouble. The data is made available to me in a uint8_t array. See the non-working code below (this is one of several attempts). I am able to draw using other wxPaintDC::Draw* functions, so I believe that everything else is set up fine.
uint8_t testBuffer[] =
{
250, 220, 222, 223, 210,
186, 22, 29, 89, 110,
250, 220, 222, 203, 210,
240, 120, 220, 123, 210,
230, 210, 252, 223, 210
};
wxBitmap cameraImage(5, 5, 8);
wxNativePixelData cameraImageData(cameraImage);
wxNativePixelData::Iterator p(cameraImageData);
for(unsigned y = 0; y < 5; ++y)
{
wxNativePixelData::Iterator rowStart = p;
for(unsigned x = 0; x < 5; ++x, ++p)
{
uint8_t value = testBuffer[5 * y + x];
p.Red() = value;
p.Green() = value;
p.Blue() = value;
}
p = rowStart;
p.OffsetY(cameraImageData, 1);
}
// _dialogDrawingCanvas is a wxPaintDC*
_dialogDrawingCanvas->DrawBitmap(cameraImage, 50, 50);
EDIT
I looked at wxImage based on the suggestion made by Thomas Matthews. Using wxImage works when I use testBuffer, but not when I use the actual buffer.
This works:
uint8_t testBuffer[] =
{
250, 220, 222, 223, 210,
186, 22, 29, 89, 110,
250, 220, 222, 203, 210,
240, 120, 220, 123, 210,
230, 210, 252, 223, 210
};
wxImage image(5, 5, testBuffer, true);
wxBitmap bmp(image);
_dialogDrawingCanvas->DrawBitmap(bmp, 50, 50);
However, the code below does not. I get a crash in the wxBitmap ctor.
// Returns instance of a class that provides access to camera image.
CameraImage camImage = grabCameraImage();
uint8_t* buf = camImage.getBuffer();
wxImage image(camImage.getWidth(), camImage.getHeight(), buf, true);
wxBitmap bmp(image); // Crashes here.
_dialogDrawingCanvas->DrawBitmap(bmp, 50, 50);
The crash I get is:
First-chance exception at 0x000007fdcf824b5b in [redacted].exe: 0xC0000005: Access violation reading location 0x000000000304d27a.
Unhandled exception at 0x000007fdcf824b5b in [redacted].exe: 0xC000041D: An unhandled exception was encountered during a user callback.
EDIT 2
I linked the wxWidgets DLL to its source code and traced into the call to the wxBitmap ctor. It looks like it's expecting at least 24BPP and starts reading at the end of the wxImage buffer and goes backwards so it's crashing as soon as it tries to access what it thinks is the last 3 bytes of the buffer. It looks like the depth argument to the wxBitmap constructor is ignored. I think maybe this wasn't an issue with testBuffer because it was a fixed size array.
This is basically answered in my edits, but the problem was caused by using 8 BPP data in the buffer. I was able to accomplish what I wanted to do as follows:
CameraImage camImage = grabCameraImage();
wxImage image(camImage.getWidth(), camImage.getHeight());
const uint8_t* cameraBuffer = camImage.getBuffer();
for(uint16_t y = 0; y < camImage.getHeight(); ++y)
{
for(uint16_t x = 0; x < camImage.getWidth(); ++x)
{
uint8_t intensity = cameraBuffer[y * camImage.getWidth() + x];
image.SetRGB(x, y, intensity, intensity, intensity);
}
}
wxBitmap bmp(image, 8);
_dialogDrawingCanvas->DrawBitmap(bmp, 50, 50);