the ASCII table symbols are not displayed - console

The console does not display characters from ASCII from 1 to 31, instead they display question marks in the rectangles. Tell me what to do, please
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<conio.h>
#include<time.h>
int main()
{
const int startingMoney = 500;
const int turnCost = 50;
const int doubleBonus = 60;
const int tripleBobus = 100;
int money = startingMoney;
char panel0 = 'X';
char panel1 = 'X';
char panel2 = 'X';
srand(time(NULL));
do
{
system("cls");
printf("\n\n");
printf("\t ######### \n");
printf("\t# BANDITO #\n");
printf("\t###########\n");
printf("\t# #\n");
printf("\t# %c %c %c #\n", panel0, panel1, panel2);
printf("\t# #\n");
printf("\t###########\n");
printf("\n");
printf("\tMoney: %d$\n", money);
_getch();
money = money - turnCost;
panel0 = 3 + (rand() % 4);
panel1 = 3 + (rand() % 4);
panel2 = 3 + (rand() % 4);
if ((panel0 == panel1) && (panel0 == panel2))
{
money = money + tripleBobus;
}
if ((panel0 == panel1) || (panel0 == panel2) || panel1 == panel2)
{
money = money + doubleBonus;
}
} while (money >= turnCost);
return 0;
}

Related

How to send a text file and append it using Arduino

