creedarn Posted December 28, 2020 Share Posted December 28, 2020 Hello! new on the forums :D I have decided on doing the panels on the viggen. I have posted a more of a follow along topic on mudspike forums where i will update how it goes. But right now i am a bit stuck. I want to save slots on my arduino and are using the analogread voltage thing quite alot. but when i try to adapt my code for the Datapanel selector, I am abit over my head. I am unsure how/what paramaters i can put in the line: sendDcsBiosMessage(“DATAPANEL_SELECTOR”, “0”) My whole code: Quote #define DCSBIOS_DEFAULT_SERIAL #include “DcsBios.h” /* paste code snippets from the reference documentation here */ const int analogInPin = A0; int sensorValue = 0; // value read from the buttons int state = 0; // used for storing what button was pressed void setup() { DcsBios::setup(); Serial.begin(9600); // initialize serial communications at 9600 bps } void loop() { DcsBios::loop(); Serial.print("Value = "); Serial.println(sensorValue); Serial.print("State = "); Serial.println(state); } void readSwitch () { //creates the function to read the switch states sensorValue = analogRead(analogInPin); //read the analog pin the switches are connected to if (sensorValue < 600 && sensorValue > 550) { //if the analog reading is within a certain range we know which button was pressed and set the state accordingly state = 0; sendDcsBiosMessage(“DATAPANEL_SELECTOR”, “0”); //have tried with and without citation marks delay(100); //delay for debounce purposes } if (sensorValue < 550 && sensorValue > 520) { //if the analog reading is within a certain range we know which button was pressed and set the state accordingly state = 1; sendDcsBiosMessage(“DATAPANEL_SELECTOR”, “1”); //have tried with and without citation marks delay(100); //delay for debounce purposes } if (sensorValue < 520 && sensorValue > 480) { //if the analog reading is within a certain range we know which button was pressed and set the state accordingly state = 2; sendDcsBiosMessage(“DATAPANEL_SELECTOR”, “2”); //have tried with and without citation marks delay(100); //delay for debounce purposes } if (sensorValue < 480 && sensorValue > 370) { //if the analog reading is within a certain range we know which button was pressed and set the state accordingly state = 3; sendDcsBiosMessage(“DATAPANEL_SELECTOR”, “3”); //have tried with and without citation marks delay(100); //delay for debounce purposes } if (sensorValue < 370 && sensorValue > 280 ) { //if the analog reading is within a certain range we know which button was pressed and set the state accordingly state = 4; sendDcsBiosMessage(“DATAPANEL_SELECTOR”, “4”); //have tried with and without citation marks delay(100); //delay for debounce purposes } if (sensorValue < 280 && sensorValue > 130 ) { //if the analog reading is within a certain range we know which button was pressed and set the state accordingly state = 5; sendDcsBiosMessage(“DATAPANEL_SELECTOR”, “5”); //have tried with and without citation marks delay(100); //delay for debounce purposes } if (sensorValue < 130 && sensorValue >= 0 ) { //if the analog reading is within a certain range we know which button was pressed and set the state accordingly state = 6; sendDcsBiosMessage(“DATAPANEL_SELECTOR”, “6”); //have tried with and without citation marks delay(100); } delay(1); //delay for timeing purposes } With this code i am able to read the values of each state on each turn of the rotary switch but it does not seem to work with the senddcsmessage part. Like i wrote in my mudspike topic: "Now I am not so good at the coding part. I have borrowed all code and experimented with them and edited them to my liking. But this last part doesn’t work the rest does. If anyone sees something wrong I’d love to try and understand why it is wrong :) I dont know if it just is something with the sendDcsBiosMessage, that the msg is not correct or the arg. because i can not find any relatable topics that have any info, the closest is this one which say there are x amount of possible values to put out but on the viggen there should be 0-6 but maybe it is not the value which it should send?" When i read on the dcs bios tutorial it says that the original code/snippet: Quote const byte datapanelSelectorPins[13] = {PIN_0, PIN_1, PIN_2, PIN_3, PIN_4, PIN_5, PIN_6} DcsBios::SwitchMultiPos datapanelSelector("DATAPANEL_SELECTOR", datapanelSelectorPins, 13); SwitchMultiPos The SwitchMultiPos code snippet is meant for multi-position selector switches with more than three positions. A rotary switch has one center terminal and one terminal for each switch position. Connect the center terminal to ground and each switch position terminal to an Arduino pin. When the Arduino sees that an I/O pin is LOW, it will send the corresponding switch position to DCS: World. When no pin is LOW, it will assume the switch position has not changed. it says that pin_0-pin_6 is low because they should be connected to a ground pin. So i also tried send message with False after "x" in the code. ps: sorry for the diffrent kind of fonts, not sure what happened there xD Thanks in advance :) Link to comment Share on other sites More sharing options...
No1sonuk Posted December 29, 2020 Share Posted December 29, 2020 (edited) Having written the part below about mapping, I noticed two problems with your code: 1: The readSwitch function isn't called in your loop(), so you're not reading the switch. 2: There's a space between "readSwitch" and "()". "readSwitch ()" should be "readSwitch()". Try fixing those and see if it works. Now on to what I was originally going to post: Are you trying to make a multi-position switch into a potentiometer? Assuming a linear distribution of your switch numbers, you could probably reduce most of that code by using the map function. e.g. void readSwitch() { //creates the function to read the switch states sensorValue = analogRead(analogInPin); //read the analog pin the switches are connected to state = map(sensorValue, 0, 600, 0, 6); // Format is "map(value, fromLow, fromHigh, toLow, toHigh)" sendDcsBiosMessage(“DATAPANEL_SELECTOR”, state); // ASSUMING this is correct delay(100); } Edited December 29, 2020 by No1sonuk Code box Link to comment Share on other sites More sharing options...
No1sonuk Posted December 29, 2020 Share Posted December 29, 2020 (edited) And once you get it working (or even before), you should add a "previous state check" so that it doesn't send the state unless it's different to the previous value. This cuts processing and data transmission time. So what I wrote becomes: void readSwitch() { //creates the function to read the switch states sensorValue = analogRead(analogInPin); //read the analog pin the switches are connected to state = map(sensorValue, 0, 600, 0, 6); // Format is "map(value, fromLow, fromHigh, toLow, toHigh)" if (state != previousState){ // Check state is "not equal to" previousState sendDcsBiosMessage(“DATAPANEL_SELECTOR”, state); // ASSUMING this is correct previousState = state; // Set previousState to the new value for the next time round } delay(100); } And don't forget to add "previousState" to the variable definitions. Edited December 29, 2020 by No1sonuk Link to comment Share on other sites More sharing options...
creedarn Posted December 29, 2020 Author Share Posted December 29, 2020 Thanks I will try that. But like what you are touching on, I believe it lies in the senddcsmessage line. But I will try it and return. Link to comment Share on other sites More sharing options...
Recommended Posts