My project doesnt' work in mikroC - mikroc

This is my project for reverse parking...this code is working without IF statements. When I put IF statements in code my hardware don't respond. (IF statements in the void main...)
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Thanks for help!
sbit LCD_RS at RD0_bit;
sbit LCD_EN at RD1_bit;
sbit LCD_D4 at RD2_bit;
sbit LCD_D5 at RD3_bit;
sbit LCD_D6 at RD4_bit;
sbit LCD_D7 at RD5_bit;
sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISD1_bit;
sbit LCD_D4_Direction at TRISD2_bit;
sbit LCD_D5_Direction at TRISD3_bit;
sbit LCD_D6_Direction at TRISD4_bit;
sbit LCD_D7_Direction at TRISD5_bit;
//inicializacija LCD modula
char message1[] = "Distance:";
char *dis = "000.0";
unsigned int dist;
float temp_value;
float x;
float y;
void Display_Distance() {
if (dist/10000)
dis[0] = dist/10000 + 48; //stotice
else
dis[0] = ' ';
dis[1] = (dist/1000)%10 + 48; // desetica
dis[2] = (dist/100)%10 + 48; // enica
dis[4] = (dist/10)%10 + 48; // decimalka
Lcd_Out(2, 1, dis); // izpis distance na LCD
}
void main() {
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,message1);
Lcd_Chr(2,7,'C');
Lcd_Chr(2,8,'m');
do {
x = (float)ADC_Read(2); //vrednost iz analognega kanala, kamor je senzor priklopljen
y = (((x*5)/1023)); //pretvorba v volte
if(y>0){
temp_value = (((log10(y))/(log10(0.98)))+62.6); //funkcija grafa
if(y<=0.8 && y>0.65)
temp_value = temp_value+10;
else if(y<=0.65 && y>0.6)
temp_value = temp_value+17.5;
else if(y<=0.6 && y>0.55)
temp_value = temp_value+13;
else if(y<=0.55 && y>0.5)
temp_value = temp_value+3.5;
else if(y>=2 && y<2.5)
temp_value = temp_value+1.3;
else if(y>=2.5 && y<2.8)
temp_value = temp_value+4.5;
temp_value=temp_value*10000;
dist = temp_value/1000;
dist = dist*10;
Display_Distance();
Delay_ms(1000);
}
} while(1);
}

Had to deal with this on a project of mine where the if statement would not branch correctly when using if without braces. The solution was using curly braces after if/else statements even if there was only one statement after it. It is a bug on the compiler.
if(y<=0.8 && y>0.65)
temp_value = temp_value+10;
else if(y<=0.65 && y>0.6)
temp_value = temp_value+17.5;
becomes
if(y<=0.8 && y>0.65){
temp_value = temp_value+10;
}
else if(y<=0.65 && y>0.6){
temp_value = temp_value+17.5;
}
If the program simply crashes, check the generated HEX/ASM.

Related

how to solve else/if statement issue?