I'm working on a GPS logger project. I have an Arduino Mega set up with a transmitter and receiver and GPS and GSM modules. I also have an Arduino Uno with a transmitter, receiver and buzzer.
When the two devices go too far from each other, the GPS data is pulled and sent to my web server using the GSM module. When I send my file to the server it creates a text file and when I move to another location it overwrites the previous location.
I'm trying to figure out how to append the text file instead of overwriting it. I found a lot of stuff using an SD card shield but nothing without it.
#define GPS_PIN_1 9 // GPS serial pin RX
#define GPS_PIN_2 8 // GPS serial pin TX
#define CHECKPIN 13 // Pin that lights up when things are checked
#define GPSRATE 4800 // GPS baud rate
#include <SoftwareSerial.h>
#include <Flash.h>
#include <Streaming.h>
// How many bytes of input to buffer from the GPS?
#define BUFFERSIZE 100
#define ENDLN
int rx1Pin=31;
int txPin=30;
int tx1Pin=11;
int onModulePin = 2;
int rxPin=12;
SoftwareSerial mySerial = SoftwareSerial(GPS_PIN_1, GPS_PIN_2); //(rx,tx)
SoftwareSerial txSerial = SoftwareSerial(rxPin, txPin);
SoftwareSerial rxSerial = SoftwareSerial(rx1Pin, tx1Pin);
char sendChar ='H';
char incomingChar = 0;
int counter=0;
//GPS variables
int numSats = 0;
int fixType = 0;
int time[] = {
0, 0, 0};
double latitude = 0.0;
double longitude = 0.0;
long altitude = 0;
long maxAlt = 0;
int speed = 0;
int txCount = 0;
int ExOnce = 0;
int FalcomCheck = 0;
int LogCheck =0;
unsigned long GpsOffTime = 0;
unsigned long SmsStart = 0; // SMS-time
char buffer[BUFFERSIZE];
void switchModule(){ // Function to switch the module ON;
digitalWrite(onModulePin,HIGH);
delay(2000);
digitalWrite(onModulePin,LOW);
delay(2000);
}
void setup(){
pinMode(rxPin, INPUT);
pinMode(txPin,OUTPUT);
pinMode(rx1Pin, INPUT);
pinMode(tx1Pin,OUTPUT);
pinMode(GPS_PIN_1, INPUT);
pinMode(GPS_PIN_2, OUTPUT);
pinMode(7, OUTPUT);
pinMode(13, OUTPUT);
digitalWrite(7, HIGH);
pinMode(onModulePin, OUTPUT);
txSerial.begin(4800);
rxSerial.begin(4800);
mySerial.begin(4800);
Serial.begin(19200); // The GPRS baud rate
switchModule(); // Switch the module ON
for (int i=0;i<2;i++){ // Wait 20 sec for connection
delay(10000);
}
}
void loop(){
txSerial.println(sendChar);
Serial.println(sendChar);
for(int i=0; i<6; i++) {
incomingChar = rxSerial.read(); //Read incoming message from TX.
if (incomingChar =='L') {
GPS();//Serial.println("It Works");
}
}
}
void GPS(){
Serial.flush();
// Get a GGA string from the GPS, and
// check if it's a valid fix, and extract the data.
getNMEA("$GPGGA");
delay(100);
numSats = getSats();
fixType = getFixType();
// Make sure we have a valid fix
if (fixType != 0) {
getTime(time);
latitude = getLat();
longitude = getLong();
altitude = getAlt();
// Keep track of the maximum altitude
}
// Convert latitude and longitude into strings.
char latString[12];
char longString[12];
doubleToString(latitude, 4, latString);
doubleToString(longitude, 4, longString);
sprintf(buffer, "%02d:%02d:%02d,%s,%s,%ld",
time[0], time[1], time[2], latString, longString, altitude);
Serial.println(buffer);
if (fixType > 0) {
if (ExOnce==0){
digitalWrite(13, HIGH);
//ExOnce=1;
sendsms();
logftp();
// for (int i=0; i<3; i++) { // Wait 30 sec for a connection.
// delay(10000);
// }
}
}
delay(200);
}
void sendsms(){
Serial.println("AT+CMGF=1"); // Set the SMS mode to text.
delay(500);
Serial.print("AT+CMGS="); // Send the SMS the number.
Serial.print(34,BYTE); // Send the " char.
Serial.print("**********"); // Send the number change *********
// by the actual number.
Serial.println(34,BYTE); // Send the " char.
delay(1500);
Serial.print("Hi this is the General text testing."); // The SMS body
delay(500);
Serial.print(0x1A,BYTE); // End of message command 1A (hex)
delay(20000);
}
void logftp(){
Serial.println("AT&k3"); // Flow activate
delay(1000);
Serial.print("AT+KCNXCFG=0,"); // Connect to GPRS
Serial.print(34,BYTE);
Serial.print("GPRS");
Serial.print(34,BYTE);
Serial.print(",");
Serial.print(34,BYTE);
//Serial.print("wap.cingular");
Serial.print("epc.tmobile.com");
Serial.print(34,BYTE);
Serial.print(",");
Serial.print(34,BYTE);
Serial.print(34,BYTE);
Serial.print(",");
Serial.print(34,BYTE);
Serial.println(34,BYTE);
delay(1000);
Serial.println("AT+KCNXTIMER=0,60,2,70"); // Set timers
delay(1000);
Serial.println("AT+CGATT=1"); // Network check
delay(1000);
Serial.print("AT+KFTPCFG=0,"); //FTP configuration/connect
Serial.print(34,BYTE);
Serial.print("ftp.insertaddress.com"); //FTP address
Serial.print(34,BYTE);
Serial.print(",");
Serial.print(34,BYTE);
Serial.print("username"); //Username
Serial.print(34,BYTE);
Serial.print(",");
Serial.print(34,BYTE);
Serial.print("password"); //Password
Serial.print(34,BYTE);
Serial.println(",21,0"); //Port
delay(500);
Serial.print("AT+KPATTERN=");
Serial.print(34,BYTE);
Serial.print("--EOF--Pattern--");
Serial.println(34,BYTE);
delay(500);
Serial.print("AT+KFTPSND=0,,");
Serial.print(34,BYTE);
Serial.print("log"); //Directory folder of FTP
Serial.print(34,BYTE);
Serial.print(",");
Serial.print(34,BYTE);
Serial.print("pol.txt"); //Text file
Serial.print(34,BYTE);
Serial.println(",0");
delay(12000);
Serial.print(buffer);
Serial.println("--EOF--Pattern--");
delay(12000);
Serial.println("AT+KTCPCLOSE=1,1");
delay(1000);
}
// ------- GPS Parsing ----------
// Reads a line from the GPS NMEA serial output
// Give up after trying to read 1000 bytes (~2 seconds)
int readLine(void) {
char c;
byte bufferIndex = 0;
boolean startLine = 0;
byte retries = 0;
while (retries < 20) {
c = mySerial.read();
if (c == -1) {
delay(2);
continue;
}
if (c == '\n') continue;
if (c == '$') startLine = 1;
if ((bufferIndex == BUFFERSIZE-1) || (c == '\r')) {
if (startLine) {
buffer[bufferIndex] = 0;
return 1;
}
}
if (startLine)
buffer[bufferIndex++] = c;
//}
else {
retries++;
delay(50);
}
}
return 0;
}
// Returns a specific field from the buffer
void getField(int getId, char *field, int maxLen) {
byte bufferIndex = 0;
byte fieldId = 0;
byte i = 0;
while (bufferIndex < sizeof(buffer)) {
if (fieldId == getId) {
// End of string, or string overflow
if (buffer[bufferIndex] == ',' || i > (maxLen - 2)) {
field[i] = 0; // Null terminate
return;
}
// Buffer chars to field
field[i++] = buffer[bufferIndex++];
}
else {
// Advance field on comma
if (buffer[bufferIndex] == ',') {
bufferIndex++; //Advance in buffer
fieldId++; // Increase field position counter
}
else {
bufferIndex++; // Advance in buffer
}
}
}
// Null terminate incase we didn't already..
field[i] = 0;
}
// Polls for an NMEA sentence of type requested
// Validates checksum, silently retries on failed checksums
int getNMEA(char *getType) {
char type[7];
byte retries = 0;
while (retries < 2) {
if (readLine() && validateChecksum()) {
;
getField(0, type, sizeof(type));
if (strcmp(type, getType) == 0) {
return 1;
}
}
else {
retries++;
}
}
Serial.println("Failed to read GPS");
return 0;
}
// Validates the checksum on an NMEA string
// Returns 1 on valid checksum, 0 otherwise
int validateChecksum(void) {
char gotSum[2];
gotSum[0] = buffer[strlen(buffer) - 2];
gotSum[1] = buffer[strlen(buffer) - 1];
// Check that the checksums match up
if ((16 * atoh(gotSum[0])) + atoh(gotSum[1]) == getCheckSum(buffer))
return 1;
else
return 0;
}
// Calculates the checksum for a given string
// returns as integer
int getCheckSum(char *string) {
int i;
int XOR;
int c;
// Calculate checksum ignoring any $'s in the string
for (XOR = 0, i = 0; i < strlen(string); i++) {
c = (unsigned char)string[i];
if (c == '*') break;
if (c != '$') XOR ^= c;
}
return XOR;
}
// Returns the groundspeed in km/h
int getSpeed(void) {
char field[10];
getField(7, field, sizeof(field));
int speed = atoi(field);
return speed;
}
// Return the fix type from a GGA string
int getFixType(void) {
char field[5];
getField(6, field, sizeof(field));
int fixType = atoi(field);
return fixType;
}
// Return the altitude in meters from a GGA string
long getAlt(void) {
char field[10];
getField(9, field, sizeof(field));
long altitude = atol(field);
return altitude;
}
// Returns the number of satellites being tracked from a GGA string
int getSats(void) {
char field[3];
getField(7, field, sizeof(field));
int numSats = atoi(field);
return numSats;
}
// Read the latitude in decimal format from a GGA string
double getLat(void) {
char field[12];
getField(2, field, sizeof(field)); // read the latitude
double latitude = atof(field); // convert to a double (precise)
int deg = (int) latitude / 100; // extract the number of degrees
double min = latitude - (100 * deg); // work out the number of minutes
latitude = deg + (double) min/60.0; // convert to decimal format
getField(3, field, sizeof(field)); // get the hemisphere (N/S)
// sign the decimal latitude correctly
if (strcmp(field, "S") == 0)
latitude *= -1;
return latitude;
}
// Read the longitude in decimal format from a GGA string
double getLong(void) {
char field[12];
getField(4, field, sizeof(field)); // read the longitude
double longitude = atof(field); // convert to a double
int deg = (int) longitude / 100; // extract the number of degrees
double min = longitude - (100 * deg); // work out the number of minutes
longitude = deg + (double) min/60.00; // convert to decimal format
getField(5, field, sizeof(field)); // get the E/W status
// sign decimal latitude correctly
if (strcmp(field, "W") == 0)
longitude *= -1;
return longitude;
}
// Converts UTC time to the correct timezone
void convertTime(int *time) {
// How many hours off GMT are we?
float offset = -5;
long sectime = ((long)(time[0]) * 3600) + (time[1] * 60) + time[2];
sectime += (offset * 3600.0);
// Did we wrap around?
if (sectime < 0) sectime += 86400;
if (sectime > 86400) sectime -= 86400;
// Convert back to time
time[0] = (int)(sectime / 3600);
time[1] = (int)((sectime % 3600) / 60);
time[2] = (int)((sectime % 3600) % 60);
}
// Parses a time field from a GGA string
void parseTime(char *field, int *time) {
char tmp[3];
tmp[2] = 0; // Init tmp and null terminate
tmp[0] = field[0];
tmp[1] = field[1];
time[0] = atoi(tmp); // Hours
tmp[0] = field[2];
tmp[1] = field[3];
time[1] = atoi(tmp); // Minutes
tmp[0] = field[4];
tmp[1] = field[5];
time[2] = atoi(tmp); // Seconds
}
// Gets the hours, minutes and seconds from a GGA string
void getTime(int *time) {
char field[12];
getField(1, field, sizeof(field));
parseTime(field, time);
convertTime(time);
}
// ------ MISC ----------
// Returns a string with a textual representation of a float
void doubleToString(double val, int precision, char *string){
// Print the int part
sprintf(string, "%d", (int)(val));
if(precision > 0) {
// Print the decimal point
strcat(string, ".");
unsigned long frac;
unsigned long mult = 1;
int padding = precision -1;
while (precision--) {
mult *=10;
}
if (val >= 0)
frac = (val - (int)(val)) * mult;
else
frac = ((int)(val)- val ) * mult;
unsigned long frac1 = frac;
while (frac1 /= 10) {
padding--;
}
while (padding--) {
strcat(string, "0");
}
// Convert and print the fraction part
sprintf(string+strlen(string), "%d", (int)(frac));
}
}
// Converts a HEX string to an int
int atoh(char c) {
if (c >= 'A' && c <= 'F')
return c - 55;
else if (c >= 'a' && c <= 'f')
return c - 87;
else
return c - 48;
}
If the server is where the text file is received via SMS/GSM, that server program controls how the file is written or appended. Your posted Arduino code would not need to be changed. On the server, where you now open the file - first check if the file exists and if it does, change the open function to add append mode. Exactly how depends on the OS/language of your server program. But you should be able to figure it out from reading the documentation on the open call.

