Delay in CodeWarrior - c++

I am new to trying to write anything. While I can read what's happening most of the time, I have no idea how to build a delay. In Arduino I have used delays but it doesn't seem to work the same here.
I have been searching the internet trying to find something that will work, but with no luck. I think I could make something work but I don't know how to add more '#includes' either. Currently I have-
#include <xbee_config.h>
#include <types.h>
#include <utils.h>
#include <xbee/atcmd.h>
I have the general idea of what is needed but now idea how to write it. I'm turning on an LED that I need delayed before turning off.
gpio_set(LED1, 1); //Turn on LED
**Delay here!!!!**
gpio_set(LED1, 0); //Turn off LED
My first thought is building a void_delay function that will increment a counter until x time is reached, then return to the program. I know that's not the best way as it will be keeping the program from other tasks while counting, but it should work for my purpose. The problem, I have no idea how to write that.

In c++ you can use Sleep(milliseconds) you only have to include <windows.h>.
Example:
#include <iostream>
#include <windows.h>
using namespace std;
int main() {
cout << "Before delay" <<endl;
Sleep(5000);
cout << "After delay" <<endl;
return 0;
}

Related

Clear command is clearing too early C++

I have a small problem with the clear command in C++. The Clear function is supposed to clear everything at the end of the program but it clears too early. Does anyone know how to solve that problem and let the program clear it as the complete end? I want that at the end the console is empty.
#include <iostream>
#include <chrono>
#include <thread> // sleep_for, sleep_until
#include <ctime>
#include <thread>
using namespace std;
using namespace std::this_thread; // sleep_for, sleep_until
using namespace std::chrono;
int main() {
cout<<"\nTest message?";
sleep_for(nanoseconds(1500000000));
system("clear");
cout<<"\nThis message should be cleared";
sleep_for(nanoseconds(5000000000));
system("clear");
}
Console output:
This message should be clearedîș§
standard cout in c++ is buffered.
this means that the text is held before it is sent to the screen, this helps with performance.
this is causing the clear to be called before the buffer is sent to the screen.
to fix this you need to manually flush the buffer by either putting << std::flush or << std::endl at the end of the cout statement

getline(ifstream, string) on Mac causes EXC_BAD_ACCESS

I've been a C++ developer since it arrived. All was on windows, and I haven't touched it in about 6 years.
Now I'm trying to get an old code-base working using VS Code on my Mac. I'm using clang++ with c++17.
This problem is vexing; I've seen many other posts with the same issue, but the problem always seemed to be something in the code.
Note: this code worked fine with C++11 on Windows.
To simplify, I copied the code to execute right at the top of main. Here is is:
ifstream file("assets/textures/blocks.txt", ios::in);
if( file.is_open() ) {
string s;
getline(file, s); // <-- This line causes the error.
cout << s << endl;
}
As this code worked elsewhere, I assume I've got a setup or environment problem and am looking for hints towards what to check on.
Thank you for any help!
An update:
Thank you. I paired the program down and tried a few things. Here's the deal:
If I leave all my files to be compiled, but replace main.cpp with the below code, the cout line generates the same exception.
If I cull all the unused files, the code works.
Something in some other file is somehow breaking the stream code. I'm clueless.
#include <iostream>
#include <istream>
using namespace std;
int main() // int argc, char** argv)
{
cout << "Hello World" << endl;
}
I should add: this is a GLFW 3D game engine app. It does not subclass or interact with any stream in any way other than the most basic file read/write operations.

can't seem to link GLFW library

I'm attempting to make my own game engine and using this video tutorial to do so: https://www.youtube.com/watch?v=Pid8JGlBdPY&t=1122s&ab_channel=TheCherno
I'm using library called GLFW. I linked everything as its shown in the video triple checked everything but every time I use function from the library I get errors.
this is all the code I've written
#include <GLFW/glfw3.h>
#include <iostream>
using namespace std;
int main()
{
if (!glfwInit()) {
cout << "NOPE" << endl;
}
return 0;
}
I'm guessing I've linked something wrong even though I can't find what. but if that's not the issue here and im missing something, please let me know. I don't really know what those errors mean.

Delay in Dev C++

I have come across to a problem while coding in C++ on Dev C++ compiler. I want to delay my statement to some milliseconds, but the problem is dev doesnt support the dos.h header file and so its contents as well. I had an alternative way for using it with the help of for loop but i aint got its proper syntax in my mind to use it properly. I wish, you folks, might be able to resolve the problem for me. Thanks in Advance..
#include <dos.h>
int main(){
delay(1000)
cout << "Hello"
return 0;
Tell me another alternative way for this please.
C++ has < thread >
< thread > has "std::this_thread::sleep_for(...)"
Example illustrating the (...)
std::this_thread::sleep_for(100ms);
The ms of 100 ms comes from
using namespace std::chrono_literals; // support suffixes like 100ms, 2s, 30 us
Probably you also need to include < chrono >
Use the header chrono and thread
#include <iostream>
#include <thread>
#include <chrono>
using namespace std::this_thread;
using namespace std::chrono;
sleep_for(nanoseconds(10));
sleep_until(system_clock::now() + seconds(1));
You can change the nanoseconds part to seconds or keep it at nanoseconds, and the number beside it is the amount of that unit that you can change to any number.
Use empty for loop
for example:
for(int i=0; i<10000; i++);
Change upper bound according to your need.

C++ keypress: getch, cin.get?

I have a Win32 program that runs on a loop. I would like to be able to pause that program while awaiting a keypress. It doesn't matter whether I use 'any key' or a specific key, but I need to have the program freeze until I press something.
I am wondering which command I should use. I am working with Visual C++ and the compiler doesn't recognise any of the following commands:
cin.get()
std::cin.get()
getch()
I am relatively new to C++. I understand that in a console app this is a fairly simple action to take (cin.get), but that it can be more difficult in Win32. Any simple solution or workaround would be appreciated. The program is bespoke to be used in a single scientific experiment, so for now I'm not fussed if the solution is a little botchy(!)
Apologies if I've missed any important info from my question.
You should use neither.
You should use
#include <iostream>
...
int main()
{
...
std::cin.ignore(); //why read something if you need to ignore it? :)
}'
Here's the documentation
Example:
#include <iostream>
#include <conio.h>
int main()
{
std::cout << "Press any key to continue . . ." << std::endl;
_getch(); // wait for keypress
}
_getch() is C++ equivalent to C getch()
Try
#include <iostream>
using namespace std;
char temp;
cin >> temp;
Assuming that you are looking for an alternative for getch ( which does not echo to screen).
If you are using windows and visual studio to be precise try using _getch.
Here is a link to it
http://msdn.microsoft.com/en-us/library/078sfkak(v=VS.100).aspx
You should #include <iostream> and use std::cin.get();
I think the getch() is a C function, but since you are using C++, then the cin would be more appropriate.
HWND hwnd = ::GetConsoleWindow();
while (!((::GetForegroundWindow() == hwnd) &&
((::GetKeyState(VK_SPACE) & 0x8000) != 0)))
::Sleep(0);
Suppose it is not the best way but it solved my problem.
Replace VK_SPACE with any other value you like.
And it is not portable.