#include <Wire.h>
#include "DFRobot_LCD.h"
DFRobot_LCD lcd(16, 2); //16 characters and 2 lines of show
#define PH_PIN 25
float voltage,phvalue,temperature = 25;
float acidVoltage = (1990); //buffer solution at 4.o
float neutralVoltage = (1389); //buffer solution at 7.o
const int Sensor_MINvalue1 = 3.8; // +-0.5 from ph 4.0
const int Sensor_MAXvalue1 = 4.1;
const int Sensor_MINvalue2 = 6.8; // +-0.5 from ph 7.0
const int Sensor_MAXvalue2 = 7.1;
const int Sensor_MINvalue3 = 9.8; // +-0.5 from ph 10.0
const int Sensor_MAXvalue3 = 10.1;
void breath(unsigned char color){
for(int i=0; i<255; i++){
lcd.setPWM(color, i);
delay(5);
}
delay(500);
for(int i=254; i>=0; i--){
lcd.setPWM(color, i);
delay(1000);
}
delay (300);
}
void setup()
{
Serial.begin(11520);
// LCD Begin //
lcd.init();
lcd.setCursor(4,0);
lcd.print("Welcome");
delay(3000);
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Reading PH");
delay(5000);
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Please Wait");
delay(5000);
lcd.clear();
}
void loop()
{
static unsigned long timepoint = millis();
if(millis()-timepoint>1000U){
timepoint = millis();
// timepoint = read temperature
voltage = analogRead (PH_PIN)/4095.0*3300;
float slope = (7.0-4.0)/((neutralVoltage-1500)/30 - (acidVoltage-1500)/3.0);
float intercept = 7.0 - slope*(neutralVoltage-1500)/3.0;
phvalue = slope*(voltage-1500)/3.0 + intercept; // y=k*x + b [formula]
if( phvalue < Sensor_MAXvalue1 && phvalue > Sensor_MINvalue1 ){ //if( phvalue < 4.1 && phvalue > 3.8 )
Serial.print("Voltage:");
Serial.print(voltage,1);
Serial.print("PH:");
Serial.println(phvalue,2);
//LCD setting 16x2
lcd.setCursor(0,0);
lcd.print("PH : ");
lcd.print(phvalue,2);
lcd.setCursor(0,1);
lcd.print("Volt : ");
lcd.print(voltage/1000,2);
if( phvalue < Sensor_MINvalue1 && phvalue > Sensor_MAXvalue1 ){
lcd.setCursor(3,0);
lcd.print("Dosing is required");
delay(5000);
lcd.clear();
}
}
else if (phvalue < Sensor_MAXvalue2 && phvalue > Sensor_MINvalue2){ //if( phvalue < 7.1 && phvalue > 6.8 )
Serial.print("Voltage:");
Serial.print(voltage,1);
Serial.print("PH:");
Serial.println(phvalue,2);
//LCD setting 16x2
lcd.setCursor(0,0);
lcd.print("PH : ");
lcd.print(phvalue,2);
lcd.setCursor(0,1);
lcd.print("Volt : ");
lcd.print(voltage/1000,2);
if( phvalue < Sensor_MINvalue2 && phvalue > Sensor_MAXvalue2 ){
lcd.setCursor(3,0);
lcd.print("Dosing is required");
delay(5000);
lcd.clear();
}
}
else (phvalue < Sensor_MAXvalue3 && phvalue > Sensor_MINvalue3); { //if( phvalue < 10.1 && phvalue > 9.8 )
Serial.print("Voltage:");
Serial.print(voltage,1);
Serial.print("PH:");
Serial.println(phvalue,2);
//LCD setting 16x2
lcd.setCursor(0,0);
lcd.print("PH : ");
lcd.print(phvalue,2);
lcd.setCursor(0,1);
lcd.print("Volt : ");
lcd.print(voltage/1000,2);
if( phvalue < Sensor_MINvalue3 && phvalue > Sensor_MAXvalue3 ){
lcd.setCursor(3,0);
lcd.print("Dosing is required");
delay(5000);
lcd.clear();
}
}
}
}
that is my code, after applying the code it is not working, I am trying so that it reads a range of values so that it reads the ph value and show me the values for example if the ph = 6.8-7.1 it will show the ph, if the value is below or above it will print 'dosing is required but now it is not showing it. i am unsure if i should create a main so that i can seperate the if, else if, esle statemant
if( phvalue < Sensor_MINvalue1 && phvalue > Sensor_MAXvalue1 )
How can phvalue be smaller than your min and larger than your max at the same time?
You should use logical or || here.
Same for
if( phvalue < Sensor_MINvalue2 && phvalue > Sensor_MAXvalue2 )
and
if( phvalue < Sensor_MINvalue3 && phvalue > Sensor_MAXvalue3 )

How can I get out of the loop?