Index or String out of bounds exception error

My program is supposed to find bad words in a given input, but sometimes I get an out of bounds error. What is the cause of this error?
/* Andrew Woan - Alien Message Board Project - Period 1 */
import java.lang.Math;
import java.util.Scanner;
public class Alien {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int dollars = 0;
int qey = 0;
int carrots = 0;
System.out.println("Enter a message board post:");
String post = scan.nextLine();
String[] parts = post.split("-");
String username = parts[0]; // Alien's Username
String message = parts[1]; // Alien's Message
System.out.println(username + "-" + message);
int space = 0;
int anotherspace = message.substring(space, message.length()).indexOf(" ");
while (anotherspace != -1) {
String badword = message.substring(space, space + anotherspace);
if (badword.equals("qey")) {
qey++;
}
if (badword.equals("^^")) {
carrots++;
}
if (badword.equals("$")) {
dollars++;
}
space = +anotherspace + 1;
anotherspace = message.substring(space, message.length()).indexOf(" ");
}
char lastdollars = message.charAt(message.length() - 1);
char seconddollars = message.charAt(message.length() - 2);
char lastqey = message.charAt(message.length() - 1);
char secondqey = message.charAt(message.length() - 2);
char thirdqey = message.charAt(message.length() - 3);
char fourthqey = message.charAt(message.length() - 4);
char lastcarrots = message.charAt(message.length() - 1);
char secondcarrots = message.charAt(message.length() - 2);
char thirdcarrots = message.charAt(message.length() - 3);
if (((lastcarrots == '^') && (secondcarrots == '^') && (thirdcarrots == ' '))) {
carrots++;
}
if (((lastqey == 'y') && (secondqey == 'e') && (thirdqey == 'q') && (fourthqey == ' '))) {
qey++;
}
if (((lastdollars == '$') && (seconddollars == ' '))) {
dollars++;
}
if (qey > 0 || carrots > 0 || dollars > 0) {
System.out.println("");
System.out.println("Results:");
System.out.println("");
System.out.println("BAD");
System.out.println(username);
System.out.println("^^: " + carrots);
System.out.println("qey: " + qey);
System.out.println("$: " + dollars);
} else {
System.out.println("");
System.out.println("Results:");
System.out.println("");
System.out.println("CLEAN");
}
}
}
I don't know for certain but have you tried changing this line?
space =+ anotherspace +1;
To:
space += anotherspace;
You already increment by 1 with the += and then you want to do it again. This is from a quick glance.