I'd like to get out of the loop in my Arduino project. Currently I am programming a digital watch, everything works just fine but I wanted to add options menu by clicking a button, but after clicking it nothing pops up even if I have something inside the code. Take a look at it. I don't know how to write it better. If you have some ideas, please you could rewrite some parts of the code and explain why you did so. Thanks forwardly.
Please do not mind s = s + 1, I wanted it like that.
#include "LiquidCrystal.h"
#include <EEPROM.h>
LiquidCrystal lcd(12,11,5,4,3,2);
int h = 0;
int m = 0;
int s = 0;
int right = 8;
int left = 9;
int buttonStateLeft = 0;
String when;
uint8_t EEPROMaddress_sec = 1;
uint8_t EEPROMaddress_min = 2;
uint8_t EEPROMaddress_hour = 3;
bool clockShown = true;
bool menuShown = false;
void setup()
{
lcd.begin(16,2);
pinMode(right, INPUT);
pinMode(left, INPUT);
}
void loop()
{
if(menuShown)
{
lcd.setCursor(0,0);
lcd.print("jozo je kkt");
delay(200);
}
if(clockShown) {
lcd.setCursor(0,0);
buttonStateLeft = digitalRead(left);
if(buttonStateLeft == HIGH)
{
clockShown = false;
menuShown = true;
lcd.clear();
}
s = EEPROM.read(EEPROMaddress_sec);
m = EEPROM.read(EEPROMaddress_min);
h = EEPROM.read(EEPROMaddress_hour);
s = s + 1;
if(h > 12)
when = "PM";
if(h < 12)
when = "AM";
if(h == 12)
when = "PM";
lcd.print("Cas: ");
if(h<10)lcd.print("0");
lcd.print(h);
lcd.print(":");
if(m<10)lcd.print("0");
lcd.print(m);
lcd.print(":");
if(s<10)lcd.print("0");
lcd.print(s);
lcd.print(" ");
lcd.print(when);
if(s == 60)
{
s = 0;
m = m+1;
}
if(m == 60)
{
s = 0;
m = 0;
h = h+1;
}
if(h == 24)
{
m = 0;
s = 0;
h = 0;
}
EEPROM.write(EEPROMaddress_sec, s);
EEPROM.write(EEPROMaddress_min, m);
EEPROM.write(EEPROMaddress_hour, h);
delay(1000);
}
}
In order to do that you will have to use Interrupts, note that you must connect your button to an interrupt pin (not every pin is an interrupt pin) you can google "what are the interrupt pins of 'your_card_name' ", the code would have to change, you can follow these :
In the setup function replace :
pinMode(left, INPUT);
by :
attachInterrupt(digitalPinToInterrupt(left), switchMode, RISING);
add this function before setup(){...}
int lastPressTime=millis();
void switchMode(){ // function called when the button is pressed
if((millis()-lastPressTime)>60){ // for debouncing
clockShown = false;
menuShown = true;
lcd.clear();
lastPressTime=millis();
}
}
and remove this part from your code : (the one in the loop() function)
buttonStateLeft = digitalRead(left);
if(buttonStateLeft == HIGH)
{
clockShown = false;
menuShown = true;
lcd.clear();
}

Vulkan swapchain with 0 extent

I've stumbled upon a strange behavior inside my Vulkan application (based on Qt, but it shouldn't matter).
When my Vulkan window gets resized I need to recreate the swapchain. I use vkGetPhysicalDeviceSurfaceCapabilitiesKHR() to get new information about the window surface (which is not being recreated, which might or might not be a problem) and receive the new surface extents: currentExtent = { 46, 0 } with minImageExtent = { 1, 1 }. In other words, Vulkan wants me to create a 0 height swapchain while simultaneously 1x1 being the minimum allowed.
Assuming that the driver knows what it's doing, I proceed along and later I get a debug report stating that some internal "vkCreateImage failed", and after that Vulkan crashes leaving only death and destruction. How to handle that? Should I constrain currentExtent to min and max? Or am I missing something?
EDIT:
As the comments suggest, I'm copy-pasting parts of the code responsible for this. What I've noticed is that the OS actually resizes the window to 46x0 at some point, for some reason.
void QtWindow::resizeEvent(QResizeEvent *event)
{
Q_UNUSED(event);
scheduleSwapchainRecreation();
}
void QtWindow::scheduleSwapchainRecreation()
{
std::lock_guard<std::mutex> lock{mSwapchainRecreationMutex};
if (mSwapchainRecreationScheduled)
return;
mSwapchainRecreationScheduled = true;
QMetaObject::invokeMethod(this, "recreateSwapchain", Qt::QueuedConnection);
}
void QtWindow::recreateSwapchain()
{
std::lock_guard<std::mutex> lock{mStateMutex};
checkVkResult(vkDeviceWaitIdle(mDevice));
createSwapchain();
// state mutex should also cover swapchain recreation
mSwapchainRecreationScheduled = false;
}
void QtWindow::createSwapchain()
{
mSwapchain = makeUnique<VulkanSwapchain>(mMemoryManager,
width(),
height(),
mInstance,
mPhysicalDevice,
mDevice,
mSurface,
mPresentationQueueFamily,
mPresentationQueue,
mMemoryManager,
mSwapchain.get());
const auto min = mSwapchain->getMinExtent();
const auto max = mSwapchain->getMaxExtent();
setMinimumSize({ static_cast<int>(min.width), static_cast<int>(min.height) });
setMaximumSize({ static_cast<int>(max.width), static_cast<int>(max.height) });
}
The swapchain class:
VulkanSwapchain::VulkanSwapchain(uint32_t width,
uint32_t height,
VkInstance instance,
VkPhysicalDevice physicalDevice,
const VulkanDevice &device,
VkSurfaceKHR surface,
uint32_t presentationQueueFamily,
VkQueue presentationQueue,
const MemoryManagerPtr &memoryManager,
VulkanSwapchain *oldSwapchain)
: mInstance{instance}
, mDevice{device}
, mSurface{surface}
, mPresentQueue{presentationQueue}
, mDetails{{},{{}, memoryManager},{{}, memoryManager}}
, mSwapchainImages{memoryManager}
, mSwapchainImageViews{memoryManager}
, mMainCommandBuffers{memoryManager}
{
createSwapchain(width, height, physicalDevice, presentationQueueFamily, memoryManager, oldSwapchain);
createSwapchainImageViews();
createSemaphores();
createMainCommandPool();
}
VulkanSwapchain::~VulkanSwapchain()
{
if (mMainCommandPool != VK_NULL_HANDLE)
vkDestroyCommandPool(mDevice, mMainCommandPool, nullptr);
vkDestroySemaphore(mDevice, mAcquireSemaphore, nullptr);
for (const auto view : mSwapchainImageViews)
vkDestroyImageView(mDevice, view, nullptr);
if (mSwapchain != VK_NULL_HANDLE)
vkDestroySwapchainKHR(mDevice, mSwapchain, nullptr);
if (mSurface != VK_NULL_HANDLE)
vkDestroySurfaceKHR(mInstance, mSurface, nullptr);
}
VkResult VulkanSwapchain::acquire() noexcept
{
return vkAcquireNextImageKHR(mDevice, mSwapchain, UINT64_MAX, mAcquireSemaphore, VK_NULL_HANDLE, &mAcquiredImageIndex);
}
VkResult VulkanSwapchain::present(VkSemaphore renderingFinishedSemaphore) noexcept
{
VkPresentInfoKHR presentInfo = {};
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
presentInfo.waitSemaphoreCount = 1;
presentInfo.pWaitSemaphores = &renderingFinishedSemaphore;
presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = &mSwapchain;
presentInfo.pImageIndices = &mAcquiredImageIndex;
return vkQueuePresentKHR(mPresentQueue, &presentInfo);
}
void VulkanSwapchain::createSwapchain(uint32_t width,
uint32_t height,
VkPhysicalDevice physicalDevice,
uint32_t presentationQueueFamily,
const MemoryManagerPtr &memoryManager,
VulkanSwapchain *oldSwapchain)
{
GE_LOG << L"Creating swapchain..." << std::endl;
querySwapchainDetails(physicalDevice);
chooseSwapchainSurfaceFormat();
const auto chosenPresentMode = chooseSwapchainPresentMode();
chooseSwapchainExtents(width, height);
// triple or double buffering
auto imageCount = (chosenPresentMode == VK_PRESENT_MODE_MAILBOX_KHR) ? (3u) : (2u);
if (imageCount < mDetails.mCapabilities.minImageCount)
imageCount = mDetails.mCapabilities.minImageCount;
else if (mDetails.mCapabilities.maxImageCount > 0 && imageCount > mDetails.mCapabilities.maxImageCount)
imageCount = mDetails.mCapabilities.maxImageCount;
GE_LOG << L"Chosen surface format: " << mSwapchainFormat.format << L' ' << mSwapchainFormat.colorSpace << std::endl;
GE_LOG << L"Chosen present mode: " << chosenPresentMode << std::endl;
GE_LOG << L"Chosen extents: " << mExtent.width << L'x' << mExtent.height << std::endl;
GE_LOG << L"Image count: " << imageCount << std::endl;
VkSwapchainCreateInfoKHR createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
createInfo.surface = mSurface;
createInfo.minImageCount = imageCount;
createInfo.imageFormat = mSwapchainFormat.format;
createInfo.imageColorSpace = mSwapchainFormat.colorSpace;
createInfo.imageExtent = mExtent;
createInfo.imageArrayLayers = 1;
createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
createInfo.preTransform = mDetails.mCapabilities.currentTransform;
createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
createInfo.presentMode = chosenPresentMode;
createInfo.clipped = VK_TRUE;
if (oldSwapchain != nullptr)
createInfo.oldSwapchain = *oldSwapchain;
const auto graphicsQueueFamily = mDevice.getGraphicsQueueFamily();
if (graphicsQueueFamily != presentationQueueFamily)
{
uint32_t queueFamilyIndices[] = { graphicsQueueFamily, presentationQueueFamily };
createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
createInfo.queueFamilyIndexCount = 2;
createInfo.pQueueFamilyIndices = queueFamilyIndices;
}
else
{
createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
}
checkVkResult(vkCreateSwapchainKHR(mDevice, &createInfo, nullptr, &mSwapchain));
uint32_t swapchainImageCount = 0;
checkVkResult(vkGetSwapchainImagesKHR(mDevice, mSwapchain, &swapchainImageCount, nullptr));
mSwapchainImages.resize(swapchainImageCount);
checkVkResult(vkGetSwapchainImagesKHR(mDevice, mSwapchain, &swapchainImageCount, mSwapchainImages.data()));
}
void VulkanSwapchain::querySwapchainDetails(VkPhysicalDevice physicalDevice)
{
checkVkResult(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, mSurface, &mDetails.mCapabilities));
uint32_t numSurfaceFormats = 0;
checkVkResult(vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, mSurface, &numSurfaceFormats, nullptr));
if (numSurfaceFormats == 0)
BOOST_THROW_EXCEPTION(RenderingModule::Exception{"Cannot find surface formats."});
mDetails.mSurfaceFormats.resize(numSurfaceFormats);
checkVkResult(vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, mSurface, &numSurfaceFormats, mDetails.mSurfaceFormats.data()));
uint32_t numPresentModes = 0;
checkVkResult(vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, mSurface, &numPresentModes, nullptr));
if (numPresentModes == 0)
BOOST_THROW_EXCEPTION(RenderingModule::Exception{"Cannot find presentation modes."});
mDetails.mPresentModes.resize(numPresentModes);
checkVkResult(vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, mSurface, &numPresentModes, mDetails.mPresentModes.data()));
}
void VulkanSwapchain::chooseSwapchainExtents(uint32_t width, uint32_t height)
{
if (mDetails.mCapabilities.currentExtent.width != std::numeric_limits<uint32_t>::max())
mExtent.width = mDetails.mCapabilities.currentExtent.width;
else
mExtent.width = std::min(std::max(mDetails.mCapabilities.minImageExtent.width, width), mDetails.mCapabilities.maxImageExtent.width);
if (mDetails.mCapabilities.currentExtent.height != std::numeric_limits<uint32_t>::max())
mExtent.height = mDetails.mCapabilities.currentExtent.height;
else
mExtent.height = std::min(std::max(mDetails.mCapabilities.minImageExtent.height, height), mDetails.mCapabilities.maxImageExtent.height);
}
void VulkanSwapchain::chooseSwapchainSurfaceFormat()
{
VkSurfaceFormatKHR desiredSurfaceFormat = { VK_FORMAT_B8G8R8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR };
if (mDetails.mSurfaceFormats.size() == 1 && mDetails.mSurfaceFormats[0].format == VK_FORMAT_UNDEFINED)
{
mSwapchainFormat = desiredSurfaceFormat;
}
else
{
auto found = false;
for (const auto &format : mDetails.mSurfaceFormats)
{
if (format.format == desiredSurfaceFormat.format && format.colorSpace == desiredSurfaceFormat.colorSpace)
{
mSwapchainFormat = format;
found = true;
break;
}
}
if (!found)
{
// try without color space
for (const auto &format : mDetails.mSurfaceFormats)
{
if (format.format == desiredSurfaceFormat.format)
{
mSwapchainFormat = format;
found = true;
break;
}
}
if (!found)
mSwapchainFormat = mDetails.mSurfaceFormats.front();
}
}
}
void VulkanSwapchain::createSwapchainImageViews()
{
mSwapchainImageViews.resize(mSwapchainImages.size());
VkImageViewCreateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
createInfo.format = mSwapchainFormat.format;
createInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
createInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
createInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
createInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
createInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
createInfo.subresourceRange.levelCount = 1;
createInfo.subresourceRange.layerCount = 1;
for (auto i = 0u; i < mSwapchainImages.size(); ++i)
{
createInfo.image = mSwapchainImages[i];
checkVkResult(vkCreateImageView(mDevice, &createInfo, nullptr, &mSwapchainImageViews[i]));
}
}
void VulkanSwapchain::createSemaphores()
{
VkSemaphoreCreateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
checkVkResult(vkCreateSemaphore(mDevice, &createInfo, nullptr, &mAcquireSemaphore));
}
void VulkanSwapchain::createMainCommandPool()
{
VkCommandPoolCreateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
createInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
createInfo.queueFamilyIndex = mDevice.getGraphicsQueueFamily();
checkVkResult(vkCreateCommandPool(mDevice, &createInfo, nullptr, &mMainCommandPool));
mMainCommandBuffers.resize(mSwapchainImageViews.size());
VkCommandBufferAllocateInfo allocateInfo = {};
allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocateInfo.commandPool = mMainCommandPool;
allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocateInfo.commandBufferCount = static_cast<uint32_t>(mMainCommandBuffers.size());
checkVkResult(vkAllocateCommandBuffers(mDevice, &allocateInfo, mMainCommandBuffers.data()));
}
VkPresentModeKHR VulkanSwapchain::chooseSwapchainPresentMode() const
{
VkPresentModeKHR chosenPresentMode = VK_PRESENT_MODE_FIFO_KHR;
QSettings settings;
if (!settings.value(swapIntervalSettingName, true).toBool())
{
for (const auto mode : mDetails.mPresentModes)
{
if (mode == VK_PRESENT_MODE_IMMEDIATE_KHR)
{
chosenPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
break;
}
}
}
#if (defined(Q_OS_WIN) && !defined(Q_OS_WINPHONE)) || (defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID))
else if (settings.value(tripleBufferingSettingName, true).toBool())
{
// reduce latency on desktops, but preserve battery life on mobile
for (const auto mode : mDetails.mPresentModes)
{
if (mode == VK_PRESENT_MODE_MAILBOX_KHR)
{
chosenPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
break;
}
}
}
#endif
return chosenPresentMode;
}