qt q3table eat sectionHandleDoubleClicked

i am using q3Table.
i want to expand the column width to max item width by double click the edge of the header
while trying to connect to h3Header::sectionHandleDoubleClicked
i found in Q3Table code that it override the header double click event and not emitting the signal.
class Q_COMPAT_EXPORT Q3TableHeader : public Q3Header
{
void mouseDoubleClickEvent(QMouseEvent *e);
...
void Q3TableHeader::mouseDoubleClickEvent(QMouseEvent *e)
{
if (e->button() != LeftButton)
return;
if (isResizing) {
int p = real_pos(e->pos(), orientation()) + offset();
int section = sectionAt(p);
if (section == -1)
return;
section--;
if (p >= sectionPos(count() - 1) + sectionSize(count() - 1))
++section;
while (sectionSize(section) == 0)
section--;
if (section < 0)
return;
int oldSize = sectionSize(section);
if (orientation() == Horizontal) {
table->adjustColumn(section);
int newSize = sectionSize(section);
if (oldSize != newSize)
emit sizeChange(section, oldSize, newSize);
for (int i = 0; i < table->numCols(); ++i) {
if (table->isColumnSelected(i) && sectionSize(i) != 0)
table->adjustColumn(i);
}
} else {
table->adjustRow(section);
int newSize = sectionSize(section);
if (oldSize != newSize)
emit sizeChange(section, oldSize, newSize);
for (int i = 0; i < table->numRows(); ++i) {
if (table->isRowSelected(i) && sectionSize(i) != 0)
table->adjustRow(i);
}
}
}
}
as in the original q3Header a signal is emitted.
void Q3Header::mouseDoubleClickEvent(QMouseEvent *e)
{
int p = orient == Qt::Horizontal ? e->pos().x() : e->pos().y();
p += offset();
if(reverse())
p = d->lastPos - p;
int header = handleAt(p);
if (header >= 0)
emit sectionHandleDoubleClicked(header);
}
how can i get the same effect as sectionHandleDoubleClicked?
or how can i achieve this effect?

Adding a total of items swiped with Arduino Uno and RFID tags

I am developing an RFID based handheld device that enables a user to swipe a product and total their purchase on the device. What would the code be to setting the RFID tags to display the price as well as adding the tags together once swiped?
Is this possible with the Arduino Uno? Here's what I have so far as it's just displaying the RFID tag once swiped.
int RFIDResetPin = 13;
char tag1[13] = "5000B9A7155B"
char tag2[13] = "5000B9A7155B"
char tag3[13] = "5000B9A7155B"
char tag4[13] = "5000B9A7155B"
char tag5[13] = "5000B9A7155B"
void setup() {
Serial.begin(9600); // Connect to the serial port.
}
void loop () {
byte i = 0;
byte val = 0;
byte code[6];
byte checksum = 0;
byte bytesread = 0;
byte tempbyte = 0;
if(Serial.available() > 0) {
if((val = Serial.read()) == 2) { // Check for header
bytesread = 0;
while (bytesread < 12) { // Read 10 digit code + 2 digit checksum
if( Serial.available() > 0) {
val = Serial.read();
if((val == 0x0D)||
(val == 0x0A)||
(val == 0x03)||
(val == 0x02)) { // If header or stop bytes before the 10 digit reading.
break; // Stop reading
}
// Do ASCII/hexadecimal conversion:
if ((val >= '0') && (val <= '9')) {
val = val - '0';
}
else
if ((val >= 'A') && (val <= 'F')) {
val = 10 + val - 'A';
}
// Every two hex-digits, add byte to code:
if (bytesread & 1 == 1) {
// Make some space for this hex-digit by
// shifting the previous hex-digit with 4 bits to the left:
code[bytesread >> 1] = (val | (tempbyte << 4));
if (bytesread >> 1 != 5) { // If we're at the checksum byte,
checksum ^= code[bytesread >> 1]; // Calculate the checksum... (XOR)
};
}
else {
tempbyte = val; // Store the first hex digit first...
};
bytesread++; // Ready to read next digit
}
}
// Output to Serial:
if (bytesread == 12) { // If 12 digit read is complete
Serial.print("5-byte code: ");
for (i=0; i<5; i++) {
if (code[i] < 16)
Serial.print("0");
Serial.print(code[i], HEX);
Serial.print(" ");
}
Serial.println();
Serial.print("Checksum: ");
Serial.print(code[5], HEX);
Serial.println(code[5] == checksum ? " -- passed." : " -- error.");
Serial.println();
}
bytesread = 0;
}
}
}

How to programmatically detect if a bitmap has alpha channel?

As subject. Preferibly using C code.
=========MFC++ version
private: static Boolean __gc* BitmapHasAlpha(BitmapData __gc* bmpData)
{
if ((bmpData->PixelFormat != PixelFormat::Format32bppArgb) && (bmpData->PixelFormat != PixelFormat::Format32bppRgb))
{
return false;
}
for (Int32 __gc* i = 0; (i < bmpData->Height); i++)
{
Int32 __gc* num2 = (i * bmpData->Stride);
for (Int32 __gc* j = 3; (j < (bmpData->Width * 4)); j += 4)
{
Byte __gc** numPtr = *static_cast<__box Byte __gc***>(((bmpData->Scan0->ToPointer() + num2) + j));
if (numPtr[0] != 0)
{
return true;
}
}
}
return false;
}
=========C# version
private static unsafe bool BitmapHasAlpha(BitmapData bmpData)
{
if ((bmpData.PixelFormat != PixelFormat.Format32bppArgb) && (bmpData.PixelFormat != PixelFormat.Format32bppRgb))
{
return false;
}
for (int i = 0; i < bmpData.Height; i++)
{
int num2 = i * bmpData.Stride;
for (int j = 3; j < (bmpData.Width * 4); j += 4)
{
byte* numPtr = ((byte*)bmpData.Scan0.ToPointer()) + num2 + j;
if (numPtr[0] != 0)
{
return true;
}
}
}
return false;
}
With ::GetDIBits as described on #333559
On that answer it is not clear what to do with GetDiBits():
GetDIBits( hDC, hBmp, 0, 1, (void**) &bits, &bmi, DIB_RGB_COLORS );
bits[3] == alpha of topleft pixel;
Should bits[3] be tested against zero ? what to do with that value ? thanks,
I have implemented a prototype but it is not working fine, so there may be something wrong in the code. I share it here with the hope that we can fix it together:
BOOL HasAlphaChannel( HBITMAP hBmp )
{
HDC hDC = CreateCompatibleDC( NULL );
BITMAPINFO bmi;
void * bits;
unsigned long ul;
BOOL bAlphaChannel = FALSE;
memset( &bmi, 0, sizeof( BITMAPINFO ) );
bmi.bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
GetDIBits( hDC, hBmp, 0, 1, NULL, &bmi, DIB_RGB_COLORS );
bits = malloc( bmi.bmiHeader.biSizeImage );
// this is returning zero wich means error (why???)
GetDIBits( hDC, hBmp, 0, bmi.bmiHeader.biHeight, &bits, &bmi, DIB_RGB_COLORS );
for( ul = 0; ul < bmi.bmiHeader.biSizeImage; ul += 4 )
if( ( ( char * ) bits )[ ul + 3 ] != 0 )
bAlphaChannel = TRUE;
free( bits );
DeleteDC( hDC );
return bAlphaChannel;
}
Thanks!
Use GetDIBits(hdc, hbmp, 0, 1, NULL, &bmi, DIB_RGB_COLORS).
And then see if bmi.bmiHeader.biBitCount has value of 32 then it has alpha channel, otherwise it doesn't have alpha.

Resources