Arduino and processing controlling servo

I am trying to control a servo using processing.
Basically, what I want is to press a button on processing and make the servo go from 0 to 180 degrees and back every two seconds and if I press another button on processing I should be able to move the servo using a potentiometer, however I am not able to make it work properly.
For example, when I pressed the POT button, the servo goes to the desired position, however if I move the potentiometer, the servo doesn't move unless I click the pot button again.
This is the relevant code for Arduino:
void loop ()
{
if(Serial.available()){
val= Serial.read();
while(val == 1){
digitalWrite(ledCPM, HIGH);
digitalWrite(ledPot, LOW);
digitalWrite(ledFSR,LOW);
cpmMovement();
val = Serial.read();
}
while(val == 2){
digitalWrite(ledPot, HIGH);
digitalWrite(ledCPM, LOW);
digitalWrite(ledFSR,LOW);
potMovement();
val = Serial.read();
}
while(val == 3){
digitalWrite(ledPot, LOW);
digitalWrite(ledCPM, LOW);
digitalWrite(ledFSR,HIGH);
fsrMovement();
val = Serial.read();
}
}
}
void cpmMovement(){unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval){
previousMillis = currentMillis;
if(positionservo == 0){
positionservo = 179;
myservo1.write(positionservo);
}
else{
positionservo = 0;
myservo1.write(positionservo);
}
}
}
void potMovement(){
int analogValuePot = analogRead(Pot)/4;
Serial.print("Potentiometer reading= ");
Serial.println(analogValuePot); // This will print the raw force value
int valuePot = map(analogValuePot, 0, 255, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo1.write(valuePot); // sets the servo position according to the scaled value
delay(10); // waits for the servo to get there
}
void fsrMovement(){
force = analogRead(FSR); // Reads the FSR
Serial.print("Force sensor reading = ");
Serial.println(force); // This will print the raw force value
int pos = map(force, 0, 1023, 0, 175); // Scales the force reading to degrees for servo control
Serial.print("servomotor degrees = ");
Serial.println(pos); // This will print the adjusted servo reading (an angle)
myservo1.write(pos); // Write the new angle to the servo
delay(150); // Delay 150 milliseconds before taking another reading
}
And here you can see the relevant processing code as everything else works ok
import processing.serial.*;
void mousePressed(){
println("Coordinates: " + mouseX + "," + mouseY);
if(pressedCPMButton && currentColorCPM==butColorCPM){ //Changing color CPM to pressed button color
currentColorCPM = butpreColorCPM;
currentColorPOT = butColorPOT;
currentColorFSR = butColorFSR;
myPort.write(1); //Send 1 to port
}else if(pressedCPMButton && currentColorCPM==butpreColorCPM){
currentColorCPM = butColorCPM;
currentColorPOT = butColorPOT;
currentColorFSR = butColorFSR;
}
if(pressedPOTButton && currentColorPOT==butColorPOT){
currentColorCPM = butColorCPM;
currentColorPOT = butpreColorPOT;
currentColorFSR = butColorFSR;
myPort.write(2); //Send 2 to port
}else if(pressedPOTButton && currentColorPOT==butpreColorPOT){
currentColorCPM = butColorCPM;
currentColorPOT = butColorPOT;
currentColorFSR = butColorFSR;
}
if(pressedFSRButton && currentColorFSR==butColorFSR){
currentColorCPM = butColorCPM;
currentColorPOT = butColorPOT;
currentColorFSR = butpreColorFSR;
myPort.write(3); //Send 3 to port
}else if(pressedFSRButton && currentColorFSR==butpreColorFSR){
currentColorCPM = butColorCPM;
currentColorPOT = butColorPOT;
currentColorFSR = butColorFSR;
}
}
boolean pressedButtonCPM(int x, int y, int width, int height) {
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
} else {
return false;
}
}
boolean pressedButtonPOT(int x, int y, int width, int height) {
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
} else {
return false;
}
}
boolean pressedButtonFSR(int x, int y, int width, int height) {
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
} else {
return false;
}
}

Initializer-string for array of chars is too long error on Arduino

I am trying to run a code below on Arduino but when I verify the code, it shows,
'Initializer-string for array of chars is too long'.
Although I have read previous questions regarding similar issues, I couldn't know where to begin in the code I am trying now. Yes,,, I am very new to C++ world,,, If you can give me a clue to teach myself or a direct answer, it would be amazingly appreciated.
Best,
/**** SET YOUR MAC ADDRESS HERE ****/
char mac[13] = "74-E5-43-BE-42-10";
/***********************************/
#define LED 13
#define BLUESMIRFON 2
#define FACTORYRESETBAUD 57600
#define DEFAULTBAUD 115200
char str[3];
char passkey[5] = "0000";
boolean success = false;
int failOuts[10] = {3,4,5,6,7,8,9,10,11,12};
void setup()
{
//Initialize pins
pinMode(LED, OUTPUT);
pinMode(BLUESMIRFON, OUTPUT);
for (int i=0; i<10; i++) {
pinMode(failOuts[i], OUTPUT);
}
// First reset to factory defaults
while (!success) {
RunBlueSmirfSetup(true);
}
success = false;
// Then set up with the correct mac address
RunBlueSmirfSetup(false);
}
void loop() {
if(success) {
digitalWrite(LED,LOW);
delay(1000);
digitalWrite(LED,HIGH);
delay(1000);
}
}
void RunBlueSmirfSetup(boolean factoryReset) {
//Initialize serial ports
if (factoryReset) {
Serial.begin(FACTORYRESETBAUD);
} else {
Serial.begin(DEFAULTBAUD);
}
digitalWrite(BLUESMIRFON, LOW);
delay(2000);
digitalWrite(BLUESMIRFON, HIGH);
delay(2000); //Wait for BlueSMIRF to turn on
Serial.print('$'); //Send command to put BlueSMIRF into programming mode
Serial.print('$');
Serial.print('$');
delay(100);
Serial.flush();
//Reset the module
if (factoryReset) {
Serial.print('S');
Serial.print('F');
Serial.print(',');
Serial.print('1');
Serial.print('\r');
while(Serial.available() < 3);
str[0] = (char)Serial.read();
str[1] = (char)Serial.read();
str[2] = (char)Serial.read();
if(str[0] == 'A' && str[1] == 'O' && str[2] == 'K') {
success = true;
} else {
success = false;
digitalWrite(failOuts[0],HIGH);
}
delay(100);
Serial.flush();
} else {
//Set the baudrate
Serial.print('S');
Serial.print('U');
Serial.print(',');
Serial.print('5');
Serial.print('7');
Serial.print('\r');
while(Serial.available() < 3);
str[0] = (char)Serial.read();
str[1] = (char)Serial.read();
str[2] = (char)Serial.read();
if(str[0] == 'A' && str[1] == 'O' && str[2] == 'K') {
success = true;
} else {
success = false;
digitalWrite(failOuts[1],HIGH);
}
delay(100);
Serial.flush();
//Set the remote MAC address
Serial.print('S');
Serial.print('R');
Serial.print(',');
for(int i = 0; i < 12; i++) {
Serial.print(mac[i]);
}
Serial.print('\r');
while(Serial.available() < 3);
str[0] = (char)Serial.read();
str[1] = (char)Serial.read();
str[2] = (char)Serial.read();
if(str[0] == 'A' && str[1] == 'O' && str[2] == 'K') {
success = true;
} else {
success = false;
digitalWrite(failOuts[2],HIGH);
}
delay(100);
Serial.flush();
//Set the passkey
Serial.print('S');
Serial.print('P');
Serial.print(',');
for(int i = 0; i < 4; i++) {
Serial.print(passkey[i]);
}
Serial.print('\r');
while(Serial.available() < 3);
str[0] = (char)Serial.read();
str[1] = (char)Serial.read();
str[2] = (char)Serial.read();
if(str[0] == 'A' && str[1] == 'O' && str[2] == 'K') {
success = true;
} else {
success = false;
digitalWrite(failOuts[3],HIGH);
}
delay(100);
Serial.flush();
//Set the BlueSMiRF mode
Serial.print('S');
Serial.print('M');
Serial.print(',');
Serial.print('3');
Serial.print('\r');
while(Serial.available() < 3);
str[0] = (char)Serial.read();
str[1] = (char)Serial.read();
str[2] = (char)Serial.read();
if(str[0] == 'A' && str[1] == 'O' && str[2] == 'K') {
success = true;
} else {
success = false;
digitalWrite(failOuts[4],HIGH);
}
delay(100);
Serial.flush();
delay(100);
//Exit command mode
}
Serial.print('-');
Serial.print('-');
Serial.print('-');
Serial.print('\r');
//delay(100);
//Serial.flush();
//delay(100);
//Serial.end();
//digitalWrite(BLUESMIRFON, LOW);
}
CleanProgramBlueSMiRF CleanProgramBlueSMiRF
It would help if you copied the exact error message including the line number.
I assume it is failing on:
char mac[13] = "74-E5-43-BE-42-10";
since it has 17 chars between the quotes and you allocated 13.

Resources